12

On bash's readline in emacs mode, I recently discovered the transpose functionality, giving me the opportunity to quickly fix a typo like

dc dir

to

cd dir

by pressing CTRL+T on the c character.

Is there something similar in Vi/Vim that lets me swap characters and words around?

1

2 Answers 2

15

For characters, it's fairly simple: xp to swap the letter under the cursor with the following letter, and Xp to swap the letter under the cursor with the previous letter.

The x command deletes the character under the cursor, leaving the cursor on the next character. The X command deletes the character just before the cursor, leaving the cursor on the same character it was on.

p puts (pastes) the last deleted or last yanked text just after the current cursor position. (P puts it just before the current cursor position, so xP and XP both leave the text the way it was before you started.)

For swapping words, I'm not sure; perhaps someone else can answer that. You can come close with dawwP (or remap it to be shorter) but that will break on several edge cases, for example near the end of a line.

1
2

Word swap mappings for Latin languages

The word swap mappings from the Vim wiki will not operate correctly on words with accented characters.

These mappings are adapted to work with (European) ISO/IEC_8859-1 Latin-1 Supplement characters. This is done by substituting all instances of \w with [0-9A-Za-zÀ-ÖØ-öø-ÿ_\-] and all instances of \_W with \_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-].

Clearing of search highlighting

Furthermore, the search highlighting is cleared where needed. This is achieved by adding :nohlsearch<return> at the end of each mapping when needed.

Here is the end result:

" Use gc to swap the current CHARACTER with the next, WITHOUT changing the cursor position.
nnoremap <silent> gc xph

" Use gw to swap the current WORD with the next, WITHOUT changing the cursor position.
nnoremap <silent> gw "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>

" Disable Alt+[menukey] menu keys (i.e. Alt+h for help)
set winaltkeys=no

" Use Alt + ← or Alt + h to swap the current WORD with the previous, keeping the cursor on the current word. This feels like "PUSHING" the word to the left.
nnoremap <silent> <A-Left> "_yiw?[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]\+\%#<CR>:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>
nnoremap <silent> <A-h>    "_yiw?[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]\+\%#<CR>:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>
" <A-h> corresponds to è

" Use Alt + → or Alt + l to swap the current WORD with the next, keeping the cursor on the current word. This feels like "PUSHING" the word to the right.
nnoremap <silent> <A-Right> "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o>/[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+<CR><c-l>:nohlsearch<return>
nnoremap <silent> <A-l>     "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o>/[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+<CR><c-l>:nohlsearch<return>
" <A-l> corresponds to ì

" Use g{ to swap the current PARAGRAPH with the next.
nnoremap g{ {dap}p{

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.