DEV Community

Colin Bartlett
Colin Bartlett

Posted on • Edited on • Originally published at vimtricks.substack.com

2 2

VimTrick: Swap two characters

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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay