VimTrick: Swap two characters
Colin Bartlett

Colin Bartlett @cbartlett

About: I help IT teams monitor the cloud with StatusGator. Vim user and author at VimTricks.com

Location:
New York, NY, USA
Joined:
Apr 25, 2020

VimTrick: Swap two characters

Publish Date: May 7 '20
8 0

Here’s a quick trick that requires no plugins. You probably already know both of these commands but many people, including myself, might not have internalized the combination. A little reminder is sometimes all we need.

Use xp to swap the character under your cursor with the one to its right.

For example, suppose you have a misspelled word like so:

puts "Hlelo world!"
Enter fullscreen mode Exit fullscreen mode

With your cursor on the misplaced l in Hlelo, type xp:

puts "H█elo world!"
Enter fullscreen mode Exit fullscreen mode

The x deletes the character under the cursor and puts it in a register and p pastes that register to the right of the cursor. You end up with:

puts "Hello world!"
Enter fullscreen mode Exit fullscreen mode

There's a quick screencast gif over on my VimTricks post for this.

I'm publishing new Vim tricks and tips every Monday and Thursday as an exercise to help me learn Vim. Even just writing up tips like this has had a tremendous impact on my productivity.

Comments 0 total

    Add comment