Last Updated: February 25, 2016
·
1.419K
· caioariede

Vim: Swapping characters to quickly fix typos

Given you have a typo at line 26 in your code:

26. from myapp.modesl import MyModel
                   ^

There are two ways you can fix it without entering insert mode:

The first way

:26 normal fsxp

The second way

/modesl
fsxp

fs - move the cursor to the first ocorrence of "s" to the right
x - delete and copy the character under the cursor
p - paste the character

7 Responses
Add your response

In order to make the shortcut more second-nature, try thinking of xp standing for 'transpose'.

over 1 year ago ·

The third way

:26s/modesl/models/

On line 26 substitute modesl for models.

over 1 year ago ·

If that happens to you frequently, you can (ab)use vim's text expand feature. In your .vimrc:

iab modesl models

and from now on it should automagically correct that.

iab is shortform for iabbrev. There is also autocorrect.vim which aims to build a database full of such things (teh -> the etc.)

over 1 year ago ·

Thanks
@originell that abbrev feature is awesome

over 1 year ago ·

@design48 glad you like it. I should probably make a protip out of that one :D

over 1 year ago ·

@originell Awesome :)

over 1 year ago ·