Move entire line up and down in Vim

后端 未结 19 975
无人及你
无人及你 2020-11-29 14:27

In Notepad++, I can use Ctrl + Shift + Up / Down to move the current line up and down. Is there a similar command to this in Vim?

相关标签:
19条回答
  • 2020-11-29 15:02

    This worked for me:

    http://vim.wikia.com/wiki/Moving_lines_up_or_down_in_a_file

    BTW, if you want to use ALT+some_key and your terminal (urxvt does this) refuses to comply, you should enter something like this in your .vimrc:

    " For moving lines (^] is a special character; use <M-k> and <M-j> if it works)
    nnoremap ^]k mz:m-2<CR>`z==
    inoremap ^]j <Esc>:m+<CR>==gi
    inoremap ^]k <Esc>:m-2<CR>==gi
    vnoremap ^]j :m'>+<CR>gv=`<my`>mzgv`yo`z
    nnoremap ^]j mz:m+<CR>`z==
    vnoremap ^]k :m'<-2<CR>gv=`>my`<mzgv`yo`z
    

    where ^] is a single character that represents the ALT key. To input that character, use C+v, Esc in Vim (C+q, Esc on Windows).

    0 讨论(0)
  • 2020-11-29 15:02

    Here's a simplified version, for MacVim, using the the Wikia article examples (cf. link from gun's comment).

    " Move selection up/down (add =gv to reindent after move)
    :vmap <D-S-Up> :m-2<CR>gv
    :vmap <D-S-Down> :m'>+<CR>gv
    

    I'm using only the block selection variant, because all it takes is Shift-V to select the current line, and optionally cursor up/down to select some more lines.

    According to the shortcuts above, pressing Cmd-Shift-Up/Down will shift the block selection up/down. "D" is the Command key in MacVim, for Windows try "C" (Control), or "A" (Alt) (eg. <C-A-f> would be Control Alt f).

    The Wikia article adds "=gv" to these, which has the effect to adjust the indentation of the block after the move, based on surrounding text. This is confusing so I removed it, and added shortcuts for quickly indenting the selection instead.

    " Indent selection left/right (Cmd Shift Left/Right is used for Tab switching)
    :vmap <D-A-Left> <gv
    :vmap <D-A-Right> >gv
    

    Mind, the same can be done with << and >> but the selection would be lost, so these shortcuts above allow to indent multiple times and still move the block around because the selection is maintained.

    My MacVim is configured to switch Tabs with Cmd-Shift-Left/Right so I used Cmd-Alt-Left/Right.

    Here's the Tab switching for MacVim (put in .gvimrc with the rest above):

    :macm Window.Select\ Previous\ Tab key=<D-S-Left>
    :macm Window.Select\ Next\ Tab key=<D-S-Right>
    
    0 讨论(0)
  • 2020-11-29 15:05

    A simple solution is to put in your .vimrc these lines:

    nmap <C-UP> :m-2<CR>  
    nmap <C-DOWN> :m+1<CR>
    
    0 讨论(0)
  • 2020-11-29 15:05

    I put the following at the end of my .vimrc file:

    noremap H ddkkp
    noremap N ddp
    

    So now 'H' and 'N' move current line up and down respectively.

    0 讨论(0)
  • 2020-11-29 15:07

    add the following to ~/.vimrc file (make sure you have no mapping for n,m )

    nmap n :m +1<CR>
    nmap m :m -2<CR>
    

    now pressing n key will move a line down and m will move a line up.

    0 讨论(0)
  • 2020-11-29 15:07

    In command mode position the cursor on the line you want to move down, and then

    ddp
    

    Explanation: dd deletes the current line to the general buffer p puts it back AFTER the cursor position, or in case of entire lines, one line below.

    There is some confusion regarding commands p and P in many docs. In reality p pastes AFTER cursor, and P AT cursor.

    0 讨论(0)
提交回复
热议问题