8 Vim Tips And Tricks That Will Make You A Pro User
Brief: In this article, I’ll show you some of my favorites Vim tricks with practical examples. If you don’t use Vim, these tips might not give reasons for using Vim but if you use it already, you’ll definitely become a better Vim user.
Yes, comfortable. A very strange word when talking about Vi or any of its clone isn’t it? And I agree, it is certainly not the most intuitive text editor.
But with some practice and by training a little bit your memory, you can perform apparently complex editing tasks in only a few keystrokes. Something that I’ve never really found with any other command line text editors for Linux.
But, instead of writing yet-an-other-vi-introduction, I chose today to let you touch the real power behind Vi(m) by presenting you few tricks I really use — and I miss in most if not all other editors. I will not give you the complete explanation of all those tricks, but I strongly encourage you to try and experiment with them until you get some grasp on how they work.
If you liked Vim and want to master it completely, you can also enroll into online Vim course provided by Linux Training Academy.
8 Vim tricks to get more out of it
Let me repeat: I strongly encourage you to try those example by yourself. Unfortunately, WordPress does not necessary do a good job to preserve the exact content of my examples & commands — especially regarding empty lines or quotes. So, for your convenience, you can download the samples used here from the link below:
Each example comes with the original text (.orig) and a Bash script (.sh) invoking Vim with the command illustrated in this article.
1. Changing capitalization in Vim
Am I the only one using that feature? Or is this because I’m doomed working with people believing that WRITING ALL CAPS IS COOL?
Anyway, when programming, when adding a copyright notice, or even after a copy-paste, it is not that uncommon to have to change text capitalization. In that respect, Vim is much more evolved than the original Vi. And I bless every day Bram Moolenaar for his great work:
2. Vim search and replace tips
This one I use every day. Why is this still not present in all text editor? Of course, most editors have some kind of search replace feature. But how many do really have the power of regex and substitution patterns? Of course, this is more complex to master than basic string substitutions. But I just couldn’t live without that feature. At such point, I sometimes see myself copying/pasting from some GUI editors into a terminal running sed and back. But Vi has that embedded since 40 years or so…
3. Moving things around in no time
Yes, copy-paste and drag-n-drop are great tools. But sometimes it is just too boring to scroll through an entire document to find where to paste my text, and then scroll in the opposite direction to bring the cursor back to its initial location.
4. Applying commands on an address range
GUI editors may allow you to apply some commands on the entire file, or only on the current selection. Sometimes, we have more options, like before or after the cursor. But Vi(m) allows a much more expressive description of the range on which a given command has to be applied:
5. Vim piping commands examples
This one is absolutely a meta-trick in the direct spirit of Unix-philosophy. Vi allows you to process part of your buffer through an external command. This is great when you need things that are not doable directly in Vi — or for things that are not easily doable in Vi. My favorite use case for this feature is to sort data — but it has virtually limitless power:
6. Typing less
When writing formal documentation, there is always some unusually long and complicated terms you have to repeat. It can be a brand or product name. Some location. A copyright notice. And so on. Obviously, each instance of those big words must be spelled correctly and using the exact same capitalization and punctuation. The abbreviation feature of Vim is a must there.
7. Getting help in Vim
Ok, there is Intenet. But people still using the man like me will appreciate the inline help. You can obtain help by topic. Or by command. Always useful when you don’t remember the exact syntax or options for Vim commands — or if you’re unsure the command you need is a normal command or an ex: command.
8. Scripting in Vim
When using Vi(m) you’re basically using a visual front end to another underlying editor called ex. Maybe did you noticed how many commands in the examples above starts by a colon (:)? That’s because those are ex commands. And this is yet another advantage of Vi(m) over so many text editors: not only you can use it interactively — but you can also script it.
Why would someone want to do that? Speaking for myself, I find that a great way to automate some text processing. Beside, you can see a typical example straight from my hard drive.
There is probably some mysterious commands in that ex script, but I can tell you this will remove any (eventually present) header from a script and will replace it by another one read from the NEW.HEADER file — adding a # before each added line. There is no doubt I could I’ve done that using other tools than ex. Indeed, that was even the subject of one of our previous Bash Challenge. But ex is definitely an option.
As I said it initially, this article was absolutely not a tutorial, nor an introduction to Vi(m). Just some Vim tips to show you why someone could still like that editor despite all other modern code editors for Linux available today. In some sense, I shared with you some of my favorite spells. But in the great tradition of wizardry, I didn’t unveil how exactly they work.
So, dear apprentices, don’t hesitate to use the comment section below to share your own incantations or countercharms — or if you dare, to explain those tricks!