Today’s article is a result of my sympathies and love for Vim. Sympathies because not a single person on internet would leave Vim without mocking. Even exiting Vim is considered as tough as getting an interview with the company you like (hottest thing on my mind 😛 ). Jokes apart. Vim is the best editor you can work with on a Linux system. Here I present you the basic HowTos in Vim which is going to be enough if you want to use it everyday for writing codes.
- Want to insert something in a file?
Use the Insert mode. How?
Open up the file with vim:
Press ‘i’ or Insert key on your keyboard. (There are other ways too but this would suffice. 😉 )
You are ready to insert any characters now.
Vim even shows what mode you’re in (if configured like that), see below:
- Want to run a command?
Get to the command mode by pressing the Esc key on your keyboard.
Now, the screen looks like:
Press colon (:) and write the command.
You must have seen an rc file for different utilities that you use in your system. This file is used to rc or “run commands” before invoking. It contains the configuration settings for your utility. The .vimrc that you create after the installation contains all the commands which are run every time you try to open a file using Vim.
Now, the question is why do we need to have a .vimrc file when we can get into the command mode and run all the commands that we want to?
Because, the changes you make in .vimrc are going to be persistent, however, whatever you do in the command mode for a particular file is valid only for that instance. See the below example.Suppose you want to see the line numbers of the file you’re working with. What do you do? Get into the command mode by pressing Esc key, and then,
Now, the next time you try to open up the file using vim, result:
Conclusion: The command you used the last time showed results for that instance only. However, if you write the same command in your .vimrc. Every time you open up a file using vim, you’ll get the line numbers.
Now, you do understand what these rc files are for, right?
- Exiting vim
No, it is not that O RLY? tough. ( 😛 ) It is as simple as, getting into the command mode by pressing Esc key, then colon and “q”. YES. q for Quit. That’s it.
If you want to write file to the disk (Save the changes you made to the file), you do:
write file to the disk (Save) and quit:
quit without showing any warnings (Otherwise, Vim would show a warning if you’re trying to exit a modified file without saving it):
- What if you want to copy some text into a file without disturbing the alignment?
Answer: The paste mode.
This is for you copying codes. ( 😛 )
Note that this is going to convert tab characters to space characters, so if you’re making a patch for Linux kernel, you better be careful.
To get out of this mode, you do
Forget Ctrl+C, Vim lets you do it even more easily. (Did you notice, it rhymed? 😎 )
OK, kidding. May be not that easily but it isn’t tough either.
You need to
Get into visual mode. How?
Press Esc and then ‘v’. Make sure you are at the character where you want to start copying from before getting into this mode.
Then, you select (simply move the cursor using arrow keys) all the text you want to copy and press ‘y’ (Yank).
Get into the visual mode. Select the text and then press ‘x’.
Text cutted. (This was to make you frown, you Grammar freaks! 😉 )
After copying or selecting the text, without having changed any modes, go to the character where you want to paste, and press:
* ‘p’ to paste after the character you’re on.
* ‘P’ to paste before the character you’re on.
Go to the command mode and,
- Want to see the difference between two files?
$ vim -d _file1_ _file2_
That’s all you need for an easy work around with Vim. Sure there are many many things you can do with Vim and many commands that you can use but mostly for a simple use, you just need this 5 minutes guide.
Thanks for reading.