It's been 3 years since I started using Vim as my default text/code editor. Yes I am a Homo Sapien with an IQ less than 140. I have to say, Vim has become part of my life and I have never looked back. Let me cover more about it in this post and why if you use Linux, you should probably use Vim too (now, read the rest of the post before calling me an opinionated potato-head).
(Note to all the PhDs from the MIT reading this post: I'm trying to keep the jargon and details to the minimum so, this probably isn't the right post to argue that you know more Vim script than I do :) )
First things first, upgrade your Vi to Vim
Vim has been around since the 70s though called by a different name Vi. Vi is boring, and is in its old-age.
Vi is like the Ford Corina whose development stopped years ago. No one uses Vi anymore. So get rid of it if you are still using Vi and install Vim. Vim has evolved over the years but the whole idea of it is still the same.
What makes Vim so different
It is because Vim uses Modes. Modes are like different states of Vim. You can type in code in one mode, navigate inside the file efficiently in another mode, copy text in another and so on. Vim can be switched to different modes. Let me go through some of the basic modes here:
1. Insert mode
This is the mode where you actually type in code. This is the only mode that is available there in all other text editors like Notepad, Netbeans, etc. You enter this mode by pressing
i and get out of this mode by pressing
2. Normal mode
This is the default mode. You cannot type code in this mode. This is like the mood that your girlfriend gets into when she's upset for who knows what! Any key you type in here is gonna do something weird, unless you know what you are doing. For example, if you type press
i (which stand for append and insert respectively), you are going to get into the insert mode. If you type
v, you get into the visual mode. If for any reason you want to get back to the normal mode, hit
ESC (well, if hitting it once doesn't work, twice should).
3. Visual mode
When you are in the visual mode, you could select text, just like how you select a piece of text in any other text editor. You use this to copy a piece of text and paste it somewhere else.
4. Command-line mode
Do you know that you can execute shell commands from within Vim? Well, if you have a lazy ass like I do, you should be glad.
There are other modes too, but the above ones should get you started.
The 80-20 rule
The two common modes you are gonna be in a lot of time are the Insert and the Normal mode. And in that, you are gonna spend 80% of the time in the Normal mode. This is the time that you are gonna just sit infront of your monitor thinking about what you are gonna type next. I don't know anyone who types in code non-stop like in the movies. Only 20% of the time is anyone going to spend his/her time actually typing code in the editor.
The point is, 80% of the time, you are gonna spend it in the normal mode navigating inside your file between words and lines and Vim is gonna help you do that fast.
Speaking to Vim
Vim has it's own language - it's not a joke. The Vim language has its own set of verbs and objects. It's like saying, delete this line, delete this word, move to the last line, etc. For example, the command
daw deletes a word;
dd deletes the current line;
yi( yanks (copies) inside parenthesis. There is a lot of verbs and lot of objects or motion that you can use together to do what you want. Just like constructing your own sentence in English, you can construct your own command in Vim, by combining verbs and objects. Now, don't worry much if all that confuses you, you are going to become a parseltougue in no time, even without you knowing it.
To know more about the Vim language, have a look at this article by Ben McCormick : Vim as a language.
The look and feel of Vim can be customized, just like any other open source software. You can change the syntax colorscheme of your code, you can add or remove line numbers to your file, you can change the background, font-color and font-family of the code you edit. The default configuration file where you can set all these options is
~/.vimrc. Better than that, put that file inside
~/.vim/ directory. Have a read at this article on constructing a Vim configuration file.
Vim has a lot of plugins. A plugin is some kind of an add-on that you can add to Vim. There are plugins to navigate files inside Vim, code completion and what not!
Some of the useful Vim plugins based on my experience include:
- ctrlp.vim - For navigating between files inside vim
- vim-airline - For awesome statusbar/tagline
- vim-snippets - For using code snippets for autocompletion.
- commentary - For toggling comments.
Visit Vim Awesome for a list of all the Vim plugins available.
Vim vs emacs
A lot of people argue over the internet trying to justify if Emacs is better than Vim or vice-versa. I must say that I've never tried emacs, not even once, in my entire life. Vim is good and I'm good with Vim. If you go ahead now and start using Vim, you pretty much should lead a happy life for the rest of your days.
I am only 17 years old, can I use Vim?
Legally, yes. Vim (originally written by Bram Moolenaar) was forked by a group of people into a new project called Neovim and they claim it to be the Next generation Vim. I myself am using Neovim. If you are using Vim currently, there is no reason not to switch to Neovim. Neovim has some cool features like allowing Vim to be used kinda like the back-end while using some modern code editors like Atom.
I guess this gave you a basic introduction to Vim and why you can/should use it. If I haven't included anything essential in this article, please go ahead and let me know about it in the comments. Thank you for reading.