Bennett Hardwick

8 Vim tips and tricks for advanced beginners

January 06, 2019 - vim neovim beginner

A few weeks ago, I saw this post by Robert Hearton which outlined a few programming projects for advanced-beginners. It talked about the idea of the “valley of despair”, and offered up some ways that might help you get out of it. This got me thinking about my experience with Vim.

It occurred to me that now that I’m using Vim (or the Vim input method) for basically every text editing task with little frustration, I’m on the upward hill out of this valley of despair. So I’d like to share some tips and tricks that just might help you do the same.

0. Become an “Advanced Beginner”

This article is predominantly targeted towards people who’ve been using Vim for a little while. So I’m going to assume that you know about the different modes, you can move around and you can use a few ex commands (:q).

I’m also going to assume that you’ve somewhat configured Vim to your liking (at least set number relativenumber), and you’re comfortable with how everything works.

The first thing you try and do in a text editor, after learning the basics, is search. In Vim, searching, substituting and the global command are some of the most powerful commands you can use.

2. Text objects

Text objects were probably my most favorite thing about Vim when I found out about them - mainly because they were so different to what I was used to, but so convenient! Text objects can are regions of text that can be manipulated as if they were selected using visual mode. The most commonly used text object is w, or “word”. When you first begin using Vim, you’re taught to move around by words. You’re taught to change words (cw), yank words (yw) and delete words (dw). To get an idea of all the text objects that exist, use the :help text-objects command from inside Vim. Here’s just a few that I often use:

3. Motion

Learning how to use hjkl is not the end of movement in Vim, there’s a number of ways to get around.

4. Markers

Markers or marks are another part of Vim that can really speed up your work-flow. Being able to edit multiple parts of multiple files can be really useful.

5. Macros

Whenever I repeat anything in Vim, a little voice in the back of my head scolds me - “You should have used a macro!” At first, it often seems counter productive to create a macro, many believe that macros are often only used for complex tasks.

The better you get at Vim, however, the more you begin to realise that simple tasks are great for macros. Here’s a few tips for making macros work more often.

6. Visual block

Sublime Text and Visual Studio Code both have ways of inserting on multiple lines - through the use of “multiple cursors”. Vim has a similar way of inserting on multiple lines, known as Visual Block mode. Visual Block mode is closely related to Visual mode, with the exception that your selection need not wrap entire lines.

Using this, you can easily comment out blocks of code, or append text to lines.

comment and append

Apart from that, you can can also cut blocks, delete blocks and do a whole lot more. Check :help visual-block for more info.

7. Finding help

The best way to learn new things about Vim, is to spend some time reading through the :help file. To get to this, write :help from normal mode, it will open up a new buffer populated by the help file. I’d really recommend spending some time to get familiar with the details of Vim features - it’s actually a pretty interesting read, and you’ll learn a lot. To better understand how the help file works and how to search better, look at :help help-summary.

Another great part of the :help files, is the Vim Reference Manual, located at :help quickref.txt. If you don’t have time to read the entire help file, you could at least skim through this.

Finally, if you’re not already playing VimGolf , you definitely should be! It’s a super fun game that requires you transform a piece of text using the least number of commands possible. You’ll be super surprised about how easily people can achieve some complex transformations.

If after all this, you’re still craving some more Vim tips. I’d suggest reading the book Practical Vim - Edit Text at the Speed of Thought by Drew Neil.

8. Some cute tricks

What next

At this point you probably know quite a lot about Vim. You’ve read an understood the help file, you’re a VimGolf pro and you’re using Vim for pretty much everything. If you haven’t got any plugins installed, this might be the time to start. While many people default to using plugins, an experienced Vim user will realise they’re barely necessary.

Unfortunately, for advanced IDE features, you’re going to need some kind of linting and completion support. To help you understand what these integrations can do, have a look at :help quickfix.

Thanks for reading!