Vim has a steep learning curve, let’s not try to cover it. But once you start getting the nuances of it you start discovering that Vim is full of time-saving tricks. That’s what we’re going to cover in this article.
I’ve scoured the internet for Vim tricks. Some of them come from different websites, twitter posts and some of them are my own. Either way, some of these might be useful to improve your workflow in 2019.
But I’m just starting with Vim
No problem, all you really need to know to start using Vim can be compacted to a single sentence:
Open a file with ‘vim {filename}’. Enter ‘i’ to start adding text. Enter ‘ESC’ when you’re done adding text. Finally, enter ‘:wq’ to save and quit, ‘:w’ to save, or ‘:q!’ to quit without saving.
Some of the “tricks” I’ll present you are not really tricks, just simple shortcuts, and commands that are built in Vim. The thing is that some of these commands are so useful, that can be considered tricks all by themselves.
So, the first one I would like to present you is vimtutor. Vim comes with an interactive tutorial that you can use to learn the basics of the editor. This tutorial is full of useful information, like how to move, the vim modal way, searching and so much more. So, if you’re just starting Vim, this is the first trick I would like you to try.
Just type <ESC> followed by :Tutor from inside Vim to launch it.
I’m a intermediate/advanced Vim user
Some of these tips might be for you. I’ll sift through them without much explanation. The goal of these tips is for you to use them. If you want to really understand how they work, the help files are always there for you.
Moving inside a file
A lot of times, when inside a file we need to do things like jump to the end or the beginning of it. When dealing with large files, some of these tasks can be very tedious. Vim offers a bunch of shortcuts to move inside a file.
G will jump to the end of the file, and gg will jump to the beginning of it. If we want to move to a specific line we can use one of the following: :123<CR>, 123G or 123gg. All of these options will move the cursor to line 123.
On the same vein, $vim filename +123 will launch the file on Vim already on line 123.
Line navigation can also be tricky, so 0 will move the cursor to the beginning of the current line, $ to the end of the line.
H will move the cursor to the top of the current window and M to the middle of the current window.
Lastly, if you’re editing code, % will jump to the matching brace.
Note that all these commands apply to Normal mode. Of course, in Insert mode those are not commands, just characters being written.
There are a lot more commands for moving within a file, but with these, we should be able to perform most of our daily tasks much faster.
Courtesy of Unsplash
While very useful, the tips above are bland. They are just regular commands disguised as tips. What about more complicated things?
- Recovering informationSometimes we need to close what we’re doing and have no time to create a session to store things. Vim offers the ability to open the last edited file on the machine with the cursor on the last known position. For that, we start Vim and hit CTRL + o + o.
- Shell commandsAnother thing that might be really useful is the ability to output in Vim the results of terminal commands. Vim offers the ability to run shell commands inside of it. For that, we use the ! command. For example, to output the result of running ls we can use :r !ls, :r !cal to output the current month calendar or :r !date for the current date and time.
- HistoryVim offers the history of the last commands ran. Sometimes we need to re-run some of those commands, but the :history command is for consultation only. To re-run those commands we can use the interactive command history window. to launch it we can use q:. Then is a matter of choosing the command to run and executing it with <CR>.
- Compressed filesAnother thing that can come handy is the ability to open, edit and save compressed files without having to extract it. this can be achieved by opening the compressed file. For example, $vim archive.tar.gz. This will launch the explorer. We can then open, edit and save those files without having to extract them.
- Don’t know what a command does?Hitting K in Normal mode will launch the manpage for the keyword under the cursor. Also, hitting * will search for all occurrences of the word under the cursor.
- Indentationgg=G will indent our entire file according to the definition of tabs we’re using. To take it a step further, we can setup marks to indent an entire file and keep the cursor where it is, for that we can use mmgg=G’m. It looks complicated but it works. I found out the best way to use this command is to remap it instead of trying to memorize it. It’s just too much.
- GreppingWe saw earlier that we can run shell commands from inside Vim. In the case of grep, Vim offers its own version of it, Vimgrep.Vimgrep is nothing more than an alias for the shell version of it, but it’s easier to memorize than ‘:!grep …’.For example, to search for all occurrences of ‘error’ in all .txt files we can use :vimgrep error *.txt.For more information on Vimgrep use :h vimgrep.
- Checking different git branch contents using VimA lot of times, when using git, we need to check the content of different branches. Using some bass-fu and Vim we can see the contents of a given file in a different branch without having to change into it.
git show branch_name:/path/to/file.txt | vim -
Basically, we are pipping the results of showing the file in a different branch into Vim.
These are some of the tricks that I found and use myself. As you can see, with some knowledge of the inner workings of Vim it is possible to do very cool things that can save us a lot of time.
What about you? What Vim tricks for 2019 do you have to share with us?
Want to learn more about Vim? Want to learn how to use it as an IDE? Check out my new book An IDE Called Vim. It has everything from basic Vim usage to file finding, auto-completion, file manager and more.