Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: how do I swap two characters?

Tags:

vim

Is there a fast command to change

Cnotrol 

to

Control 
like image 520
volni Avatar asked Oct 07 '09 04:10

volni


People also ask

How to swap text Vim?

Visual-mode swapping 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.

How do I change one letter in Vim?

You can also make changes to single characters in normal mode. For example, to replace a single character, move your cursor over it and press r , and then the character you want to replace it with.

How do I go to a specific character in Vim?

You can type f<character> to put the cursor on the next character and F<character> for the previous one. You can also use ; to repeat the operation and use , to repeat it in opposite direction.


2 Answers

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.

like image 71
Mark Rushakoff Avatar answered Sep 21 '22 08:09

Mark Rushakoff


xp

This swaps the current character with the next.

like image 29
CTT Avatar answered Sep 19 '22 08:09

CTT