Login With Github

Vim Configuration Tutorial In 10 Minutes

Vim is one of the most important editors, and the main advantages are:

  • You can use the keyboard completely to operate instead of using the mouse.
  • The system resources are small, so it's easy to open large files.
  • After the keyboard command becomes your muscle memory, the operation speed will be extremely fast.
  • The servers all install Vi or Vim by default.

However, the configuration for Vim is not easy, as it has its own syntax, and there are a lot of commands for it. So I've summarize a list of the main configuration items in the article.

1. Basics

Generally, Vim's global configuration is in /etc/vim/vimrc or /etc/vimrc, and is valid to all users. And the user's personal configuration is in ~/.vimrc.

If you want to enable a configuration item for a single edit, you can enter the configuration after typing a colon under the command mode. For example, the configuration set number can either be written inside .vimrc or be entered under the command mode.

:set number

Generally, configuration items have two settings: "on" and "off". You just need to add the prefix "no" to the setting to turn the "on" to the "off".

" On
set number

" Off
set nonumber

In the above code, it's a comment if the line started with double quotation mark.

To query whether a configuration item is on or off, you can enter the configuration under the command mode with a question mark after it.

:set number?

The above command will return number or nonumber.

If you want to view help, you can use the help command.

:help number

2. Basic Configuration

set nocompatible

It won't be compatible with Vi (You need to use Vim's own operational commands).

syntax on

It will turn on syntax highlighting. And it can identification code automatically, and be displayed in multiple colors.

set showmode

It will shows at the bottom whether it is currently in command mode or insert mode.

set showcmd

The currently typed command will be displayed at the bottom under the command mode. For example, if you type the command 2y3d, it will shows 2y3 at the bottom. And when you type d, the operation will be completed and the display will disappear.

set mouse=a

You're supported to use the mouse.

set encoding=utf-8

It's supported to use utf-8 encoding.

set t_Co=256

It'll enable 256 colors.

filetype indent on

It'll enable file type checking and load the indentation rules corresponding to this type. For example, if you are editing a .py file, Vim will look for Python's indentation rule ~/.vim/indent/python.vim for you.

3. Indentation

set autoindent

After pressing the Enter key, the indentation of the next line will keep consistent with the indentation of the previous line automatically.

set tabstop=2

The number of spaces that Vim will display when the Tab key is pressed.

set shiftwidth=4

It's used to indicate the number of characters in each level when you press >> (increase one level of indentation), << (cancel one level of indentation), or == (cancel all indentations) on the text.

set expandtab

This setting will turn Tab into a space automatically because the Tab key is inconsistent in different editors.

set softtabstop=2

It's used to indicate how many spaces Tab will be converted into.

4. Appearance

set number

It's used to display the line number.

set relativenumber

It's used to display the line number of the current line where the cursor is located. All other lines will be the relative line numbers relative to the line.

set cursorline

The current line where the cursor is located will be highlighted.

set textwidth=80

It's used to set the line width to indicate how many characters will be displayed in one line.

set wrap

It will wrap lines automatically, which means a line that is too long will be divided into several lines.

set nowrap

It will close automatic line-wrapping.

set linebreak

The linebreaking occurs only if encountering a specified symbol (such as a space, hyphen or others). In other words, it won't occur inside a word.

set wrapmargin=2

It's used to specify the number of characters that are vacated between the linebreaking and the right edge of the edit window.

set scrolloff=5

It will indicate the distance(unit: line) from the cursor to the top/bottom When scrolling vertically.

set sidescrolloff=15

It will indicate the distance(unit: character) from the cursor to the beginning or end of the line When scrolling horizontally. This configuration will be useful when not linebreaking.

set laststatus=2

It 's used to indicate whether to display the status bar. 0 means no display, 1 means to display only in multi-window, and 2 means to display the status bar.

set  ruler

The current position of the cursor (in which row and which column) will be displayed in the status bar.

5. Search

set showmatch

The other corresponding parenthesis, square bracket, and brace will be highlighted automatically when the cursor encounters one parenthesis, square bracket, and brace.

set hlsearch

Highlight the match when searching.

set incsearch

Each time you enter a character, it will jump to the result of the first match automatically.

set ignorecase

Case will be ignored when searching.

set smartcase

If the ignorecase is on at the same time, it will be case sensitive to the search term with only one uppercase letter; otherwise, it will be case insensitive. For example, when searching for Test, it will not match test; but when searching for test, it will match Test.

6. Edit

set spell spelllang=en_us

It will turn on the spell checking for English words.

set nobackup

It won't create backup files. However, an additional backup file will be created by default when a file is being saved, and its filename is to add a tilde(~) to the end of the original filename.

set noswapfile

It's used to indicate it won't create swap files. Swap files are mainly used to recover files when the system crashes. The file name begins with ., and ends with .swp.

set undofile

Retain the cancellation history.

Vim will save the operation history when editing, which can be used to undo changes. By default, the operation log is only valid for its edit. Once the edit is finished and the file is closed, the operation history disappears.

If you enable the setting, the operation record will remain in a file and continue to exist after the file is closed. It means that you can undo the last operation by reopening the file. The undo file is the hidden file that is saved with the original file, and its file name begins with .un~.

set backupdir=~/.vim/.backup//  
set directory=~/.vim/.swp//
set undodir=~/.vim/.undo// 

Set the save location of the backup file, swap file and operation history file.

The ending // indicates that the generated file name has an absolute path, and the directory separator is replaced with % in the path, which is used to prevent file being renamed.

set autochdir

Switch the working directory automatically. This is mainly used in the cases where multiple files are opened in a Vim session. And the default working directory is the directory of the first file opened. The configuration will switch the working directory automatically to the directory of the file being edited.

set noerrorbells

Do not make any noise when an error occurs.

set visualbell

When an error occurs, a visual cue will be given, which usually is the screen flickers.

set history=1000

It's used to indicate how many historical operations Vim needs to remember.

set autoread

Open file monitoring. If there are external changes happened to the file during editing (such as being edited by another editor), a prompt will be given.

set wildmenu
set wildmode=longest:list,full

Under the command mode, the bottom-layer operation instructions will be completed automatically by pressing the Tab key. The first time you press Tab, a list of all matched operation instructions will be displayed; and the second time you press Tab, each instruction will be selected in turn.

7. Reference

0 Comment

temp