7 Vim Tips That Changed My Life (With Demo)

Vim code editor

As a full-stack developer, I‘m always looking for ways to speed up my coding workflow. One of the most powerful tools in my efficiency arsenal is the Vim code editor.

Now, you may have heard of Vim before or even played around with it a bit. There are plenty of great resources out there that cover the basics of navigating and using Vim.

But in this article, I want to go a step further. I‘m going to share 7 advanced Vim tips that, once I learned them, completely revolutionized my programming process. These go beyond the beginner tutorials and really showcase the power and flexibility of Vim.

Now I‘m not saying you need to ditch your current IDE and go all-in on Vim tomorrow. Start by progressively incorporating these tips into your workflow. Over time you‘ll find yourself flying through code faster than ever before.

With that, let‘s dive in to the 7 Vim tips that changed my life!

1. Jumping Between Split Windows

Jumping between Vim split windows

Vim allows you to split your editor into multiple windows, which is great for referring to code side-by-side. But navigating between those splits can be cumbersome, requiring multiple keystrokes.

Luckily, there‘s a faster way:

RECIPE: Ctrl+w [direction key]

Ctrl+w, followed by one of h/j/k/l (the direction keys in Vim) will quickly jump your cursor to the split in that direction. So Ctrl+w j moves down a split, Ctrl+w l moves right, etc.

This one tip has saved me countless keystrokes and really streamlined working with multiple files in Vim.

2. Recording and Playing Macros

Recording macros in Vim

Vim macros allow you to record a series of commands and play them back. This is incredibly powerful for automating repetitive editing tasks.

To record a macro, press q followed by a letter to assign it to (like qa). Perform your commands, then press q again to stop recording. To play the macro back, type @a (or whatever letter you used).

For example, say I have a list of items I want to convert to an HTML unordered list. I move to the first line, press qa to start recording. I type I

  • Esc$
  • Enter to surround the line with li tags. Then I press j to move down and q to stop recording.

    Now I have the @a macro to replay those steps! I can move through my list and just tap @a to apply the HTML. Or I can do a numbered replay like 50@a to apply it to the next 50 lines all at once.

    Macros take a bit of practice, but are incredibly powerful for flying through repetitive code changes.

    3. Commenting Lines

    Commenting lines in Vim

    Commenting and uncommenting lines of code is a frequent task when programming. Vim doesn‘t have a built-in way to do this, but with a simple mapping you can toggle comments on the current line or selection:

    nnoremap :s/^/\/\/:noh
    vnoremap :s/^/\/\/:nohgv

    I map this to Ctrl+/ but you can use whatever you prefer. In normal mode, this will toggle // at the start of the line to comment/uncomment it. In visual mode, it will do the same for your selection.

    With this handy mapping, commenting is a breeze. No more manually adding and removing // across multiple lines!

    4. Visual Block Mode

    Visual block mode in Vim

    Vim‘s visual block mode is a powerful way to perform column editing. Ctrl+v enters block mode, allowing you to make a rectangular selection. You can then edit each line in that block simultaneously.

    For example, say I have:

    const one = 1;
    const two = 2;
    const three = 3;

    I want to edit these to be uppercase and have a prefix. I can Ctrl+v to select the "one", "two", "three" block, then c (change), type MYCONST_, and Esc. This changes all lines at once, resulting in:

    const MYCONST_one = 1;
    const MYCONST_two = 2;
    const MYCONST_three = 3;

    I can continue blocking the numbers, using A to append, and add _POSTFIX to the end of each. Visual block mode makes previously tedious alignment and refactoring tasks trivial.

    5. Navigating the Undo Tree

    Undo tree in Vim

    Vim maintains a tree of every change you make in a file. u undoes a change and Ctrl+r redoes. But you can also navigate through the branching undo history:

    Earlier changes: g-
    Later changes: g+
    Older undo branches: 2g- (or 3g-, etc)
    Newer undo branches: 2g+ (or 3g+, etc)

    So if you undo with u, make some different changes, then want to go back to the undone version, 2g- will jump to that older branch in the undo tree. It‘s like a time machine for your code!

    With some practice, you can quickly navigate to any previous state of your file. This is a life saver when you need to revert or reference older versions.

    6. Swapping Text

    Swapping text in Vim

    Vim makes swapping text easy with a few key commands:

    Swap characters: xp
    Swap words: dwwP
    Swap lines: ddp

    x deletes the character under the cursor and p pastes it after. So xp swaps the character under the cursor with the next one.

    dwwP deletes the word under the cursor, moves forward a word, and pastes, effectively swapping the two words.

    ddp does the same for full lines, deleting the current and pasting it after the next to swap them.

    No more fiddling with cut, copy, paste, and carefully placing the cursor. These Vim text swaps make rearranging code quick and painless.

    7. Using Marks

    Using marks in Vim

    Vim marks allow you to set a bookmark in your code and quickly jump back to it later. This is useful for navigating long files or remembering important locations.

    To set a mark, use m followed by a letter, like ma. Then you can jump to that mark from anywhere with ‘a (single quote).

    You can even set marks that persist between Vim sessions! Use capital letters for these, like mA. Even if you quit Vim and reopen the file later, ‘A will still take you to that location.

    I like to set marks for key functions or sections of a file I frequently need to reference. A few quick keystrokes later and I‘m right where I need to be. No more furiously scrolling or searching!

    Putting it All Together

    Incorporating even a few of these tips into your workflow can make a huge difference in your productivity with Vim. You don‘t have to memorize all of them at once – start with one or two and practice until they feel natural.

    That‘s the beauty of Vim. You can learn it incrementally, mastering a few commands at a time. Each new tip or trick you pick up makes you that much faster and more efficient.

    So give these tips a try and see how they revolutionize your coding life with Vim. Trust me, you‘ll never look at code editing the same way again!

    Similar Posts