How do I swap two regions of text without using registers?
Answer
cx{motion} then cx{motion}
Explanation
vim-exchange (by Tom McDonald) lets you swap two text regions in two steps — no yank register manipulation, no cut-paste juggling. Press cx{motion} on the first region to mark it, then cx{motion} on the second region to complete the swap. The two regions exchange positions.
How it works
cx{motion}— mark the first region (highlighted)cx{motion}— mark the second region → both regions instantly swapcxx— shorthand forcx{line}(exchange the current line)Xin Visual mode — exchange the visual selectioncxc— cancel a pending exchange
Install with: Plug 'tommcdo/vim-exchange'
Examples
Swap two words on a line:
foo bar → bar foo
cxiwonfoo(marked)cxiwonbar→ swapped
Swap two lines:
cxx " mark line 1
cxx " mark line 2 → lines swap
Swap two arguments in a function call:
doSomething(alpha, beta) → doSomething(beta, alpha)
cxi(... (navigate) ... no, usecxi,if you have targets.vim- Or:
cxiwonalpha, thencxiwonbeta
Tips
- Works with any motion or text object:
cxiw,cxi",cx3j,cxip, etc. - The visual
Xis the fastest for irregular selections - Unlike yank-delete-paste, vim-exchange preserves surrounding whitespace cleanly
- No register is used — your yank register stays intact throughout