BenArmston (talk | contribs) mNo edit summary |
(Change <tt> to <code>, perhaps also minor tweak.) |
||
(11 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
− | {{review}} |
||
{{TipImported |
{{TipImported |
||
|id=191 |
|id=191 |
||
|previous=189 |
|previous=189 |
||
|next=193 |
|next=193 |
||
− | |created= |
+ | |created=2002 |
|complexity=intermediate |
|complexity=intermediate |
||
|author=Kontra Gergely |
|author=Kontra Gergely |
||
|version=5.7 |
|version=5.7 |
||
|rating=31/15 |
|rating=31/15 |
||
− | |category1= |
+ | |category1=Usage |
|category2= |
|category2= |
||
}} |
}} |
||
+ | == Swapping lines and columns == |
||
+ | The [https://github.com/salsifis/vim-transpose transpose.vim plugin] can be used to transpose arrays of text, lines becoming columns and columns becoming lines. It can handle arrays of text or arrays of delimited fields. |
||
− | Swapping a line with the line directly above or below it is a common task when |
||
+ | |||
− | writing computer software. This tip provides some mappings for normal, insert |
||
+ | == Swapping lines == |
||
− | and visual modes which make this more easily accomplished. |
||
+ | |||
+ | Swapping a line with the line directly above or below it is a common task when writing computer software. This tip provides some mappings for normal, insert and visual modes which make this more easily accomplished. Adding the following to your _vimrc file allows you to move a line (or multiple lines) of text up or down within a document using the <Control> and <Up>/<Down> arrow keys. This is similar to the <Alt> and <Up>/<Down> arrow text movement feature in Eclipse. |
||
<pre> |
<pre> |
||
function! MoveLineUp() |
function! MoveLineUp() |
||
− | + | call MoveLineOrVisualUp(".", "") |
|
endfunction |
endfunction |
||
− | |||
function! MoveLineDown() |
function! MoveLineDown() |
||
− | + | call MoveLineOrVisualDown(".", "") |
|
endfunction |
endfunction |
||
− | |||
function! MoveVisualUp() |
function! MoveVisualUp() |
||
− | + | call MoveLineOrVisualUp("'<", "'<,'>") |
|
− | + | normal gv |
|
endfunction |
endfunction |
||
− | |||
function! MoveVisualDown() |
function! MoveVisualDown() |
||
− | + | call MoveLineOrVisualDown("'>", "'<,'>") |
|
− | + | normal gv |
|
endfunction |
endfunction |
||
− | |||
function! MoveLineOrVisualUp(line_getter, range) |
function! MoveLineOrVisualUp(line_getter, range) |
||
− | + | let l_num = line(a:line_getter) |
|
− | + | if l_num - v:count1 - 1 < 0 |
|
− | + | let move_arg = "0" |
|
− | + | else |
|
− | + | let move_arg = a:line_getter." -".(v:count1 + 1) |
|
− | + | endif |
|
− | + | call MoveLineOrVisualUpOrDown(a:range."move ".move_arg) |
|
endfunction |
endfunction |
||
− | |||
function! MoveLineOrVisualDown(line_getter, range) |
function! MoveLineOrVisualDown(line_getter, range) |
||
− | + | let l_num = line(a:line_getter) |
|
− | + | if l_num + v:count1 > line("$") |
|
− | + | let move_arg = "$" |
|
− | + | else |
|
− | + | let move_arg = a:line_getter." +".v:count1 |
|
− | + | endif |
|
− | + | call MoveLineOrVisualUpOrDown(a:range."move ".move_arg) |
|
endfunction |
endfunction |
||
− | |||
function! MoveLineOrVisualUpOrDown(move_arg) |
function! MoveLineOrVisualUpOrDown(move_arg) |
||
− | + | let col_num = virtcol(".") |
|
− | + | execute "silent! ".a:move_arg |
|
− | + | execute "normal! ".col_num."|" |
|
endfunction |
endfunction |
||
+ | nnoremap <silent> <C-Up> :<C-u>call MoveLineUp()<CR> |
||
− | |||
− | nnoremap |
+ | nnoremap <silent> <C-Down> :<C-u>call MoveLineDown()<CR> |
− | + | inoremap <silent> <C-Up> <C-o>:call MoveLineUp()<CR> |
|
− | inoremap |
+ | inoremap <silent> <C-Down> <C-o>:call MoveLineDown()<CR> |
− | + | "vnoremap <silent> <C-Up> :<C-u>call MoveVisualUp()<CR> |
|
− | vnoremap |
+ | "vnoremap <silent> <C-Down> :<C-u>call MoveVisualDown()<CR> |
− | + | xnoremap <silent> <C-Up> :<C-u>call MoveVisualUp()<CR> |
|
+ | xnoremap <silent> <C-Down> :<C-u>call MoveVisualDown()<CR> |
||
</pre> |
</pre> |
||
− | The above mappings all take a count so < |
+ | The above mappings all take a count so <code>5<C-Up></code> would move the current line (or visual selection) 5 lines up. |
− | line (or visual selection) 5 lines up. |
||
+ | They make use of the <code>:move</code> command to do the movement which avoids overwritting a register which other methods may suffer from. As <code>:move</code> wont move a line to an invalid location an extra check is made to move no further than the start (or end) of the file. The mappings are also careful to restore the original cursor location in the line (or selection) by first getting it with <code>virtcol()</code> and then restoring it with <code>|</code>. |
||
− | They make use of the <tt>:move</tt> command to do the movement which avoids |
||
− | overwritting a register which other methods may suffer from. As <tt>:move</tt> |
||
− | wont move a line to an invalid location an extra check is made to move no |
||
− | further than the start (or end) of the file. The mappings are also careful to |
||
− | restore the original cursor location in the line (or selection) by first |
||
− | getting it with <tt>virtcol()</tt> and then restoring it with <tt>|</tt>. |
||
− | ==See |
+ | ==See also== |
+ | *[[Moving lines up or down]] |
||
+ | *[[Swapping characters, words and lines]] |
||
+ | *{{script|id=1590|text=unimpaired.vim}} <code>[e</code> and <code>]e</code> exchange current line with [count] lines above/below |
||
+ | *{{script|id=2586|text=upAndDown.vim}} plugin implementing this tip |
||
+ | *[http://vimcasts.org/episodes/bubbling-text/ Text "bubbling"] |
||
+ | ==References== |
||
− | *[[Swapping_characters%2C_words_and_lines]] |
||
*{{help|:move}} |
*{{help|:move}} |
||
*{{help|v:count1}} |
*{{help|v:count1}} |
||
*{{help|virtcol()}} |
*{{help|virtcol()}} |
||
*{{help|bar}} |
*{{help|bar}} |
||
+ | |||
+ | ==Comments== |
Revision as of 07:49, 11 July 2012
created 2002 · complexity intermediate · author Kontra Gergely · version 5.7
Swapping lines and columns
The transpose.vim plugin can be used to transpose arrays of text, lines becoming columns and columns becoming lines. It can handle arrays of text or arrays of delimited fields.
Swapping lines
Swapping a line with the line directly above or below it is a common task when writing computer software. This tip provides some mappings for normal, insert and visual modes which make this more easily accomplished. Adding the following to your _vimrc file allows you to move a line (or multiple lines) of text up or down within a document using the <Control> and <Up>/<Down> arrow keys. This is similar to the <Alt> and <Up>/<Down> arrow text movement feature in Eclipse.
function! MoveLineUp() call MoveLineOrVisualUp(".", "") endfunction function! MoveLineDown() call MoveLineOrVisualDown(".", "") endfunction function! MoveVisualUp() call MoveLineOrVisualUp("'<", "'<,'>") normal gv endfunction function! MoveVisualDown() call MoveLineOrVisualDown("'>", "'<,'>") normal gv endfunction function! MoveLineOrVisualUp(line_getter, range) let l_num = line(a:line_getter) if l_num - v:count1 - 1 < 0 let move_arg = "0" else let move_arg = a:line_getter." -".(v:count1 + 1) endif call MoveLineOrVisualUpOrDown(a:range."move ".move_arg) endfunction function! MoveLineOrVisualDown(line_getter, range) let l_num = line(a:line_getter) if l_num + v:count1 > line("$") let move_arg = "$" else let move_arg = a:line_getter." +".v:count1 endif call MoveLineOrVisualUpOrDown(a:range."move ".move_arg) endfunction function! MoveLineOrVisualUpOrDown(move_arg) let col_num = virtcol(".") execute "silent! ".a:move_arg execute "normal! ".col_num."|" endfunction nnoremap <silent> <C-Up> :<C-u>call MoveLineUp()<CR> nnoremap <silent> <C-Down> :<C-u>call MoveLineDown()<CR> inoremap <silent> <C-Up> <C-o>:call MoveLineUp()<CR> inoremap <silent> <C-Down> <C-o>:call MoveLineDown()<CR> "vnoremap <silent> <C-Up> :<C-u>call MoveVisualUp()<CR> "vnoremap <silent> <C-Down> :<C-u>call MoveVisualDown()<CR> xnoremap <silent> <C-Up> :<C-u>call MoveVisualUp()<CR> xnoremap <silent> <C-Down> :<C-u>call MoveVisualDown()<CR>
The above mappings all take a count so 5<C-Up>
would move the current line (or visual selection) 5 lines up.
They make use of the :move
command to do the movement which avoids overwritting a register which other methods may suffer from. As :move
wont move a line to an invalid location an extra check is made to move no further than the start (or end) of the file. The mappings are also careful to restore the original cursor location in the line (or selection) by first getting it with virtcol()
and then restoring it with |
.
See also
- Moving lines up or down
- Swapping characters, words and lines
- unimpaired.vim
[e
and]e
exchange current line with [count] lines above/below - upAndDown.vim plugin implementing this tip
- Text "bubbling"