Setup Vim for Development

Minimum setup for using Vim as an Integrated Development Environment (IDE).

Steps

  1. First, install Vim (version 9.x):
    sudo apt install vim
  2. Create a .vimrc file in your home directory (Refer to Appendix A for example file content).
  3. Follow instructions for installing Vim-Plug for Vim plugin management.
  4. Follow instructions for installing NERDTree for providing a file tree pane in Vim.
  5. Follow instructions for installing NERDTree Git Plugin for providing Git status flags in the NERDTree file pane.
  6. Follow instructions for installing Vim Autosave.
  7. Install Clangd for LSP server:
    sudo apt install clangd
  8. Follow instructions for installing ALE for providing LSP client for Vim.
  9. Open Vim, type the :PlugInstall command and wait until done, then quit Vim.
  10. Using the previous linked sites, add the desired configuration lines for NERDTree and ALE (see Appendix B below).
  11. Follow instructions for installing VimDevIcons.
  12. Download a Nerd Font (I chose ‘JetBrainsMono Nerd Font’).
  13. Decompress and install the font files to the system font folder via Terminal (example based on the one I chose):
    sudo unzip JetBrainsMono.zip -d /usr/share/fonts/
  14. Update the system font cache with the following command:
    fc-cache -vf
  15. Open Terminal, open the ‘Preferences’ pane, and set the font to the chosen Nerd font.
  16. Close Terminal.
  17. Open Terminal and verify the font has changed to your chose Nerd font.
  18. Add configuration for Vim built-in debugger ‘Termdebug’ to be loaded at the start of each Vim session (see Appendix B below).

Here is what Vim looks like upon opening with a directory argument (vim <directory>) after the configuration is done with NERDTree, ALE, and VimDevIcons plugins active:

ALE providing hover popup with information via Clangd language server. The color theme here is ‘shine’. See Appendix B.
ALE providing code completion suggestions.

Tips and tricks:

  • The built-in terminal can be invoked with the :terminal command.
  • Configure vimrc to have the built-in terminal enter insert mode upon opening (see Appendix B below).
  • Use [Ctrl] + ww to switch between the NERDTree pane and the other pane(s).
  • This GitHub page has a list of plugins for Vim/NeoVim, examine and install at your own risk.

For more detailed information and examples, please use your preferred search engine. I will correct mistakes as I have time.

Appendix A – Minimum vimrc setup

" Disable compatibility with vi which can cause unexpected issues.
set nocompatible

" Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype on

" Enable plugins and load plugin for the detected file type.
filetype plugin on
set omnifunc=syntaxcomplete#Complete

" Load an indent file for the detected file type.
filetype indent on

" Turn syntax highlighting on.
syntax on

" Add numbers to each line on the left-hand side.
set number

" Set shift width to 4 spaces.
set shiftwidth=4

" Set tab width to 4 columns.
set tabstop=4

" Use space characters instead of tabs.
set expandtab

" Do not save backup files.
set nobackup

" Do not let cursor scroll below or above N number of lines when scrolling.
set scrolloff=10

" Do not wrap lines. Allow long lines to extend as far as the line goes.
set nowrap

" While searching though a file incrementally highlight matching characters as you type.
set incsearch

" Ignore capital letters during search.
set ignorecase

" Override the ignorecase option if searching for capital letters.
" This will allow you to search specifically for capital letters.
set smartcase

" Show partial command you type in the last line of the screen.
set showcmd

" Show the mode you are on the last line.
set showmode

" Show matching words during a search.
set showmatch

" Use highlighting when doing a search.
set hlsearch

" Set the commands to save in history default number is 20.
set history=1000

" Enable auto completion menu after pressing TAB.
set wildmenu

" Make wildmenu behave like similar to Bash completion.
set wildmode=list:longest

" There are certain files that we would never want to edit with Vim.
" Wildmenu will ignore files with these extensions.
set wildignore=*.docx,*.jpg,*.png,*.gif,*.pdf,*.pyc,*.exe,*.flv,*.img,*.xlsx

" Highlight cursor line underneath the cursor horizontally.
set cursorline

" Highlight cursor line underneath the cursor vertically.
set cursorcolumn

Appendix B – My current “compelete” vimrc file

" Disable compatibility with vi which can cause unexpected issues.
set nocompatible

" Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype on

" Enable plugins and load plugin for the detected file type.
filetype plugin on
set omnifunc=syntaxcomplete#Complete

" Load an indent file for the detected file type.
filetype indent on

" Turn syntax highlighting on.
syntax on

" Add numbers to each line on the left-hand side.
set number

" Set shift width to 4 spaces.
set shiftwidth=4

" Set tab width to 4 columns.
set tabstop=4

" Use space characters instead of tabs.
set expandtab

" Do not save backup files.
set nobackup

" Do not let cursor scroll below or above N number of lines when scrolling.
set scrolloff=10

" Do not wrap lines. Allow long lines to extend as far as the line goes.
set nowrap

" While searching though a file incrementally highlight matching characters as you type.
set incsearch

" Ignore capital letters during search.
set ignorecase

" Override the ignorecase option if searching for capital letters.
" This will allow you to search specifically for capital letters.
set smartcase

" Show partial command you type in the last line of the screen.
set showcmd

" Show the mode you are on the last line.
set showmode

" Show matching words during a search.
set showmatch

" Use highlighting when doing a search.
set hlsearch

" Set the commands to save in history default number is 20.
set history=1000

" Enable auto completion menu after pressing TAB.
set wildmenu

" Make wildmenu behave like similar to Bash completion.
set wildmode=list:longest

" There are certain files that we would never want to edit with Vim.
" Wildmenu will ignore files with these extensions.
set wildignore=*.docx,*.jpg,*.png,*.gif,*.pdf,*.pyc,*.exe,*.flv,*.img,*.xlsx

" Highlight cursor line underneath the cursor horizontally.
set cursorline

" Highlight cursor line underneath the cursor vertically.
set cursorcolumn

" color theme
colors shine 

set completeopt=menu,popup,noinsert,noselect

set lazyredraw

set mouse=a

set noswapfile

set colorcolumn=80

call plug#begin()
  Plug 'preservim/nerdtree'
  Plug 'Xuyuanp/nerdtree-git-plugin'
  Plug 'dense-analysis/ale'
  Plug 'ryanoasis/vim-devicons'
  Plug '907th/vim-auto-save'
call plug#end()

" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
" If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree.
autocmd BufEnter * if winnr() == winnr('h') && bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
    \ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif
" Exit Vim if NERDTree is the only window remaining in the only tab.
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | call feedkeys(":quit\<CR>:\<BS>") | endif
nnoremap <leader>n :NERDTreeFocus<CR>
nnoremap <C-n> :NERDTree<CR>
nnoremap <C-t> :NERDTreeToggle<CR>

" ALE configuration
let g:ale_completion_enabled = 1
let g:ale_cpp_clangd_executable = '/usr/bin/clangd'
let g:ale_cpp_clangd_options = '--log=verbose --header-insertion=iwyu --header-insertion-decorators=true --all-scopes-completion=true --pretty --background-index --limit-results=500 --completion-style=detailed'
let g:ale_hover_to_floating_preview = 1
augroup ale_hover_cursor
  autocmd!
  autocmd CursorHold * ALEHover
augroup END

" automatically enter insert mode when the terminal buffer is opened
autocmd BufEnter * if &buftype == 'terminal' | :startinsert | endif

"  load built-in Vim debugger at start of session
packadd! termdebug

let g:auto_save = 1
let g:auto_save_events = ["TextChanged"]