A couple of weeks back, we looked at how vim’s normal and insert modes let us navigate and edit files using nothing but our keyboard and a bit of persistence.
Today, we’ll be taking a step further to see how we can quickly modify or replace text, run commands, and install plugins that will make our day-to-day work as developers a little easier.
We’ll start with the visual mode, which lets us do a number of useful things, including of course, copying and pasting text from Stack Overflow.
Click & drag no more: the visual mode
There are three ways to go into the visual mode, depending on what you want to do with it.
To highlight a piece of text in a line, simply navigate to it using the shortcuts we’ve learned in our previous post and press v. You won’t see anything happen until you move your cursor, and again, you can do that using the same shortcuts from the normal mode, such as h, j, f<character> or b.
If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor.
Finally, you can select text in columns by pressing ctrl+v and moving up or down the block. This can come in handy when you want to change the prefix of multiple variables coming one after the other, for example.
Once you’re done highlighting your text, you can take action on it:
- Press d to delete the selection
- Press c to change it
- Press y to yank (copy) the text
- Press p to put (paste) it
- Press esc to go back to normal mode
There are other keystrokes you can use in visual mode, and you’ll often find yourself using them instinctively. This ability to reuse and combine shortcuts across multiple modes is what makes vim special in the first place.
Getting things done with the command mode
Pressing : will take you to command mode, where you can give commands to the editor and have it open and close files, find and replace text or delete duplicate lines, among other things.
Don’t worry if you see :'<,'> pop up in your command line after switching to command mode from visual mode — this is just vim telling you that you’re modifying a specific portion of the text.
- < marks the beginning of your selection
- > marks the end of your selection
You can use these to your advantage. For instance, if you want to save a part of the file you created without having to highlight it through the visual mode, you can simply run:
- :1,10write output.txt to save the first ten lines in your file to output.txt
After all, that’s what highlighting is all about — defining a range by marking a starting point and an endpoint.
Your ranged command can also include numbers or special characters such as:
- A dot character . which marks the line your cursor is pointing to
- A $ symbol which marks the last line in your file
- A % symbol which marks all lines (shorthand for 0,$)
Mix and match these to select the area of text you’d like to work on. For instance, you can type :.,+5write output.txt and vim will save the current line and the five lines below it to the file output.txt. Besides saving files, you can also:
- Edit new files by typing :edit
- Split a window by typing :split
- Quit vim and save a file by typing :wq
- Edit a file in a new tab by typing :tabedit
- Go back to the previous or next tab by typing :tabprevious or :tabnext
- Add a ! mark after a command to force an operation
There are hundreds of commands you can use in vim — type :help to get an overview of the basics or follow it up with an argument to get help with a specific command. For example, typing :help u will tell you how to use undo and redo commands.
Continue to explore and stay curious. Every time you add a new command to your repertoire, you’ll inch one step closer to mastering vim, whatever that looks like.
Making vim yours
As you get more comfortable with vim, you’ll want to make some changes to make it even easier to use.
Tweaking the settings vim loads at runtime requires you to create a config file where you can configure anything from custom commands and mappings to different color schemes.
To do that, type in vim ~/.vimrc in your terminal.
From here, we can configure the editor to, for example, show us line numbers whenever it starts.
We simply go into insert mode and type set number. Now, we need to save the changes so we hit the esc key and run the command :write to save our configuration.
You can set a host of things this way, including syntax highlighting, tab width, spell check and runtime paths for plugins that you install.
Plugins for C++ development
Let’s see how we can turn vim from an ordinary text editor to a powerful code editor with all the bells and whistles you could wish for.
Since we’re trying to set up a C++ workflow, we first need a way of building C++ projects, and for that purpose, we'll use a plugin for integrating with the cmake build system called cmake4vim.
This will allow us to select cmake variants, edit cmake variables through ccmake and even build and run unit tests from vim.
The best part? Cmake4vim is written in pure vimscript, which means anyone can modify or extend the plugin if they’re inclined to do so. There are no barriers between you and what the plugin is doing behind the scenes.
Next, we need a way to quickly identify errors, rename variables or jump to definitions about certain objects or methods in our file. YouCompleteMe will do great here, mainly because it allows you to use clangd as its backend (and because you complete it as much as it completes (for) you).
Finally, the ability to easily debug your code is also a must-have if you’re striving for an IDE-like experience with vim. We can use vimspector to set breakpoints, jump around stack frames, watch variables and do other things to figure out why the program is not behaving as expected.
With just these three plugins, we can quite comfortably develop C++ applications in vim. That’s a massive step up from the text editor we began our journey with.
For more awesome plugins you can use for your own project, check out this list.
And if there isn’t a plugin that fits your needs, you could always write your own! Vim’s extensive documentation and community support will help you every step of the way.
Thanks for sticking with us until the end. Remember that vim always offers more to learn and explore — take small steps every day and watch them compound into a fast and joyful programming experience.
Always looking for new ways to take your programming skills to the next level? Join us.