How to Customize Vim
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
:
Settings
such as line numbers and colorscheme. To see what can be set, search by:h option-list
Mappings
such asjj
for<Esc>
Plugins
' installations and settingsVimscript
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 modeim[ap]
: map in insert modevm[ap]
: map in visual modecm[ap]
: map in command mode
The following enables \
to delete a line under both normal and insert modes:
:nmap \ dd
:imap \ <Esc>ddi
2
To unmap dd
from \
, do unmap \
.
If we define:
map \ dd
map - \
2
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 modesnn[oremap]
: map in normal modeino[remap]
: map in insert modevn[oremap]
: map in visual modecno[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>
2
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()
2
3
# How to search for plugins
- You know what you need, then search on Google
- 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:
- start
nvim
with a fancy screen: vim-startify (opens new window) - customize your
colorscheme
to your favorite:colo[rscheme] {your favorite}
. Personally I liked: gruvbox (opens new window), hybrid (opens new window), solarized (opens new window), molokai (opens new window), etc. - beautify status and tab lines: vim-airline (opens new window)
- display indentation levels with thin vertical lines: indentline (opens new window)
- file explorer as tree: nerdtree (opens new window)
- fuzzy file finder: 1. fzf (opens new window) is a really good command-line fuzzy finder, and in Vim you could use fzf.vim (opens new window). 2. more traditional and is less powerful: ctrip.vim (opens new window). I prefer
fzf
b/c of its blazing speed. - jump to any character instantly on screen: vim-easymotion (opens new window)
- overview of file structure: majutsushi/tagbar (opens new window)
- change, delete, or add quotes/parentheses: vim-surround (opens new window)
- find and replace in multiple files: far.vim (opens new window)
- highlight different words simultaneously: lfv89/vim-interestingwords (opens new window)
- code completion: 1. neoclide/coc.nvim (opens new window) is a new tool by a Chinese frontend pro but needs to install nodejs before installation. 2. Shougo/deoplete.nvim (opens new window) does a similar job but I feel the user experience of coc is better
- format code: sbdchd/neoformat (opens new window)
- lint code: w0rp/ale (opens new window)
- comment code: tpope/vim-commentary (opens new window)
- easily align code: junegunn/vim-easy-align (opens new window)
- easier look on parentheses: luochen1990/rainbow (opens new window)
These are great plugings to combine Git and Vim:
- the best Git wrapper in Vim: tpope/vim-fugitive (opens new window) could do the following commands:
:G[status]
,:Gblame
,:Gedit
for a try - git diff in the gutter (sign column): airblade/vim-gitgutter (opens new window)
- git commit browser: junegunn/gv.vim (opens new window)
# 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
- a clean and well-documented
.vimrc
:The Ultimate Vim configuration: vimrc (opens new window) - a Vim rookie could use SpaceVim (opens new window), but personally I hate black box
- a really good book if you want to read more about Vim: Practical Vim (opens new window)
# 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
.