What beginners should know about vim

Thu 16 May 2013
By Stephen Cripps

In Programming

tags: programmingtextvim

Subscribe: RSS

Flattr this

Vim is a great editor, I've been using it for several years, and I don't think there will ever be any going back for me. The commands and "shortcuts" are so engrained in how I expect to type that I shrug off any tool that isn't vim like. Sometimes to the point of being just plain irrational. Either way, I think everyone should at least try to join the cult of vim at one point or another.

So when you open vim for the first time, you might be presented with a welcome screen, although you may not even get that (I didn't know about vimtutor for the longest time because of it). Hopefully someone has already told you about mode oriented editing, how you mostly want to stay in "normal" mode, and only use "insert" mode when you're entering content.

Basic functionality

When you're in insert mode and you want to backspace past the beginning of the line, you will find that you just can't. The cursor stops and you need to exit and get into normal mode to delete the line and move the cursor up. I remember feeling panicked, even the most basic function I would expect from any application is not present, what kind of horrible alien monster is this?! It wasn't until I started looking up how to customise vim that I learned that this was just the default setting of many.

: set backspace=indent,eol,start

The above is a command that is entered in vim's command line, which you access by typing ":" in normal mode. The following is an excerpt from my ".vimrc" file. After entering the command, the backspace will do exactly what you would expect, when you presse backspace, it deletes something! (in insert mode). The following are some more settings that make you're vim feel more natural.

set nocompatible        " Turn off vi compatibility mode
set spell                       " Turn on spell checking
set backspace=indent,eol,start  " Allow backspace in all circumstance
set history=1000        " Allow undo, remember last command with up
set mouse=a             " Allow selection with the mouse in all mode

set ruler               " Text in status bar shows cursor location
set incsearch           " As you type in search pattern, highlight it
set number              " Line numbers on left-hand side
set showcmd             " Update status line when selecting text

set tabstop=4           " Number of spaces a tab represents
set shiftwidth=4        " Number of spaces shifted with a shift >>
set expandtab           " Use spaces rather than hard tab characters
set softtabstop=4       " Tab spaces in no hard tab mode
set autoindent          " When you press enter, vim puts cursor in a
                        " good place

set scrolloff=5         " When cursor is 5 lines from bottom, scroll
set ignorecase          " Ignore case in searches

set nohid               " Don't let user hide text buffer if unusaved
set encoding=utf-8      " Default UTF-8 text encoding, you want this
set fileencoding=utf-8  " unless you're using Notepad

set colorcolumn=74      " Highlight this column

Thats a lot to take in, but I recommend you consider using them. All of these are personal preference, especially the choice to convert hard tabs into spaces, but thats another story all together.

The other thing is that you really don't want to have to type so many commands into vim every time you open it to edit a file. Whenever you open vim, it looks for a file where you would put these commands. It just reads them line by line and executes them every time you start vim. This hints at the incredible configure-ability of vim, but for now lets focus on getting them into your vim.

Vim expects you to have a "home folder", historically this where where configuration files for all your personal preferences would go for a variety of programs. If you're running linux, you will know at least that your home folder is filled with hidden config files. To find out where vim thinks home is try entering the following:

: echo $HOME

If you're on Windows, you would go to that directory and create a file called _vimrc, on Linux or Mac, you would create a file called .vimrc. Take the above code and copy it into your vimrc file.

The built-in help

As always, vim has some pretty impressive documentation you can consult. Just use the command:

: h keyword

And vim will open a buffer where it thinks the search term is most relevant. On that note, when you're navigating the help, you will see certain words that are a different color. If you put the cursor on them and press <Ctl-[>, you will be taken to the help section related to that term. To get back you press <Ctl-t>. Why this is, I'm not sure, but vim has a pretty cool history.

If you're bored, search for the keyboard layout vim was originally intended for, suddenly using the <ESC> key all the time will make a lot more sense.

Multiple Windows?!

Yep, the help command casually creates a new window on the screen. To switch between them press <Ctl-w> then j, where j is to go down, as per the hjkl navigation scheme.

I think however, window navigation is beyond the scope of this.

Comments !