|
|
| Line 44: |
Line 44: |
| |
See [[moving lines up or down]] for the best method to '''move lines'''. |
|
See [[moving lines up or down]] for the best method to '''move lines'''. |
| |
|
|
|
| − |
To use <tt>gc</tt> to swap the '''current character''' with the next, without changing the cursor position: |
+ |
To use <code>gc</code> to swap the '''current character''' with the next, without changing the cursor position: |
| |
<pre> |
|
<pre> |
| |
:nnoremap <silent> gc xph |
|
:nnoremap <silent> gc xph |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
To use <tt>gw</tt> to swap the '''current word''' with the next, without changing cursor position: (See note.) |
+ |
To use <code>gw</code> to swap the '''current word''' with the next, without changing cursor position: (See note.) |
| |
<pre> |
|
<pre> |
| |
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l> |
|
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l> |
| Line 56: |
Line 56: |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
To use <tt>gl</tt> to swap the '''current word''' with the previous, keeping cursor on current word: (This feels like "pushing" the word to the left.) (See note.) |
+ |
To use <code>gl</code> to swap the '''current word''' with the previous, keeping cursor on current word: (This feels like "pushing" the word to the left.) (See note.) |
| |
<pre> |
|
<pre> |
| |
:nnoremap <silent> gl "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l> |
|
:nnoremap <silent> gl "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l> |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
To use <tt>gr</tt> to swap the '''current word''' with the next, keeping cursor on current word: (This feels like "pushing" the word to the right.) (See note.) |
+ |
To use <code>gr</code> to swap the '''current word''' with the next, keeping cursor on current word: (This feels like "pushing" the word to the right.) (See note.) |
| |
<pre> |
|
<pre> |
| |
:nnoremap <silent> gr "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o>/\w\+\_W\+<CR><c-l> |
|
:nnoremap <silent> gr "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o>/\w\+\_W\+<CR><c-l> |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
To use <tt>g{</tt> to swap the '''current paragraph''' with the next: |
+ |
To use <code>g{</code> to swap the '''current paragraph''' with the next: |
| |
<pre> |
|
<pre> |
| |
:nnoremap g{ {dap}p{ |
|
:nnoremap g{ {dap}p{ |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
''Note:'' Mappings above which perform a search-and-replace (ones containing <tt>:s//</tt>) will operate incorrectly on words with accented characters. To adjust the mappings above to work with your locale, replace all <tt>\w</tt> with [''alphabet''] and <tt>\W</tt> with <tt>[^''alphabet'']</tt>, where ''alphabet'' is the set of characters in your alphabet. {{help|/\w}} |
+ |
''Note:'' Mappings above which perform a search-and-replace (ones containing <code>:s//</code>) will operate incorrectly on words with accented characters. To adjust the mappings above to work with your locale, replace all <code>\w</code> with [''alphabet''] and <code>\W</code> with <code>[^''alphabet'']</code>, where ''alphabet'' is the set of characters in your alphabet. {{help|/\w}} |
| |
|
|
|
| |
==Visual-mode swapping== |
|
==Visual-mode swapping== |
| − |
To use this mapping: first, delete some text (using a command such as <tt>daw</tt> or <tt>dt</tt> in normal mode, or <tt>x</tt> in visual mode). Then, use visual mode to select some other text, and press Ctrl-X. The two pieces of text should then be swapped. |
+ |
To use this mapping: first, delete some text (using a command such as <code>daw</code> or <code>dt</code> in normal mode, or <code>x</code> in visual mode). Then, use visual mode to select some other text, and press Ctrl-X. The two pieces of text should then be swapped. |
| |
<pre> |
|
<pre> |
| |
:vnoremap <C-X> <Esc>`.``gvP``P |
|
:vnoremap <C-X> <Esc>`.``gvP``P |
Normal-mode commands
Swap the current character (the character under the cursor) with the next:
xp
Swap the current line with the next (but see below for a better method):
ddp
Swap the current line with the previous:
ddkP
Swap the current word with the next (see note):
dawwP
dawelp
"xdiwdwep"xp (works with most punctuations too)
Swap the current word with the previous (see note):
dawbP
Note: These swap-word techniques don't work with punctuation. Use the mappings below to more intelligently move words. However, for many more complex swaps, several plugins are available with often do a better job.
Mappings
See moving lines up or down for the best method to move lines.
To use gc to swap the current character with the next, without changing the cursor position:
:nnoremap <silent> gc xph
To use gw to swap the current word with the next, without changing cursor position: (See note.)
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l>
" This version will work across newlines:
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l>
To use gl to swap the current word with the previous, keeping cursor on current word: (This feels like "pushing" the word to the left.) (See note.)
:nnoremap <silent> gl "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l>
To use gr to swap the current word with the next, keeping cursor on current word: (This feels like "pushing" the word to the right.) (See note.)
:nnoremap <silent> gr "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o>/\w\+\_W\+<CR><c-l>
To use g{ to swap the current paragraph with the next:
:nnoremap g{ {dap}p{
Note: Mappings above which perform a search-and-replace (ones containing :s//) will operate incorrectly on words with accented characters. To adjust the mappings above to work with your locale, replace all \w with [alphabet] and \W with [^alphabet], where alphabet is the set of characters in your alphabet. :help /\w
Visual-mode swapping
To use this mapping: first, delete some text (using a command such as daw or dt in normal mode, or x in visual mode). Then, use visual mode to select some other text, and press Ctrl-X. The two pieces of text should then be swapped.
:vnoremap <C-X> <Esc>`.``gvP``P
Related plugins
See also