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.
分享到:
相关推荐
Added TFlexFileFormat.StreamSupport property (default True), which difines, is it necessary to open file stream or give the file name only. - FIX: If the TCustomProp.Assign method calls when the ...
Open, view, and save an image in a different file format 117 Image format conversion for JPEG, PNG, TIFF, GIF, BMP 118 Image rotation in the plane of the image 120 Image size alteration 121 ...
- With Delphi XE2 or XE3 it is possible to compile WPTools into a 64 bit applications. You need WPTools 6 PRO or WPTools 6 Premium for this. - Localization and Inch/CM selection see ...
Bounce (example 4) - ball moving up and down. bounce4.py Bounce (example 5) - ball moving around the screen. bounce5.py Chapter 14 Bounce (example 6) - adding the paddle. bounce6.py Bounce (example 7...
- Built in file viewer (Lister) to view files of ANY SIZE in hex, 捋? 圹? binary or text format, using either the ASCII- (DOS) or the 捋? 圹? ANSI- (Windows) character set. The line width and font ...
SYNONYMS move, rearrange, backwards, forwards, up, down HINT This command is available in history modeling mode. HELP DSN_feature_edit BITMAP reorderfeature ACTIONS STANDARD SEPARATOR ...
1192 It s not a Bug, It s a Feature! 简单题 1196 Fast Food 简单题 1107 FatMouse and Cheese 简单题,不过题目描述有些混乱 1136 Multiple 简单题,BFS 1276 Optimal Array Multiplication Sequence 简单...
1192 It s not a Bug, It s a Feature! 简单题 1196 Fast Food 简单题 1107 FatMouse and Cheese 简单题,不过题目描述有些混乱 1136 Multiple 简单题,BFS 1276 Optimal Array Multiplication Sequence 简单...
Setting Up a Pygame Program ..................................................................................................... 8 Game Loops and Game States ............................................