So where does Vim's power come from
The editor that I started out with was Notepad++. I was pretty happy with it, because all I needed at that time was syntax highlighting, and tabbed editing. Then at my first workplace, I saw a guy using Vim. It's kinda hard to describe the difference in words, but what I usually see when people edit text, is that they think about which line they have to edit, and move there. By move, I mean that they either click on the line with the mouse, or move the cursor line by line, character by character. In Vim, you don't see this, because the cursor just jumps around the screen, seemingly always landing at the right position. The reason for this, is that Vim has much more sophisticated methods for moving in text, than the cursor keys, or the mouse.
Let's see some examples on where Vim is more effective:
$greeting = sprintf('Welcome to the site %s!', $username);
Now, let's say we want to add the word "back", between Welcome, and to. You probably have to do the following:
If you are using the mouse:
- Move the mouse next to the e character in 'Welcome'
- Type ' back'
If you are using the keyboard:
- Press up/down until you are on the correct line
- Press right/ctrl-right until you are at the end of 'Welcome'
- Type ' back'
- 1G: Jump to the 1st line
- f': Put the cursor on the first ' character
- e: Put the cursor on the end of the current word that the cursor is currently on
- a: Start inserting characters at the right side of the cursor
- back: Insert the string ' back'
Another example, the same text, but now let's change the text to 'We are glad that you came back %s!':
1Gf'ci'We are glad that you came back %s!
- 1G: Jump to the 9th line
- f': Jump to the first ' character
- ci': Delete the text that is between the current '' characters, and start inserting
- We are glad that you came back %s!: Insert the string
write($string, $window, $color);
Let's say the parameter order is incorrect, let's switch the first and second parameters:
- 50G: Jump the 50th line
- f$: Put the cursor on the first $ character
- dW: Delete everything until the end of the current word, including punctuation and whitespace
- W: Jump to the end of the next word, including punctuation and whitespace
- P: Paste the previously deleted text
It might seem like the Vim way involves much more work, but it does not. Assuming you are a programmer, you should have a WPM (Words per minute) of 50-80 according to Wikipedia, at which you can type *1Gf$dWWP *in ~1 second. Mine is 100 WPM, after 5 years of working. If you cannot type that fast, you should probably work on that first, because that is like being a taxi driver without the ability to drive vehicles. It is going to be magnitudes faster than pressing up/down until you are on the correct line, and then pressing ctrl-left/right until you are next to the correct character. You can always tell the line number at a glance, because it's right there on the side, but you cannot easily tell how many lines you need to go up/down. If you use the mouse, you can click exactly on the correct position, but you will constantly have to move your hand from the keyboard to the mouse. In Vim you never have to do this, your hands can stay on the keyboard until you finished your work, because everything has a keyboard shortcut. The amount of time saved by this is tremendous.
Of course there are two assumptions that must be true, for you to be faster:
- You have to type well enough on a keyboard
- You have to know the commands/shortcuts in Vim. This will come in time, just start using the editor, and don't worry about it
After using it for a few months, you won't even notice how many little shortcuts you know, you will be using them from muscle memory, the same way that you can type "blind". If you think it's not worth the hassle, or you can't type fast enough, Vim is probably not for you. The other part of Vim's power comes from the plugins. As I mentioned on the my toolset page:
It can diff whole directories, show the color of RGB values in CSS files, act as a file browser, open files by patterns without knowing where they are, can match the closing pair of almost any kind of tag/brace/quote mark, or do syntax/spell checking. There is no file type, that it does not have a syntax definition for. These are all solved through plugins. Just look at the plugin repository at VimScripts, there will be definitely some things that you wish your current editor could do.
Brief explanation of the various modes
Vim is a modal editor. It means that every key does something different, depending on what mode you are in. The 4 modes that you will use the most, in order:
- Normal, activated by pressing
- Insert, activated by pressing one of a A i I o O
- Command, activated by pressing :
- Visual, activated by pressing v V or Ctrlv
Normal mode is what you start out in, this is when your cursor looks like . You cannot type in this mode, you have to press one of the mentioned keys to switch to insert mode. This is the mode where moving around is the easiest, for example, w will take you to the beginning of the next word, W will take you to the beginning of the next word, skipping whitespace and punctuation, e will jump to the end of the current word, ^ will jump to the beginning of the current line, $ will jump to the end of the current line, f' will go the first ' (or any other) character, and so on. The way of the VimFu is, that you only enter insert mode for "short bursts", after typing down what you needed, you return to command mode with
Insert mode is where the cursor looks like a |. This is the "notepad mode", where you can actually type something. Everything works as you would expect from any other text editor.
Command mode is activated by pressing : in normal mode, you will notice that cursor jumps to the bottom of the screen. You can enter a command here (duh), the most useful at the beginning will be q which quits the editor, q! which really quits (quit without saving), w which saves, and x, which saves and quits.
Visual allows you to select text, similar to using a mouse. The v key will activate the "normal" visual mode, you can select text with the cursor (or hjkl) keys, shift v activates visual line, which allows you to select whole lines, without actually going to the end/beginning of the line, and ctrl v, which is visual block, allowing you to select columns of text. After selecting text, you can press y to copy (yank) the text, and d to cut it. You can press p and P in normal mode to paste the previously copied text.
So how do I actually learn to use the editor
The first thing you do, is close Vim if you have already opened it, because by default it looks so ugly, like a child, that only a mother could love. My first recommendation is always, always do some preliminary configuration, then pick a font and color scheme that looks good to you. You will spend most of your work (life) looking at your text editor, might as well make it comfortable.
The config files
There are two files (there are more, but these two will be enough) that are read when Vim starts up, these are .vimrc, and .gvimrc, .gvimrc is only read when you are using gVim (the GUI version), .vimrc is read in both cases. Vim looks for these files under your home directory, but you can see where it is exactly by typing :version in vim, and looking at the "user vimrc" line. The absolute minimum for keeping your sanity, in my opinion (put these in your ~/.vimrc):
" Enables most of the features of modern vim set nocompatible " Enable filetype auto detection, and syntax hilightning filetype plugin indent on syntax on " Show line numbers set numbers " Highlight search hits set hlsearch set showmatch " Find words as we are typing set incsearch " Show multiple choices for tab-completion in a "navigable" menu format set wildmenu set wildmode=list:longest " Ignore case if we are searching for an only-lowercase string set ignorecase set smartcase " Everything is UTF-8 without BOM by default set encoding=utf-8 " A tab is 4 spaces wide set sw=4 set ts=4 " Depending on which religious group you are in, you may want to convert tabs to spaces with set expandtab
Font and color scheme
Pick a font. Look through Google web fonts, because they have a ton of free to use fonts . If you just want a tl;dr version, take a look at Hivelogic's top 10 list, they have pictures. I'm currently using Inconsolata, with a size of 10, and my other favorites are Monaco, Droid Sans Mono, Liberation Mono, and Anonymus Pro.
Installing the fonts depends on your operating system, I'll leave that to you. After installing, you can choose a font by typing :set guifont=*, don't forget to also play with the size. After you are satisfied, type :set guifont? to see the correct way to define it in your .vimrc (Paste that line to your .vimrc).
After that, pick a good color scheme. I love wombat, molokai, and neverland, but they are only for gVim. Still to this day, I couldn't find a better theme than the default for the text based vim. So, either do a search for "Vim colorschemes", or use the Color Sampler Pack from VimScripts (screenshots here). After downloading it, unzip it into your ~/.vim/ (that means in a folder called .vim, under your home directory, create it if it does not exist), and after that you can change color schemes with :color
Actually using the editor
Now that you a basic configuration, it's time to learn some VimFu. I recommend VimTutor, which is a "learning mode" for Vim. It won't take more than an hour, and it has a very good "try it as you read it" approach. On Windows look for something like gvimtutor.exe next to the gvim.exe, under Linux just launch gvimtutor. Don't freak out that it looks ugly again, vimtutor does not read your configuration files, that's okay.
Use it, use it, use it. Don't give up, it's hard at the beginning, yes.
You should probably look through the plugins, after you got the hang of it. No matter what area of programming you are working in, you will find a ton of plugins for it, and there a lot for general purposes. I will more about the plugins that I use, but in the meantime, you can browse through the popular plugins of vimscripts.org. Plugins should be extracted the same way as color schemes (~/.vim/).
I will also show some of my plugins in later posts.