Skip to content

Vim configuration example

I've tried many IDEs, but I keep coming back to Vim.

This is my Vim configuration, which has served me well over the years.

" be iMproved, required
set nocompatible

call plug#begin()
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'editorconfig/editorconfig-vim'
Plug 'godlygeek/tabular'
Plug 'morhetz/gruvbox'
Plug 'nanotech/jellybeans.vim'
call plug#end()

let g:coc_global_extensions = [
\    'coc-clangd',
\    'coc-css',
\    'coc-html',
\    'coc-json',
\    'coc-pyright',
\    'coc-tsserver',
\]

" May need for Vim (not Neovim) since coc.nvim calculates byte offset by count
" utf-8 byte sequence
set encoding=utf-8
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup

" Having longer updatetime (default is 4000 ms = 4s) leads to noticeable
" delays and poor user experience
set updatetime=300

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved
set signcolumn=yes

" Always show the status line.
set laststatus=2

" Enable 256 colors
set t_Co=256

" Enable GUI colors for the terminal to get truecolor
" See :h xterm-true-color
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors

" Set mode to "dark" or "light"
set background=dark

" Gruvbox
" https://github.com/morhetz/gruvbox/wiki/Configuration
colorscheme gruvbox
let g:gruvbox_italic=1
let g:gruvbox_contrast_dark="medium"

" Jellybeans
"colorscheme jellybeans
"let g:jellybeans_use_term_italics=1

" General
set modeline
set smartindent

" 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

" 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

" Highlight cursor line underneath the cursor horizontally.
set cursorline

" Set number of context lines around cursor
set scrolloff=10

" 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 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

" Show matching words during a search.
set showmatch

" Highlight search keywords
set hlsearch

" Spell checker.
setlocal spell

" Hard-wrap lines in insert mode.
set textwidth=79
set wrapmargin=0
set formatoptions+=t
set linebreak

" Better TAB completion
set wildmode=longest,list,full
set wildmenu

" Highlight trailing whitespaces
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/

" Remove trailing space upon save
autocmd BufWritePre * :%s/\s\+$//e

" C, C++, C#, Java, objc
autocmd Filetype c,cpp,cs,java,objc set cindent
autocmd Filetype c,cpp,cs,java,objc setlocal expandtab tabstop=4 shiftwidth=4

" Python
autocmd Filetype python set cindent
autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4

" Other
autocmd Filetype markdown setlocal expandtab tabstop=4 shiftwidth=4

" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

" GoTo code navigation
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gD <Plug>(coc-declaration)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Symbol renaming
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

" Remap keys for applying code actions at the cursor position
nmap <leader>ac  <Plug>(coc-codeaction-cursor)
" Remap keys for apply code actions affect whole buffer
nmap <leader>as  <Plug>(coc-codeaction-source)
" Apply the most preferred quickfix action to fix diagnostic on the current line
nmap <leader>qf  <Plug>(coc-fix-current)

" Remap ENTER to make it confirm completion.
inoremap <expr> <cr> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

" Search workspace symbols
nnoremap <silent><nowait> <space>s  :<C-u>CocList -I symbols<cr>
{
    "diagnostic.errorSign": "✗",
    "diagnostic.hintSign": "♦",
    "diagnostic.infoSign": "ⓘ",
    "diagnostic.warningSign": "⚠",
    "pyright.enable": true,
    "pyright.inlayHints.parameterTypes": false,
    "pyright.organizeimports.provider": "pyright",
    "pyright.testing.provider": "pytest",
    "python.formatting.provider": "autopep8",
    "python.linting.mypyEnabled": true,
    "python.linting.ruffEnabled": true,
    "clangd.arguments": ["--background-index", "--clang-tidy"],
    "clangd.fallbackFlags": ["-std=c++23"]
}