在Vim中上下移动整条线

本文翻译自:Move entire line up and down in Vim

In Notepad++, I can use Ctrl + Shift + Up / Down to move the current line up and down. 在Notepad ++中,我可以使用Ctrl + Shift + 向上 /向下来上下移动当前行。 Is there a similar command to this in Vim? 在Vim中有类似的命令吗? I have looked through endless guides, but have found nothing. 我看过无尽的向导,却一无所获。

If there isn't, how could I bind the action to that key combination? 如果没有,我怎么能将动作绑定到该组合键?

Edit: Mykola's answer works for all lines, apart from those at the beginning and end of the buffer. 编辑:Mykola的答案适用于所有行,除了缓冲区的开头和结尾处。 Moving the first line up or the bottom line down deletes the line, and when moving the bottom line up it jumps two spaces initially, like a pawn! 向上移动第一行或向下移动底线会删除该行,当向下移动底线时,它最初会跳转两个空格,就像一个棋子! Can anyone offer any refinements? 任何人都可以提供任何改进吗?


#1楼

参考:https://stackoom.com/question/36yk/在Vim中上下移动整条线


#2楼

vim plugin unmpaired.vim [e和] e


#3楼

:m.+1 or :m.-2 would do if you're moving a single line. :m。+ 1或:m.-2如果你移动一行就行。 Here's my script to move multiple lines. 这是我移动多行的脚本。 In visual mode, Alt-up/Alt-down will move the lines containing the visual selection up/down by one line. 在可视模式下,Alt-up / Alt-down将包含视觉选择的线向上/向下移动一行。 In insert mode or normal mode, Alt-up/Alt-down will move the current line if no count prefix is given. 在插入模式或正常模式下,如果没有给出计数前缀,则Alt-up / Alt-down将移动当前行。 If there's a count prefix, Alt-up/Alt-down will move that many lines beginning from the current line up/down by one line. 如果有一个计数前缀,Alt-up / Alt-down将从当前行开始向上/向下移动多行。

function! MoveLines(offset) range
    let l:col = virtcol('.')
    let l:offset = str2nr(a:offset)
    exe 'silent! :' . a:firstline . ',' . a:lastline . 'm'
        \ . (l:offset > 0 ? a:lastline + l:offset : a:firstline + l:offset)
    exe 'normal ' . l:col . '|'
endf

imap <silent> <M-up> <C-O>:call MoveLines('-2')<CR>
imap <silent> <M-down> <C-O>:call MoveLines('+1')<CR>
nmap <silent> <M-up> :call MoveLines('-2')<CR>
nmap <silent> <M-down> :call MoveLines('+1')<CR>
vmap <silent> <M-up> :call MoveLines('-2')<CR>gv
vmap <silent> <M-down> :call MoveLines('+1')<CR>gv

#4楼

Assuming the cursor is on the line you like to move. 假设光标位于您想要移动的线上。

Moving up and down: :m for move 上下移动:m移动

:m +1 - moves down 1 line :m +1 - 向下移动1行

:m -2 - move up 1 lines :m -2 - 向上移动1行

(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) (注意你可以用任何数字替换+1,具体取决于你想要向上或向下移动多少行,即+2将它向下移动2行,-3将它向上移动2行)

To move to specific line 移动到特定的行

:set number - display number lines (easier to see where you are moving it to) :set number - 显示数字行(更容易看到您将其移动到的位置)

:m 3 - move the line after 3rd line (replace 3 to any line you'd like) :m 3 - 在第3行之后移动线(将3替换为您想要的任何线)

Moving multiple lines: 移动多行:

V (ie Shift - V ) and move courser up and down to select multiple lines in VIM V (即Shift - V )并向上和向下移动courser以在VIM中选择多条线

once selected hit : and run the commands above, m +1 etc 一旦选中命中并运行上面的命令, m +1


#5楼

I put the following at the end of my .vimrc file: 我把以下内容放在我的.vimrc文件的末尾:

noremap H ddkkp
noremap N ddp

So now 'H' and 'N' move current line up and down respectively. 所以现在'H'和'N'分别上下移动当前线。


#6楼

This worked for me: 这对我有用:

http://vim.wikia.com/wiki/Moving_lines_up_or_down_in_a_file 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: 顺便说一句,如果您想使用ALT + some_key并且您的终端(urxvt这样做)拒绝遵守,您应该在.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. 其中^]是表示ALT键的单个字符 To input that character, use C+v, Esc in Vim ( C+q, Esc on Windows). 要输入该字符,请在Vim中使用C + v,EscC + q, Windows上的Esc )。