Skip to content

ViM tools and configuration

Renato Golin edited this page Dec 7, 2023 · 5 revisions

Basic Configuration

" Syntax highlight
filetype indent plugin on
syntax on

" Force UTF-8 encoding
set encoding=utf-8

" Visual cues
set showmode
set showcmd
set cursorline

" Indentation, spaces not tabs
set autoindent
set tabstop=2
set expandtab
set bs=2
set shiftwidth=2
set softtabstop=2
set tabpagemax=30

" Makefiles with tabs not spaces
autocmd FileType make setlocal noexpandtab

" Search & Replace (/ n N)
set ignorecase
set smartcase
set incsearch
set showmatch
set hlsearch

" File search (open tab, file search) ignore pattern
set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*.a,*.png,*.jpg,*.gif,*.o,.git,.svn

" Remember line position after close
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

" Spell check
set spell spelllang=en_gb
set nospell

" Force-reload of current/all open buffers (safer than auto-reload)
nmap <F5> :open<CR>
nmap <C-F5> :checktime<CR>

" Terminal mode
set t_Co=256

" LLVM File types (syntax highlight)
augroup filetype
  au! BufRead,BufNewFile *.ll set filetype=llvm
  au! BufRead,BufNewFile *.td set filetype=tablegen
  au BufRead,BufNewFile *.inc set filetype=cpp
  au BufRead,BufNewFile *.def set filetype=cpp
augroup END

" 80-column colour
highlight ColorColumn ctermbg=235 guibg=#2c2d27
let &colorcolumn=81

" Move between windows with Alt+Arrows
nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>

" Move across tabs with Ctrl+Arrows
nmap <silent> <C-t> :tabnew<CR>
nmap <silent> <C-Up> :tabprev<CR>
nmap <silent> <C-Down> :tabnext<CR>

" Move across buffers with Ctrl+b
nmap <silent> <C-b> :bnext<CR>
nmap <silent> <C-S-b> :bprev<CR>

Plugins

For plugins, you can use Vundle.

Follow the quick start guide.

After adding all plugins in your ~/.vimrc, open ViM and type :VundleInstall to install all plugins (clone the repos inside ~/.vim/bundle). Later, use :VundleUpdate to update the plugins. Some plugins (like YCM) need to be rebuilt by hand after update.

NERDTree

Provides a "navigation" tab for opening files across multiple folders.

""""""""""""""""""""""""""""""" NERD Tree
Plugin 'scrooloose/nerdtree'
" CTRL+n opens/closes the tree
map <C-n> :NERDTreeToggle<CR>
" TAB opens the tree
map <C-i> :NERDTreeFind<CR>
" ENTER selects the file and closes it
let g:NERDTreeQuitOnOpen = 1

CTRL+P

Provides a "Open File" functionality with full-text and regular expression search. Similar to VSCode.

""""""""""""""""""""""""""""""" CTRL+P
Plugin 'kien/ctrlp.vim'
let g:ctrlp_regexp = 1
let g:ctrlp_by_filename = 1
let g:ctrlp_clear_cache_on_exit = 0
"let g:ctrlp_working_path_mode = 'ra'
let g:ctrlp_working_path_mode = 0
let g:ctrlp_show_hidden = 0
let g:ctrlp_max_files = 0
let g:ctrlp_custom_ignore = 'pycache\|build\|git'
let g:ctrlp_open_multiple_files = '2vjr'
map <C-p><C-r> :CtrlPMRUFiles<CR>
map <C-p><C-b> :CtrlPBuffer<CR>

PowerLine / AirLine

There are two main projects that provide a "status bar" at the bottom of the screen:

  • PowerLine
  • AirLine

I prefer AirLine, but both are equally good.

You will need to install Noto fonts to make most of the symbols work.

""""""""""""""""""""""""""""""" AirLine
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
let g:airline_powerline_fonts = 1
let g:airline_theme = 'simple'
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
set laststatus=2
" GVIM font (works better with airline)
" You also need to clone https://github.com/powerline/fonts
" and install it
if has('gui_running')
  set guifont=Monospace\ Regular\ 9
endif

Bookmarks

Provides a bookmark facility that works across multiple files.

""""""""""""""""""""""""""""""" Bookmarks
Plugin 'mattesgroeger/vim-bookmarks'
map <F6> :BookmarkToggle<CR>
map <F7> :BookmarkPrev<CR>
map <F8> :BookmarkNext<CR>
map <F9> :BookmarkShowAll<CR>
map <C-F9> :BookmarkClearAll<CR>

Clang Format

Allows you to run clang-format inside the editor. You need to be in edit (i) mode.

""""""""""""""""""""""""""""""" Clang Format
Plugin 'rhysd/vim-clang-format'
let g:clang_format#code_style='llvm'
map <C-K> :ClangFormat<CR>
imap <C-K> <c-o>:ClangFormat<CR>

You Complete Me

There are newer auto-complete tools, but this is one that worked consistently over the years.

You need to rebuild (./install.py --clang-completer) every time you update the plugin.

First time you use in a repository it will have to "compile" all symbols and it takes a while (and uses all your cores).

Works with compile_commands.json that CMake generates (with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON), but you need to create a symbolic link from the build directory in your source directory. Adding at the base of the repository works for editing any file inside it (YCM searches recursively).

""""""""""""""""""""""""""""""" YouCompleteMe
"Plugin 'rdnetto/ycm-generator'
Plugin 'ycm-core/YouCompleteMe'
let g:ycm_min_num_of_chars_for_completion=3
let g:ycm_auto_trigger=0
let g:ycm_show_diagnostics_ui=1
let g:ycm_auto_hover=''
let g:ycm_path_to_python_interpreter='/usr/bin/python3'
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_autoclose_preview_window_after_insertion=1
let g:ycm_confirm_extra_conf=0
let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'
let g:ycm_always_populate_location_list = 1
let g:ycm_max_diagnostics_to_display = 300
map <C-]> :YcmCompleter GoTo<CR>
map <F2> :YcmCompleter GoToDefinition<CR>
map <F3> :YcmCompleter GoToDeclaration<CR>
map <F4> :YcmCompleter GetType<CR>
"map <C-m> :YcmCompleter FixIt<CR>
"map <?> :YcmCompleter GoToImprecise<CR>

Assorted Interesting Plugins

""""""""""""""""""""""""""""""" Others
" Git support
Plugin 'tpope/vim-fugitive'
" Julia support
Plugin 'JuliaEditorSupport/julia-vim'
" Vim-tmux
Plugin 'christoomey/vim-tmux-navigator'
" AnsiEsc
Plugin 'jbnicolai/vim-AnsiEsc'