Vim as an IDE: General Features
Overview
Although the goal is an IDE, in this part, I'll make Vim work as a fully functional text editor.
First you need to install a plugin manager for Vim. Then I'll give each feature and its corresponded configuration. At last, you can find the full .vimrc
at the end of this post.
Prerequisite: plugin manager
You could find several popular plugin managers for Vim. vim-plug is used here which is a minimalist plugin manager for Vim.
You can find the installation instructions here. For Unix/Linux, here's the one-line command:
1curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
2 https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
The core usage is straight-forward. Just update your .vimrc
like below:
1call plug#begin()
2
3" List plugins to be installed here
4
5call plug#end()
6
To reload your .vimrc
, just restart Vim or run :so ~/.vimrc
File explorer
NERDTree
is the plugin that provides features of 'File Explorer'. It provides powerful file and directory management functions using an intuitive tree.
Features
- Create, delete, rename, copy, move of nodes;
- Open files in different ways;
- Different view modes of the tree;
- Sync editing file in the tree;
- Bookmark management;
- And more.
Install NERDTree
plugin (vim-plug)
1call plug#begin()
2" ...
3Plug 'preservim/nerdtree'
4" ...
5call plug#end()
Useful mapping
1" Toggle the NERDTree using F7
2nmap <F7> :NERDTreeToggle<CR>
File structure
preservim/tagbar is used to display the current file Structure, and it also allows you to sync file positions with tags. This plugin doesn't generate tag files for you but rely on ctags
to do it.
Install ctags
You can install ctags
using system package manager on most of Linux distributions. It's also available on macOS
through homebrew
. For more details, please refer to the maintained ctags
implementation.
Install tagbar
plugin (vim-plug)
1call plug#begin()
2" ...
3Plug 'preservim/tagbar'
4" ...
5call plug#end()
Useful mapping
1" Toggle tagbar
2nmap <F8> :TagbarToggle<CR>
Status bar and tabs
Vim's status bar (statusline
) is a powerful and highly customizable feature, and you can use vim-airline plugin to make it show some most important information for editing.
The feature called Tabs
here is actually buffer list, which is similar to common concept like tabs in browsers, rather than tab-page
of Vim that behaves more like a layout. vim-airline has a built-in extension called tabline
for this purpose. You just need to enable it.
Install vim-airline
plugin (and theme repository)
1call plug#begin()
2" ...
3Plug 'vim-airline/vim-airline'
4Plug 'vim-airline/vim-airline-themes'
5" ...
6call plug#end()
7
Status bar
To display the symbols properly, you need to install powerline-compatible
fonts; to get the colors, just choose the corresponding theme.
The default status bar of vim-airline
is pretty good for most use cases. You can find most information for the current buffer like mode, git status (see later section), path, position, etc.
Install powerline-compatible
fonts
fonts-powerline
package is available on Ubuntu, and similar packages are available on most other distributions.- For desktops, you could install
powerline-compatible
fonts like other fonts. ThisMeslo Nerd
Font patched forPowerlevel10k
is recommended here, and you could also find instructions for popular terminals.
Configurations
1" Using Powerline Fonts
2let g:airline_powerline_fonts=1
3" Useing luna theme
4let g:airline_theme='luna'
Tabs
What's called tabs
here is not tab-page
in Vim, which should be called layout
or so. Rather, you're going to get tabs
like in modern browsers using tabline
extension of vim-airline
plugin.
Installation
tabline
is a built-in extension of vim-airline
, so you can use it directly.
Configurations
1" Show buffers list in tab section
2let g:airline#extensions#tabline#enabled = 1
3" Show buffers title only
4let g:airline#extensions#tabline#fnamemod = ':t'
Useful mapping
1" Switch buffers using Left and Right keys
2nnoremap <silent><Left> :bp<CR>
3nnoremap <silent><Right> :bn<CR>
Version control
tpope/vim-fugitive is probably the only plugin you need to work with git, and it's easy to install and without need to configure at all.
Another good news is that vim-airline
supports display of git information in the status bar out-of-the-box once you installed both of them.
Installation
1call plug#begin()
2" ...
3Plug 'tpope/vim-fugitive'
4" ...
5call plug#end()
File & content searching
FZF is preferred over CtrlP here. First it's much faster for large list, and second it's more powerful and flexible by design.
Installation
Install FZF
Basically there's two different ways of installation:
- Install
FZF
onto your system separately using package manager or its install script, and install its plugin for Vim. - Or, Install the Vim plugin and let it install
FZF
for you. This way, to useFZF
from command line, you have to add the installedFZF
executable to yourPATH
or create a symbol link to it. (Forvim-plug
, it'll be in~/.vim/plugged/fzf/bin/
.)
1" If FZF is installed separately
2Plug 'junegunn/fzf'
3
4" Or let the plugin install it using post-hook
5Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Install Rg
or Ag
The Vim plugin of FZF
has built-in support for Ag
(the silver searcher), Rg
(ripgrep), and other useful tools. To install the tools, just refer to the respective installation guides.
Other general settings
The followings are some general configurations on convention. Other plugins could override them for different purposes.
1" General settings
2syntax on
3set nu
4set hidden
5set laststatus=2
6set encoding=utf-8
7set colorcolumn=80
8set tabstop=4 softtabstop=4 shiftwidth=4 expandtab autoindent fileformat=unix
The full .vimrc
1call plug#begin()
2
3Plug 'vim-airline/vim-airline-themes'
4Plug 'vim-airline/vim-airline'
5Plug 'preservim/tagbar'
6Plug 'scrooloose/nerdtree'
7Plug 'Yggdroot/indentLine'
8Plug 'tpope/vim-fugitive'
9Plug 'godlygeek/tabular'
10Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
11Plug 'junegunn/fzf.vim'
12
13call plug#end()
14
15" Using Powerline Fonts
16let g:airline_powerline_fonts=1
17let g:airline_theme='luna'
18
19" Show buffers list in tab section
20let g:airline#extensions#tabline#enabled = 1
21" Show buffers title only
22let g:airline#extensions#tabline#fnamemod = ':t'
23
24" Toggle Tagbar
25nmap <F8> :TagbarToggle<CR>
26" Toggle NerdTree
27nmap <F7> :NERDTreeToggle<CR>
28" switching buffers
29nnoremap <silent><Left> :bp<CR>
30nnoremap <silent><Right> :bn<CR>
31
32" General settings
33syntax on
34set nu
35set hidden
36set laststatus=2
37set encoding=utf-8
38set colorcolumn=80
39set tabstop=4 softtabstop=4 shiftwidth=4 expandtab autoindent fileformat=unix
What's next
This part should get you started using Vim as an IDE or, at least, a general fully functional editor. I'll move on to features for more specific tech stacks in other posts.