VimTricks

Share this post

VimTrick: Swap two characters

vimtricks.substack.com

VimTrick: Swap two characters

Easily clean up typos by swapping two misplaced characters

Colin Bartlett
May 7, 2020
4
Share this post

VimTrick: Swap two characters

vimtricks.substack.com
3

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!"

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

puts "H█elo world!"

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!"

Just internalize xp for swapping characters and stop going in and out of insert mode to make small edits.


Was this useful? Help us improve!

With your feedback, we can improve VimTricks. Click a link to vote:

  • 👍Great trick, very useful. Thanks!

  • 👌I already knew this, but still helpful.

  • 👎Not interesting to me.

4
Share this post

VimTrick: Swap two characters

vimtricks.substack.com
3
Share
3 Comments
Share this discussion

VimTrick: Swap two characters

vimtricks.substack.com
bobbyquine_
May 7, 2020Liked by Colin Bartlett

Nice one, it's kinda like internalizing `ddp` for swapping 2 lines! To add on, your terminal/shell usually uses Emacs-style bindings, and invoking Ctrl-t will swap the characters on the left of the cursor with the right. For macOS users, Emacs-style bindings is OS-wide, so you can use Ctrl-t anywhere!

Expand full comment
Reply
Share
1 reply by Colin Bartlett
Andrei Glingeanu
May 14, 2020

Love this!

Expand full comment
Reply
Share
1 more comment...
Top
New
Community

No posts

Ready for more?

© 2024 Nimble Industries, Inc.
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing

Our use of cookies

We use necessary cookies to make our site work. We also set performance and functionality cookies that help us make improvements by measuring traffic on our site. For more detailed information about the cookies we use, please see our privacy policy. ✖