Move entire line up and down in Vim

后端 未结 19 972
无人及你
无人及你 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 14:54

    Just add this code to .vimrc (or .gvimrc)

    nnoremap <A-j> :m+<CR>==
    nnoremap <A-k> :m-2<CR>==
    inoremap <A-j> <Esc>:m+<CR>==gi
    inoremap <A-k> <Esc>:m-2<CR>==gi
    vnoremap <A-j> :m'>+<CR>gv=gv
    vnoremap <A-k> :m-2<CR>gv=gv
    
    0 讨论(0)
  • 2020-11-29 14:56

    Here is a solution that works on my machine : MacBook Pro running VIM 8.1

    These commands will not delete your lines at the top or bottom of your buffer.

    Using the actual symbols that Alt-J and Alt-K output is a workaround for their key-codes not mapping properly in my environment.

    Throw this in the old .vimrc and see if works for you.

    " Maps Alt-J and Alt-K to macros for moving lines up and down
    " Works for modes: Normal, Insert and Visual
    nnoremap ∆ :m .+1<CR>==
    nnoremap ˚ :m .-2<CR>==
    inoremap ∆ <Esc>:m .+1<CR>==gi
    inoremap ˚ <Esc>:m .-2<CR>==gi
    vnoremap ∆ :m '>+1<CR>gv=gv
    vnoremap ˚ :m '<-2<CR>gv=gv
    
    0 讨论(0)
  • 2020-11-29 14:59

    Assuming the cursor is on the line you like to move.

    Moving up and down: :m for move

    :m +1 - moves down 1 line

    :m -2 - move up 1 lines

    (Note you can replace +1 with any numbers depending on how many lines you want to move it up or down, ie +2 would move it down 2 lines, -3 would move it up 2 lines)

    To move to specific line

    :set number - display number lines (easier to see where you are moving it to)

    :m 3 - move the line after 3rd line (replace 3 to any line you'd like)

    Moving multiple lines:

    V (i.e. Shift-V) and move courser up and down to select multiple lines in VIM

    once selected hit : and run the commands above, m +1 etc

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

    For me, ddkkp did it (instead of ddkP with an uppercase P, which would work too).

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

    Exactly what you're looking for in this awesome plugin: https://github.com/vim-scripts/upAndDown

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

    In case you want to do this on multiple lines that match a specific search:

    • Up: :g/Your query/ normal ddp or :g/Your query/ m -1
    • Down :g/Your query/ normal ddp or :g/Your query/ m +1
    0 讨论(0)
提交回复
热议问题