Vim is powerful on its own, but plugins lift productivity even higher. In this lesson we install a plugin manager and try three plugins that come up in real work.
Working Code
First, install the plugin manager vim-plug. One line in the terminal does it:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Once installed, add a plugin block to ~/.vimrc:
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-surround'
Plug 'tpope/vim-commentary'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
call plug#end()
Open Vim and run :PlugInstall — the plugins download automatically.
Try It Yourself
vim-surround — the wrapping master
Swap, delete, or add quotes, brackets, and tags with ease:
" change double quotes to single quotes
cs"' → "hello" → 'hello'
" remove surrounding characters
ds" → "hello" → hello
" wrap a word in double quotes
ysiw" → hello → "hello"
" wrap a Visual selection in parentheses
S) → hello → (hello)
Open any file and practice cs"', ds", and ysiw".
vim-commentary — toggle comments
" toggle the current line's comment
gcc
" toggle comments on 3 lines
3gcc
" comment by motion (current paragraph)
gcap
" comment a Visual selection
gc
The comment style adapts to the file type. .js uses //, .py uses #.
fzf.vim — fuzzy finder
Find files and contents across the project in a flash:
" search by filename
:Files
" search open buffers
:Buffers
" search file content (requires ripgrep)
:Rg search_term
Add shortcuts to ~/.vimrc for convenience:
nnoremap <leader>f :Files<CR>
nnoremap <leader>b :Buffers<CR>
nnoremap <leader>r :Rg<CR>
"Why?"
Why do you need a plugin manager? Managing plugins manually makes installing, updating, and removing them painful. vim-plug wraps these into one-command operations.
| Command | Role |
|---|---|
:PlugInstall | Install new plugins |
:PlugUpdate | Update installed plugins |
:PlugClean | Remove plugins deleted from vimrc |
:PlugStatus | Check plugin status |
Install only what you need. Too many plugins slow Vim startup and may clash with your key bindings.
Deep Dive
Picking plugins
- Check the GitHub star count and most recent commit date.
- A well-maintained README usually signals a well-maintained plugin.
- VimAwesome lists popular plugins by category.
Practice with three plugins
- Install vim-plug and add the three plugins above to
~/.vimrc. - Run
:PlugInstall. - Open any code file and try:
ysiw"— wrap a word in quotesgcc— toggle a line comment:Files— search for files
- Add fzf shortcuts (
<leader>f,<leader>b) to~/.vimrc.
What does cs"' do in Vim?