Vim Tips Wiki

Swapping characters, words and lines

Redirected from VimTip47

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 47 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0

Normal-mode commandsEdit

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


Swap the current character with the previous:


Swap the current line with the next (but see below for a better method):


Swap the current line with the previous:

ddkP or ddkkp

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

hdeep                (works at start of first word, useful if your separators aren't spaces)
"xdiwdwep"xp         (works with most punctuations too)

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


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 which often do a better job.


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>:nohlsearch<CR>
" This version will work across newlines:
:nnoremap <silent> gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<CR>

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>:nohlsearch<CR>

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>:nohlsearch<CR>

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

For ISO/IEC_8859-1 Latin-1 Supplement characters, substitute all \w instances with [0-9A-Za-zÀ-ÖØ-öø-ÿ_] and all \_W instances with \_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_].

Visual-mode swappingEdit

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 pluginsEdit

See alsoEdit


Swap all lines in a visual selected blockEdit

When working with arrays I sometimes realize that I have to do things in the opposite order. To easily change this I created this small function:

function SwapAll() range
  if a:firstline != a:lastline
    if a:firstline < a:lastline
      let first=a:firstline
      let last=a:lastline
      let first=a:lastline
      let last=a:firstline
    while first < last
      exec first.'m'.last.'|'.(last-1).'m'.(first-1)
      let first=first+1
      let last=last-1

vmap     <silent> <C-s>   :call SwapAll()<CR>
vnoremap <silent> <C-x>   :call SwapAll()<CR>

--December 4, 2013

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki