Leveling Up Your Vim

17 April 2017

If you’re just starting out your programming career, especially if you’re just learning the command line, getting into vim can be a bit of a nightmare. I remember having to completely quit out of my terminal because I couldn’t quit vim. I think we’ve all been there.

Well, not anymore!

This guide will give you some good resources to get you started, or get your beginner vim knowledge to the next level.

Customize your vim environment with vimrc

The most important thing when you get started is making vim your own. Just as you can customize your bash or zsh profile, you can customize your vim profile with a “.vimrc” file.

If you’re familiar with a bashrc or zshrc file, a vimrc file is pretty similar. It allows you to set certain user preferences, such as your color scheme, key mappings, or add plugins.

This is my vimrc:

colorscheme desert 
syntax enable "enable language specific syntax
set tabstop=4 "tabs are 4 spaces
set number "show line numbers on the right
set showcmd "show command at bottom
set wildmenu "show autocomplete options
set showmatch "highlight corresponding [{()}]
set laststatus=2 "always show status bar
set splitbelow "set split to bottom
set splitright "set split to right hand side
filetype indent on "turn on file specific indentation

"add pathogen plugins
execute pathogen#infect()

"Map NERDTree toggle to func + F2
map <F2> :NERDTreeToggle<CR>

It is really simple, and the syntax is pretty readable.

There are a lot of vimrc files out there that people like to share, and you may be tempted to just copy and paste a vimrc file you find on the internet and just use that. Personally, a good rule of thumb for me is that if I don’t know what it does, I do not put it in my vimrc.

I like to keep my vimrc pretty simple, and I like to know exactly what each line does. I find that the settings above are all I need.

Let’s look at a couple important parts of the vimrc file:

Creating a vimrc file

If you don’t know if you have a vimrc file, you probably don’t have a vimrc file.

Most people keep their vimrc file at their home directory.

You can search for it by running:

ls -a

If you see a “.vimrc” file, then go ahead and open it with vim (vimception!). If not, go ahead and create it:

touch .vimrc

You can also just:

vim .vimrc

Then save your file by hitting the escape key, then typing “:w” (meaning “write”).

Let’s add things to our vimrc file!

Picking a color scheme

Vim comes with some standard color schemes pre-installed for you to select. The pre-installed color schemes are:

You can set your color scheme in your vimrc file as follows:

colorscheme darkblue

(Or whichever color scheme from the list above you’d like.)

If you want to venture outside of the pre-installed color schemes, Vim Colors is a good resource.

Install instructions can typically be found on the color scheme Github repo, but really all you need to do is download the “.vim” file and place it here:

/usr/share/vim/vim74/colors

Please note “vim74” is the version of vim I currently have. Your version might vary. You can find your version (in Mac OS X) by opening vim and entering the following command—

:version

Plugin management with Pathogen

Vim is more fun with plugins!

You can use Pathogen to handle your vim plugins. This means anytime you install a vim plugin in your .vim/bundle directory, Pathogen will load it into vim for you.

To install Pathogen, you can follow the instructions in the Pathogen Github repo, then make sure you add the following line into your vimrc file:

execute pathogen#infect()

Alternatively, some people like to use Vundle, so you may want to check that out as well.

File exploration with NERDTree

While using vim, it is helpful to be able to open a file tree, and navigate to other files in your directory structure. NerdTree adds this functionality to vim.

This command maps the key “f2” to toggle , which is a file explorer for vim. This makes it easy to navigate to files and open them in vim.

Installing NERDTree is easy, simply follow the instructions for how to install it with Pathogen (or the vim plugin manager of your choice).

Once you have installed NERDTRee, you will need to add a key mapping that will toggle NERDTree while in vim. I use the following key mapping:

map <F2> :NERDTreeToggle<CR>

Now everytime I hit “fn” + “f2”, NERDTree will togge my file system structure for me to navigate and open files.

Helpful status info with Airline

A lot of people in the vim community like to use Powerline, which is a helpful status bar you can insert at the bottom of your vim editor that is really good for navigating vim, and displaying useful information.

I personally have never got it to install correctly, so I use Airline instead. It’s much easier to install!

If you’re using Pathogen, simply follow the installation instructions here and you’re good to go!

Installing the Powerline fonts

Lots of people in the community like to use the Powerline fonts for their command line.

Installation is easy, simply git clone the Powerline fonts repo, cd into it, then run the “./install.sh” command. This will copy the necessary fonts to your Fonts directory. Once this is done, you can safely remove the git repo you just downloaded.

Getting good

I searched online for a good course on how to get good with vim. Honestly, I didn’t find anything good anywhere.

However, you don’t need no stinkin outside resources! Vim comes with a preloaded tutorial that teaches you everything you need to know about vim.

Simply type this into your terminal:

vimtutor

and hit enter! This will load the Vimtutor. This is a text-based tutorial that lets you learn vim as you go. It is really the best resource for learning vim, and it is completely free and already installed with vim!

Vim cheatsheet

There are a lot of commands and shortcuts, so I do refer to a cheatsheet from time to time when I forget a specific command. I would recommend you make one of your own for reference. There are many vim cheatsheets online, but I find that making one for your own use will help you commit the commands to memory.

WINDOWS
ctrl + ww switch between windows
ctrl + wv split windows vertically

DELETE
x delete character the cursor is on
dw delete word
d<X>w delete <X> words
de delete until end of word
d$ delete until end of line
dd delete a whole line
<X>dd delete <X> lines

MOVE
0 move to beginning of line
$ move to end of line
w move to beginning of word
<X>w move to beginning of <X> words
e move to end of word
<X>e move to end of <X> words
b move to beginning of word (backwards)
gg move to beginning of file
G move to end of file

PUT AND REPLACE
c change
ce change until end of word
c<X>w change <X> words
c$ change until end of line
r replace one character (i.e. rx replace current character with x_
p place / put yanked line

Further resources

There are some other good resources available to help you on your journey to conquering vim.