`
studyworks
  • 浏览: 16720 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Moving lines up or down in a file

阅读更多

Moving lines up or down in a file

From Vim Tips Wiki

Tip 646 Previous Next created 2004 · complexity basic · author Frank Butler · version 6.0


The following mappings in your vimrc provide a quick way to move lines of text up or down. The mappings work in normal, insert and visual modes, allowing you to move the current line or a selected block of lines.

nnoremap <M-j> mz:m+<CR>`z==
nnoremap <M-k> mz:m-2<CR>`z==
inoremap <M-j> <Esc>:m+<CR>==gi
inoremap <M-k> <Esc>:m-2<CR>==gi
vnoremap <M-j> :m'>+<CR>gv=`<my`>mzgv`yo`z
vnoremap <M-k> :m'<-2<CR>gv=`>my`<mzgv`yo`z

Press Alt-j to move the current line down, or press Alt-k to move the current line up. The == re-indents the line to suit its new position.

Explanation

The command :m 5 moves the current line to below line 5. If the number starts with + or -, it is relative to the current line, so :m +5 moves the current line down by 5 lines (+5 is interpreted as .+5 where . means the current line). The space after :m is not required, and the +1 can be written as + (the 1 is assumed).

See also

Comments

 TO DO 

  • A quick test shows that this is very nice, and if polished, it could be a featured tip.
  • Why does it use a mark? I only tried the normal-mode mapping, but it seemed to work fine with the mz and `z removed.
  • Need to improve my above explanation.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics