Vim: how do I swap two characters?

Vim

Vim Problem Overview


Is there a fast command to change

Cnotrol

to

Control

Vim Solutions


Solution 1 - Vim

While in normal mode, with your cursor on top of the first character to swap, you can type xp to delete one character and put it after the cursor, effectively swapping the two characters.

One possibly useful command (taken straight from the Vim page on swapping) would be

:nnoremap <silent> gc xph

to map gc (or another command of your choice) to swapping two characters. Note that the h simply moves the cursor back to its original position, on top of the first of the two characters to be swapped.

Solution 2 - Vim

xp

This swaps the current character with the next.

Solution 3 - Vim

You can also just use Xp with the cursor being on the second character. That way you stay on the same position without defining a new mapping.

Solution 4 - Vim

If you have some common missspellings you can also use the abbreviation feature to correct these as you type. For example, I often mistype 'String' as 'Stirng', the following command fixes these while typing:

inoreab <buffer> Stirng String

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvolniView Question on Stackoverflow
Solution 1 - VimMark RushakoffView Answer on Stackoverflow
Solution 2 - VimCTTView Answer on Stackoverflow
Solution 3 - Vimkongo2002View Answer on Stackoverflow
Solution 4 - VimJörn HorstmannView Answer on Stackoverflow