set runtimepath^=~/.guix-home/profile/share/vim/vimfiles/ "------------------------------------------------- " General "------------------------------------------------- " Turn off vi compatibility. set nocompatible " Set the amount of lines of history to remember. set history=100 " Set a mapleader key. This is used for extra key combinations. let mapleader = '\' map " Better command line completion. set wildmenu set wildmode=list:longest,full " Ignore compiled files. set wildignore=*.o,*~,*.pyc " Ignore binary files. set wildignore^=*.png,*.pdf " Filetypes autocmd BufRead,BufNewFile *.irst setfiletype rst autocmd BufRead,BufNewFile *.puml setfiletype plantuml autocmd BufRead,BufNewFile *.tikz setfiletype tex autocmd BufRead,BufNewFile *.tpp setlocal filetype=cpp " When editing a file, always jump to the last known cursor position. " Don't do it when the position is invalid, when inside an event handler " (happens when dropping a file on gvim) and for a commit message (it's " likely a different one than last time). autocmd BufReadPost * \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' \ | exe "normal! g`\"" \ | endif " Set the swap directory. The extra '/' suffix is required to make vim create " unique filenames. call mkdir(expand("~/.cache/nvim/swap"), "p", 0700) set dir=~/.cache/nvim/swap// " Use system clipboard by default set clipboard +=unnamedplus "------------------------------------------------- " User interface "------------------------------------------------- " Display line numbers. set number " Set the line numbers relative from the current line. set relativenumber " Show the (partial) command in status line. set showcmd " Always display the status line, even if only one window is displayed. set laststatus=2 " Shows a dialogue asking if the file has to be saved, " instead of raising an error. set confirm " Keep the cursor in the centre of the buffer if possible. set scrolloff=10000 " Toggle NERDTree. map :NERDTreeToggle " Always show NERDTree on start if no files were specified. autocmd StdinReadPre * let s:std_in=1 autocmd VimEnter * if argc() ==# 0 && !exists("s:std_in") \ | exe 'NERDTreeToggle' | endif " Disable automatic folding. set nofoldenable " Add buffer switching with airline let g:airline#extensions#tabline#buffer_idx_mode = 1 nmap 1 AirlineSelectTab1 nmap 2 AirlineSelectTab2 nmap 3 AirlineSelectTab3 nmap 4 AirlineSelectTab4 nmap 5 AirlineSelectTab5 nmap 6 AirlineSelectTab6 nmap 7 AirlineSelectTab7 nmap 8 AirlineSelectTab8 nmap 9 AirlineSelectTab9 nmap bp AirlineSelectPrevTab nmap bn AirlineSelectNextTab nmap AirlineSelectPrevTab nmap AirlineSelectNextTab " Delete the current buffer without closing the windows map bd :Bdelete " Close window map wd :q " Vertical split map ws :vsplit " Horizontal split map wh :split " Exit vim map qq :qa " Invoke completion on : for ledger. autocmd FileType ledger imap : : " Easy vertical terminal split opening (why is this not built-in?) cnoreabbrev vterm vert term "------------------------------------------------- " Colours and fonts "------------------------------------------------- " Enable Doxygen syntax highlighting. let g:load_doxygen_syntax=1 " Set the color scheme. " colorscheme wombat256mod " Set the airline theme and configure the tabline " let g:airline_theme='wombat' let g:airline#extensions#tabline#enabled = 1 let g:airline#extensions#tabline#fnamecollapse = 1 let g:airline#extensions#tabline#formatter = "default" " Use 24-bit colours in the terminal, requires 'advanced' terminal emulator. set termguicolors " Make the terminal background transparent. hi Normal guibg=NONE ctermbg=NONE " Set encoding. set encoding=utf8 " Set filetype. set ffs=unix "------------------------------------------------- " Search "------------------------------------------------- " Highlights all matches of the search pattern. set hlsearch " Show all the matches while typing the search command. set incsearch " Ignores the case when searching. set ignorecase " Case insensitive search, except when capital letters are used. set smartcase " Redraw the screen when is pressed. " This also turns off the search highlighting until the next search. nnoremap :nohl " Set the manual page section order for keywordprg let $MANSECT="3p:3:2:1:n:l:8:0:5:4:9:6:7" "------------------------------------------------- " Text (tab, spaces, indent) "------------------------------------------------- " Tabsize of 8 (default). set shiftwidth=8 tabstop=8 " Reset default behaviour to tabs instead of spaces for reStructuredText files. autocmd Filetype rst setlocal noexpandtab softtabstop=8 " 2 spaces instead of a tab for YAML files. autocmd Filetype yaml setlocal expandtab shiftwidth=2 softtabstop=2 " 4 spaces instead of a tab for Python and Haskell files. autocmd Filetype python,haskell,lhaskell,js setlocal expandtab shiftwidth=4 \ softtabstop=4 " Copy indent from current line when starting a new line. set autoindent smartindent " Highlight tabs and trailing spaces set list listchars=tab:>\ ,trail:ยท " Removes trailing spaces when saving autocmd BufWrite * :call Delete_trailing_spaces() " Disable softwrapping on long lines set nowrap " Enable persistent undo if it is supported if has('persistent_undo') let vundodir = expand('~/.cache/nvim/undo') if !isdirectory(vundodir) call mkdir(vundodir) endif let &undodir = vundodir set undofile endif "------------------------------------------------- " Deletion "------------------------------------------------- " Delete without putting the deleted words into the register. map odw "_dw map odW "_dW map ode "_de map odE "_dE map odb "_db map odB "_dB map odd "_dd map od^ "_d^ map od$ "_d$ map od{ "_d{ map od} "_d} map odi( "_di( map odi) "_di) map odi' "_di' map odi" "_di" map odi< "_di< map odi> "_di> map odf. "_df. map odf? "_df? map odf! "_df! map odt. "_dt. map odt? "_dt? map odt! "_dt! "------------------------------------------------- " Code " Compiling "------------------------------------------------- " Show warnings. set statusline=%#warningmsg#%{SyntasticStatuslineFlag()}%* " Set syntastic options. let g:syntastic_always_populate_loc_list = 1 let g:syntastic_mode_map = { \ "mode": "active", \ "passive_filetypes": ["python"], \} let g:syntastic_check_on_open = 0 let g:syntastic_check_on_wq = 0 let g:syntastic_check_on_wq = 0 " Compiles the current file. map c :SyntasticCheck " Opens the location list that shows the errors. map co :lopen " Closes the location list that shows the errors. map cd :lclose " Close any preview window that is open map cf :pclose "" Completion framework. let g:deoplete#enable_at_startup = 1 " Disable abbreviation of signatures (also causes issues with echodoc) call deoplete#custom#source('_', 'max_abbr_width', 0) " Disable auto-completion on backspace character. call deoplete#custom#option('refresh_backspace', v:false) " Disable preview window containing function documentation. set completeopt-=preview " Map arrow keys to and for better selection behaviour. :inoremap pumvisible() ? "\" : "\" :inoremap pumvisible() ? "\" : "\" " Allow selection of completion with tab and shift+tab. :inoremap pumvisible() ? "\" : "\" :inoremap pumvisible() ? "\" : "\" if executable('pyls') " pip install python-language-server au User lsp_setup call lsp#register_server({ \ 'name': 'pyls', \ 'cmd': {server_info->['pyls']}, \ 'allowlist': ['python'], \ }) endif if executable('clangd') au User lsp_setup call lsp#register_server({ \ 'name': 'clangd', \ 'cmd': {server_info->['clangd', '-background-index']}, \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp'], \ }) endif if executable('gopls') au User lsp_setup call lsp#register_server({ \ 'name': 'gopls', \ 'cmd': {server_info->['gopls', '-remote=auto']}, \ 'allowlist': ['go'], \ }) endif function! s:on_lsp_buffer_enabled() abort " setlocal omnifunc=lsp#complete " Do not use virtual text, they are far too obtrusive. " let g:lsp_virtual_text_enabled = 0 " echo a diagnostic message at cursor position " show diagnostic in floating window " let g:lsp_diagnostics_float_cursor = 1 " whether to enable highlight a symbol and its references " let g:lsp_highlight_references_enabled = 1 " let g:lsp_preview_max_width = 80 setlocal signcolumn=yes if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif nmap gd (lsp-definition) nmap gs (lsp-document-symbol-search) nmap gS (lsp-workspace-symbol-search) nmap gr (lsp-references) nmap gi (lsp-implementation) nmap gt (lsp-type-definition) nmap rn (lsp-rename) nmap [g (lsp-previous-diagnostic) nmap ]g (lsp-next-diagnostic) nmap K (lsp-hover) inoremap lsp#scroll(+4) inoremap lsp#scroll(-4) let g:lsp_format_sync_timeout = 1000 let g:lsp_diagnostics_enabled = 1 let g:lsp_diagnostics_echo_cursor = 1 let g:lsp_diagnostics_float_cursor = 0 let g:lsp_diagnostics_highlights_enabled = 1 let g:lsp_diagnostics_virtual_text_enabled = 0 " TODO: Call this when vim-lsp is enabled else call clang-format. "autocmd! BufWritePre *.c, *.cpp, *.h, *.hpp, *.rs, *.go call execute('LspDocumentFormatSync') " autocmd BufWritePre *.c, *.cpp, *.h, *.hpp, *.rs, *.go LspDocumentFormatSync " refer to doc to add more commands endfunction augroup lsp_install au! " call s:on_lsp_buffer_enabled only for languages that has the server registered. autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() augroup END " Doxygen let g:DoxygenToolkit_briefTag_pre = '' let g:DoxygenToolkit_templateParamTag_pre = '\tparam ' let g:DoxygenToolkit_templateParamTag_post = ' ' let g:DoxygenToolkit_paramTag_pre = '\param ' let g:DoxygenToolkit_paramTag_post = ' ' let g:DoxygenToolkit_returnTag = '\return ' let g:DoxygenToolkit_throwTag_pre = '\throw ' let g:DoxygenToolkit_throwTag_post = ' ' let g:DoxygenToolkit_fileTag = '\file ' let g:DoxygenToolkit_authorTag = '\author ' let g:DoxygenToolkit_dateTag = '\date ' let g:DoxygenToolkit_versionTag = '\version ' let g:DoxygenToolkit_blockTag = '\name ' let g:DoxygenToolkit_classTag = '\class ' " C is /** */, C++ is /// let g:DoxygenToolkit_commentType = 'C' " allow /** \brief Foo */ for e.g. enum doc. let g:DoxygenToolkit_compactOneLineDoc = 'yes' " No empty line between e.g. brief and param section. let g:DoxygenToolkit_compactDoc = 'yes' " use ENTER to generate Doxygen block map :Dox "------------------------------------------------- " Code " Style "------------------------------------------------- " Tip: format paragraph with 'gq' in normal mode. " Line wrap: default to 80 chars, except for python. " TODO if everyone agrees to move to pyls, remove this bit as it's hard-coded. set tw=80 autocmd Filetype python setlocal tw=79 " Line wrap: recognise list alignment. set fo+=n " Line wrap: remove comment leader when joining lines. set fo+=j " Highlight the first 3 characters over 80 character limit. autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#501010 autocmd BufEnter * match OverLength '\%<84v.\%>81v' " Set colour for the vertical line that shows the character limit. highlight ColorColumn ctermbg=Grey guibg=#2d2d2d " Toggle between the vertical line and the highlighting of characters. map cl :call Colorcolumn_highlighting() " Toggle between the vertical line and the highlighting of characters. func! Colorcolumn_highlighting() if &l:colorcolumn ==# 81 setlocal colorcolumn& match OverLength '\%<84v.\%>81v' else setlocal colorcolumn=81 match OverLength /\%1000v.\+/ endif endfunc " Configure cursor line. set cursorline " Disable checks for RST to avoid errors at unknown directives. let g:syntastic_rst_checkers = [] " Set table mode settings for RST. let g:table_mode_corner_corner='+' let g:table_mode_header_fillchar='=' "------------------------------------------------- " Code " Shortcuts "------------------------------------------------- " Open the quickfix window containing lc diagnostics map lco :copen " Close the quickfix window containing lc diagnostics map lcd :cclose "------------------------------------------------- " Code " Tags "------------------------------------------------- map t :TagbarToggle "------------------------------------------------- " Spell checking "------------------------------------------------- " Set spell checking. set spell spelllang=en_us " Disable spell check for some problematic filetypes. autocmd Filetype diff,gitrebase,plantuml,te,yaml setlocal nospell " Toggle spell checking. map ss :setlocal spell! map sl :call Spellcheck_cycle_lang() " Shortcuts for spell checking. map sn ]s " Next misspelled word. map sp [s " Previous mispelled word. map sa zg " Add word to dictionary. map s? z= " List alternative words. "------------------------------------------------- " Functions "------------------------------------------------- " Finds and set the dir containing C/C++ compilation database func! Compilation_database_build_dir_set() let l:db_pre = expand('%:p:h') let l:db_post = '' while !filereadable(l:db_pre . l:db_post . '/compile_commands.json') " probe a potential build dir if filereadable(l:db_pre . l:db_post . '/build/compile_commands.json') let l:db_post = l:db_post . '/build' break endif " otherwise try a directory up let l:db_post = l:db_post . '/..' " Give up after after 10 dirs up (5 + 3 * 10). if strlen(l:db_post) > 35 let l:db_pre = '' let l:db_post = '' break endif endwhile " Simplify the dir path, changing /dir/src/../build to /dir/build let l:db_dir = simplify(l:db_pre . l:db_post) " Stop here if path hasn't changed to avoid language server restart. if exists('s:compdb_dir') && \ s:compdb_dir ==# l:db_dir return 0 endif let s:compdb_dir = l:db_dir " Syntastic. " XXX Is this really needed? Remove and experiment. " TODO vim-lsp already provides their own database-searching function, find if " it can be used to replace this. let g:syntastic_c_clang_tidy_args = '-p=''' . s:compdb_dir . '''' let g:syntastic_cpp_clang_tidy_args = g:syntastic_c_clang_tidy_args endfunc " Removes trailing spaces when saving " http://amix.dk/vim/vimrc.html func! Delete_trailing_spaces() " Do not execute for diff (patch) files, spaces are part of the context. if &filetype ==# 'diff' return 0 endif exe "normal mz" %s/\s\+$//ge exe "normal `z" endfunc " Cycle between spellcheck languages func! Spellcheck_cycle_lang() " If spellchecking is disabled, just enable it only if &spell ==# 0 setlocal spell! echo 'enabled spell checking' return 0 endif if &spelllang ==# 'en_us' let l:lang = 'nl' else let l:lang = 'en_us' endif let &spelllang = l:lang echo 'changed spell checking language to ' . l:lang endfunc