DEV Community 👩‍💻👨‍💻

Christoph Hermann
Christoph Hermann

Posted on

DRY Vim

I believe Vim's biggest strength is that it removes a lot of repetition in my workflow. This post is a short (probably incomplete) list of commands that allow you to repeat an action without having to repeat the whole sequence of keystrokes.

Editing

Key Description
. Repeats the last change you've made.
@@ Repeat last macro.
@: Repeat last command.

Searching

Key Description
; Repeat last f, F, t, or T motion.
, Same as ; but in the opposite direction.
cgn Apply last change to next search result.

Macros

Macros are a handy tool in your toolbox to avoid repetition.
It's crucial to practice, though, and memorize some patterns that make creating macros simpler and reusable.

Start at the beginning

To create reusable macros you need to make the changes as generic as possible. This means that you need to avoid starting the recording at a specific point in the code snippet. It's best to first move to the start of the line 0 or the start of the first word in the line ^.

Navigate to generic places

If you need to navigate in your macro it's best to avoid navigating using hjkl. Navigate to special characters that are common on the lines you'd like to change or navigate to the start or end of a line.

I wrote about macros a long time ago if you'd like to learn more techniques. https://medium.com/@schtoeffel/you-don-t-need-more-than-one-cursor-in-vim-2c44117d51db

Plugins

Plugins can mess up repeating changes with .. For example, changing the surrounding quotes with double quotes using vim-surround (cs'") isn't repeatable. There is a plugin though that fixes this for (at least) some plugins vim-repeat. You don't need to configure anything, just install it using your plugin manager of choice and you're good to go.

I hope you were able to learn a trick or two from this post to level-up your vim skills 🙂.

If you're curious to check out how my vim config looks like then head over to https://github.com/whimsical-vim/whimsical-vim.

Top comments (0)

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠