How to Customize Vim

1/2/2020 tech

Write our own .vimrc!

Following the last blog about how to use Vim w/o any customization or plugin, this blog talks about how to customize Vim for better personal usage by writing a .vimrc.

In Unix systems, we could create a hidden file (starting with .) vim ~/.vimrc for Vim or vim ~/.config/nvim/init.vim. In Windows, we set value of environmental variable $MYVIMRC and vim $MYVIMRC.

In .vimrc, we don't need :.

There are basically 4 parts in a .vimrc:

  1. Settings such as line numbers and colorscheme. To see what can be set, search by :h option-list
  2. Mappings such as jj for <Esc>
  3. Plugins' installations and settings
  4. Vimscript defines functions to streamline common tasks such as setting a title in a Python file

# Mappings

To map some key, say, dd to \, we could do map \ dd, so \ deletes the current line.

To map in only one mode, we could do:

  • nm[ap]: map in normal mode
  • im[ap]: map in insert mode
  • vm[ap]: map in visual mode
  • cm[ap]: map in command mode

The following enables \ to delete a line under both normal and insert modes:

:nmap \ dd
:imap \ <Esc>ddi

To unmap dd from \, do unmap \.

If we define:

map \ dd
map - \

Because Vim mapping is recursive by default, we could then - to delete the current line.

Because some plugins define new mappings, there are cases when recursive mappings bring in accidental conflicts. Thus, we should always use non-recursive over recursive mappings.

To create a non-recursive mapping under **n***ormal* mode, do nnoremap <space> zz, after which pressing Space centers current line on the screen.

To summarize, these are non-recursive mappings:

  • no[remap]: map in all modes
  • nn[oremap]: map in normal mode
  • ino[remap]: map in insert mode
  • vn[oremap]: map in visual mode
  • cno[remap]: map in command mode

# Leader Key (See explanation (opens new window) from Learn Vimscript the Hard Way (opens new window))

After we define a <leader> key, we could define mappings combining the leader key:

let mapleader=','
inoremap <leader>w <Esc>:w<cr>

The leader key is , and we could now ,w to save.

p.s.: <cr> is carriage return (<Enter> or <Return>).

# Plugins

We can use a plugin manager to manage plugins which would make Vim more customizable and powerful.

# Plugin Manager

There are many plugin managers for Vim/NeoVim, I switched from singled-threaded Vundle (opens new window) to multi-threaded vim-plug (opens new window) since installing and updating plugins in parallel save much time.

# Plugin installation

To add a plugin using vim-plug (opens new window), just put the Github repo after Plug, between call begin and end:

call plug#begin('~/.vim/plugged')
Plug 'mhinz/vim-startify'
call plug#end()

# How to search for plugins

  1. You know what you need, then search on Google
  2. You don't know what you need, but randomly browse on vimawesome (opens new window)

# Useful Plugins

Below are some useful plugins, which you can learn how to use through these videos in Chinese (opens new window), searching on Google, or reading corresponding documentation:

These are great plugings to combine Git and Vim:

# Plugin customization

To customize installed plugins, please read the official Github README(linked above for each plugin I recommended) and customize according to your needs.

# My .vimrc

This is my init.vim (opens new window) for NeoVim.

# Useful Pages

# Coda

While so many editors/IDE's became favorable and extinct, Vim has been the world's best editor over almost 30 years. Vim/NeoVim are used by so many developers and the community is actively pushing forward many new features and plugins. Remeber: it is never a waste of time to learn Vim, and practice makes perfect.

This is the 2nd of totally 2 blogs I wrote about Vim. In the next blog, I will write about tmux.