A Vim Setup Guide: Making Vim Your Editor of Choice

Matthew G Lang
5 min readMar 28, 2022

I feel the most comfortable in a terminal environment. A blank screen with a blinking cursor minimizes distractions. Furthermore, things just get done faster as both of my hands stay on the keyboard.

This feeling extends to whenever I am developing software. A full IDE is often not necessary or even counterproductive. With Vim — given one has conquered its learning curve — things simply get done faster. Opening a file, navigating through it, and making a few edits takes only seconds.

Intuitive customizability is at the root of Vim’s speediness. What makes Vim fast is what makes Vim fast for you. No two Vim users share the same experience, and that is the beauty of it. The two main areas of customizability are how a file is presented and how you navigate through that file.

This article will show you some simple steps to take in order to make Vim your text editor.

Starting Off

All of your customizations to vim are located in your .vimrc file in the home directory. The commands will be written in Vim’s own scripting language “Vimscript”. The commands are fairly intuitive and easy to write, so there is no need to spend hours learning this language.

Changing Vim’s Appearance

First, we’ll start off with customizing the look of Vim. Here are some commands you can add to your .vimrc

syntax on          " turn on syntax highlighting
syntax off " turn off syntax highlighhting

This enables syntax highlighting for files written in any language with a defined syntax. The default value of syntax is different machine to machine, so it’s good to have this defined for consistency.

colorscheme elflord

Will change your color scheme. All colors can be found in your home directory inside .vim/colors.

set number           " show line number
set nonumber " hide line number

This will number the lines of a file as shown below.

:set number
:set nonumber
set hlsearch         " searches are highlighted
set incsearch " search as you type

When searching a file, setting hlsearch will highlight any matches found. Setting incsearch will make Vim search as you type.

:set hlsearch
:set nohlsearch

Formatting and Code Style

Vim, similar to an IDE, allows customization of how it automatically styles something (such as tabs, next line, etc) and how it displays it.

set tabstop=4       " tabs in a file are 4 spaces wide
set softtabstop=4 " Width of wightspace using <TAB> or <BACKSPACE>
set shiftwidth=4 " indentation will be 4 spaces wide

tabstop is how much whitespace a <TAB> looks like. softtabstop is how much whitespace a tab or backspace is worth. The difference between the two is the former only deals with what tabs look like while the latter is what tabbing actually does. shiftwidth is how many spaces are used when indenting.

set expandtab       "<TAB> will now use spaces
"or
set noexpandtab "<TAB> uses <TAB>

expandtab will use spaces anytime <TAB> is pressed.

It is often the case that one deals with several programming languages and different styles. With vim, you can make a specific style language-specific by using setlocal. For example, suppose you are using Linux Kernel style with your C projects, and thus use 8 spaces to indent. However, your Java code only uses 4. Lastly, all of your other projects use tabs instead of spaces. You can write the following:

" Global configurationset mouse=a
set number
set showcmd
set incsearch
set hlsearch
set ignorecase
set smartcase
...
set noexpandtab " tabs instead of spaces
"Local configuration" .c files
autocmd Filetype c setlocal expandtab tabstop=8 shiftwidth=8 softtabstop=8
".java files
autocmd Filetype c setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4

set noexpandtab is a global configuration. This means that any file opened with Vim will use this setting.

For the other two lines, there are many things happening. autocmd commands are executed whenever something happens. They have the following structure:

autocmd <EVENT> <COMMAND>
  • <EVENT> is when the command should be executed. In this case, we want it to run anything there’s a file type of .c or .java.
  • <COMMAND> is what the command should execute. We only want this to run local to the session and filetype, so we use setlocal instead of set.

Macros and Keymapping

Specifically, inoremap and nnoremap. The former is a macro that is run when Vim is in insert mode, whereas the latter is when Vim runs in normal mode.

As an example, suppose we want to be able to write to our file and quit very quickly. We can write the following:

nnoremap ;lkj :wq<RETURN>
inoremap ;lkj <esc>:wq<RETURN>

When the text “;lkj” is written, Vim will type what is to the right as if it had the keyboard. In this instance, it escapes to normal mode (if in Insert mode originally), types the command :wq (save and quit), and enters it.

In general, noremap commands have the following structure:

{i/n}noremap <EVENT_TEXT> <EXECUTE_TEXT>

Whenever EVENT_TEXT is written, Vim writes the set of characters in <EXECUTE_TEXT>.

It is important to note that <EXECUTE_TEXT> is everything that comes after the space between it and <EVENT_TEXT>. This can lead to some strange errors.

inoremap jk <ESC>         "Maps jk to <ESC>

Above is a command that puts Vim into normal mode from insert mode. Not only does vim execute the escape key, but also every space between it and the comment. This has the unwanted effect of the cursor moving forward several spaces after going from insert mode. Thus, avoid whitespace and comments after a mapping command.

Macros have endless possibilities and are a big part of what makes Vim so speedy. They can be as simple as one-to-one command replacements or as complex as executing multi-line scripts.

Conclusion

Overall, .vimrc allows one to configure the appearance, format, and behavior of Vim specific to the user’s preferences. This article only shows a few of the ways to change Vim to your liking. Vimscript can be used to write functions for text replacement routines (a keymapping to wrap a word in quotes, remove extra whitespace, etc).

Similar Articles

Below are some technical writing articles that inspired this one. These go into further detail in using Vimscript.

Learn Vimscript the Hard Way

Vimrc Tutorial

A Good Vimrc

--

--