Move entire line up and down in Vim

后端 未结 19 1150
无人及你
无人及你 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

    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  :m-2gv
    :vmap  :m'>+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. 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   >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=
    :macm Window.Select\ Next\ Tab key=
    

提交回复
热议问题