Blaise
Blaise

Reputation: 7518

Move whole line up/down shortcut in nano (analogue to intelliJ or Visual Studio)

How to move a line of text up/down in Nano linux command line editor?

Is there any analogue way to do that as in IntelliJ Idea:

  1. Place the caret at the line to be moved.
  2. Do one of the following:

On the main menu, choose Code | Move Line Up or Code | Move Line Down.

Press Shift+Alt+Up or Shift+Alt+Down.

Upvotes: 108

Views: 39695

Answers (3)

月夜龙Ladon
月夜龙Ladon

Reputation: 1

These you can just use it like the vscode. In these functions i used {anchor} {prevanchor} & {nextanchor} to registered begin & end point for copy. and use "{cut}{paste}" to locating the points so you would prob face to trouble when try to undo.

## Fully select the lines that you selected. 
bind ^L "{cut}{anchor}{paste}{down}{left}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}" main

## Move up current line (or selected lines)
bind M-, "{cut}{anchor}{paste}{down}{left}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}{cut}{up}{anchor}{paste}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}" main

## Move down current line (or selected lines)
bind M-. "{cut}{anchor}{paste}{down}{left}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}{cut}{down}{anchor}{paste}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}" main

## Duplicate current line (or selected lines)
bind M-D "{cut}{anchor}{paste}{down}{left}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{copy}{paste}{anchor}{prevanchor}{anchor}{mark}{nextanchor}{anchor}" main

Upvotes: 0

SpinUp __ A Davis
SpinUp __ A Davis

Reputation: 5531

You can also add a binding to accomplish this in your personal settings file, which is usually found at ~/.nanorc or ~/.config/nano/nanorc (you can create one if neither exist).

This uses nano functions to move a line up or down when pressing alt-d or shift-alt-d, respectively:

## Move line up or down
bind M-d "{cut}{up}{paste}{up}" main
bind Sh-M-d "{cut}{down}{paste}{up}" main

I also like to use a similar binding for a "duplicate line" function, bound to Ctrl-d:

## ^D = duplicate line
bind ^D "{copy}{paste}{up}" main

The full details for how to set these bindings is available in the nanorc man page. The documentation indicates that it's not possible to rebind the arrow keys, unfortunately.

Upvotes: 2

Alvaro Gutierrez Perez
Alvaro Gutierrez Perez

Reputation: 3887

You can use Ctrl+K to cut a line, move to destination position and press Ctrl+U to paste it.

Upvotes: 177

Related Questions