Vim is huge. Trust me. It’s huge. You think 12 different modes for editing and other tasks in a text editor to be a joke? Who does? Those who don’t know the power of it. This article should be a proper explanation for why Vim is such a huge thing.
In Vim, you can use a single letter in different ways in different modes to do different things. For instance, you can use pair w with d to delete a word, or pair a g with d to navigate to a function’s declaration. I know. Some of you are wondering, what’s so cool about it? You’ll know once you see for yourself. But that is for another day.
In the meantime, we’ll see pretty much all the possible commands we can use with the letter “w” in Vim.
Motion commands with “w”
Vim has a wide array of motions, to help you jump and navigate within words and everything quite flawlessly. One of the most common of all the letters that are assigned for Vim motions is the letter “w“. When you press w in Vim Normal Mode, your cursor will jump to the first letter of the next word that is in front.
This allows you to save a lot more time and a lot more keystrokes as compared to using the conventional h and l or the left and right arrow keys for navigating within a column. However, if the next word you’re navigating to has any sort of punctuation, and you want to skip the punctuation and land directly on the first letter of the next word, you can use a capital W instead.
Moreover, if you want to navigate to the first letter of the previous word instead of the next one, you can use the letter b to do so. There are more motion commands based around this, and you can also pair other letters to make cooler combinations of navigation with professional text manipulation inside of Vim. But this should pretty much be enough for the time being!
Saving files in Vim with “:w”
Quitting vim is as tough as it could get, if you’re not used to it. Beginners have always had trouble with it, and there are countless memes based around the unconventional way of quitting Vim. But trust me, once you get used to it, it doesn’t feel weird at all, and will come to you by nature. If you use Vim for some time, you’ll constantly try to use the same way of quitting in other programs too!
Enough of that fuss. Now let’s get down to business.
Vim has several ways of quitting. Now don’t ask me to list them all. But I’ll probably at least tell you a few common ways of quitting Vim other than using :q or :q!. You know what? You’ll never actually be able to save files if you only happen to use :q and :q! when quitting out of Vim. And best of luck trying to save your file using Ctrl + s. Because Vim does not save files. It writes files. Well it’s essentially the same thing, but I suggest you get used to this way of referring to saving files. As Vim will save a file when you type the following in Vim’s Normal Mode:
or simply just:
Ya following me? Good. This will write all the changes you make to your current file. It’s the same as using Ctrl + s in other editors. But as always, Vim has a tendency to do things in its own ways, for the sake of a good amount of reasons.
It is not, however, the only way of saving a file. If you wish that you want to save a file and quit Vim at the same time, you don’t need to first write it and then quit separately you can do both together, by using:
But there’s one thing you should keep in mind. If the file you are editing in a new one, and does not have a name yet, you’ll get a red, error message that says “no file name”. In order to assign a name while you’re saving a file, you can use this syntax instead:
And the same goes for the previous :w one as well. If you’re editing a new file and wish to save it, you need to specify a filename for it first before you can.
Nevertheless, the whole story does not end here. Sometimes, you might face files which you may wish to edit having a read-only tag, where this save and quit won’t be applicable. If the user you’re logged into owns the file, but the file is read-only then you can use the following command to force-write the file:
Also, make sure to note that no files in the root directory (which are owned by the root user) can be edited this way unless you’ve explicitly been given the superuser privileges.
Deleting words in Vim Insert Mode
Did you know that in Vim Insert Mode, you can delete a whole word directly without having to come back and forth between Normal Mode and Insert Mode? You may not have known so. If you press Ctrl + w in Insert Mode to delete the word behind the cursor position.
This way you won’t need to switch back to the Normal Mode every time if all you want to do is delete a word. Quite simple yet very useful, isn’t it?
Vim is well known for its efficiency, and this greatness is achieved by the different modes Vim uses. Having different modes for different tasks, Vim is able to utilize the same letters that are specific to that Mode. This way, it is able to pump in loads of functionalities in a single character, which in our case was the letter w. There’s more for you to discover as you dive deeper and deeper into mastering Vim. Which is why, it is safe to say that Vim is like an “Endless Vortex of Text Manipulation”.