Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
Line 1: Line 1:
  +
{{TipImported
{{Tip
 
 
|id=47
 
|id=47
  +
|previous=46
|title=Swapping characters, words and lines
 
  +
|next=48
 
|created=March 13, 2001
 
|created=March 13, 2001
 
|complexity=basic
 
|complexity=basic
Line 7: Line 8:
 
|version=5.7
 
|version=5.7
 
|rating=15/14
 
|rating=15/14
|text=
 
 
}}
 
}}
 
 
==Normal-Mode Commands==
 
==Normal-Mode Commands==
 
Swap the '''current character''' (the character under the cursor) with the next:
 
Swap the '''current character''' (the character under the cursor) with the next:
xp
+
xp
   
 
Swap the '''current line''' with the next:
 
Swap the '''current line''' with the next:
ddp
+
ddp
   
 
Swap the '''current word''' with the next (see note):
 
Swap the '''current word''' with the next (see note):
dawwP
+
dawwP
dawelp
+
dawelp
   
 
Swap the '''current word''' with the previous (see note):
 
Swap the '''current word''' with the previous (see note):
dawbP
+
dawbP
   
 
''Note:'' These approaches to swap words don't work around punctuation and will do the wrong thing. For this reason, it is recommended that you use some mappings (see below) that will more intelligently move words around.
 
''Note:'' These approaches to swap words don't work around punctuation and will do the wrong thing. For this reason, it is recommended that you use some mappings (see below) that will more intelligently move words around.
Line 28: Line 27:
 
==Normal-Mode Mappings==
 
==Normal-Mode Mappings==
 
To use '''gc''' to swap the '''current character''' with the next, without changing the cursor position:
 
To use '''gc''' to swap the '''current character''' with the next, without changing the cursor position:
:nnoremap <silent> gc xph
+
:nnoremap <silent> gc xph
   
 
To use '''gw''' to swap the '''current word''' with the next, without changing cursor position: (See note.)
 
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>
+
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l>
" This version will work across newlines:
+
" This version will work across newlines:
: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>
   
 
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.)
 
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>
+
: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.)
 
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>
+
: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:
 
To use '''g{''' to swap the '''current paragraph''' with the next:
:nnoremap g{ {dap}p{
+
:nnoremap g{ {dap}p{
   
 
''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 <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}}
Line 52: Line 51:
 
To use this mapping: first, delete some text (using any normal Vim command, such as '''daw''', '''dt,''' in normal mode, '''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.
 
To use this mapping: first, delete some text (using any normal Vim command, such as '''daw''', '''dt,''' in normal mode, '''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
+
:vnoremap <C-X> <Esc>`.``gvP``P
   
 
===Scripts===
 
===Scripts===
 
 
* Chip Campbell has written a ''Visual Mode Based Swapping'' script similar to the above, available at http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs
 
* Chip Campbell has written a ''Visual Mode Based Swapping'' script similar to the above, available at http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs
   

Revision as of 05:08, 28 October 2007

Tip 47 Printable Monobook Previous Next

created March 13, 2001 · complexity basic · author Chip Campbell, Arun Easi, Benji Fisher · version 5.7


Normal-Mode Commands

Swap the current character (the character under the cursor) with the next:

xp

Swap the current line with the next:

ddp

Swap the current word with the next (see note):

dawwP
dawelp

Swap the current word with the previous (see note):

dawbP

Note: These approaches to swap words don't work around punctuation and will do the wrong thing. For this reason, it is recommended that you use some mappings (see below) that will more intelligently move words around.

Normal-Mode Mappings

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

Other Solutions

Visual-Mode swapping

To use this mapping: first, delete some text (using any normal Vim command, such as daw, dt, in normal mode, 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

Scripts

Comments