1
0
vim/vimrc
2022-11-08 09:54:39 +01:00

784 lines
18 KiB
VimL

" TODO
" - Make :Mark optional
" - https://github.com/christoomey/vim-sort-motion
" - https://github.com/kana/vim-textobj-user/wiki
" - https://github.com/kana/vim-textobj-indent
" - https://vimawesome.com/plugin/vim-operator-surround
" - https://vimawesome.com/?q=fugitive
" + Gstatus mapping
" - https://github.com/octol/vim-cpp-enhanced-highlight
""" README {{{1
"""""""""""""""""""""""
"" Post-Git {{{2
" After cloning the root repository
" git submodule init
" git submodule update --init --recursive pack/*/start
" git submodule update --init --recursive pack/*/opt
" After upgrading the repository
" git submodule sync
" git submodule update
" git submodule update --init --recursive pack/*/start
"
" To see empty modules (ZSH globbing)
" ls -1d pack/*/*/*(^F)
" git submodule update --init --recursive pack/*/opt
"
" To update submodules to latest origin commits
" git submodule foreach 'git fetch; echo "\n\n"'
" git submodule foreach 'LANG=C git status | egrep --color=always "(up to date|behind)"; echo -n " "; git branch -r | grep HEAD; echo "\n\n"'
" git submodule foreach 'git merge FETCH_HEAD; echo "\n\n"'
" To create all doc indexes
" :helptags ALL
"" Debug {{{2
" To see all loaded VIM scripts
" :scriptnames
" To only see some of the loaded VIM scripts
" :filter /opt/ scriptnames
" To edit a script from its number
" :script 3
""" Helper functions {{{1
"""""""""""""""""""""""
function ToggleFlag(option,flag)
exec ('let lopt = &' . a:option)
if lopt =~ (".*" . a:flag . ".*")
exec ('set ' . a:option . '-=' . a:flag)
else
exec ('set ' . a:option . '+=' . a:flag)
endif
endfunction
" https://vim.fandom.com/wiki/Faster_loading_of_large_files
function! LargeFile()
" no syntax highlighting etc
set eventignore+=FileType
" save memory when other file is viewed
setlocal bufhidden=unload
" is read-only (write with :w new_filename)
setlocal buftype=nowrite
" no undo possible
setlocal undolevels=-1
" display message
autocmd VimEnter * echo "The file is larger than " . (g:LargeFile / 1024 / 1024) . " MB, so some options are changed (see .vimrc for details)."
endfunction
""" Directories {{{1
"""""""""""""""""""""""
if getcwd() == $HOME
" Move away from home folder to speed-up call to CtrlP...
cd $HOME/tmp
endif
" ** From the Arch Linux global vimrc **
" Move temporary files to a secure location to protect against CVE-2017-1000382
if exists('$XDG_CACHE_HOME')
let &g:directory=$XDG_CACHE_HOME
else
let &g:directory=$HOME . '/.cache'
endif
let &g:undodir=&g:directory . '/vim/undo//'
let &g:backupdir=&g:directory . '/vim/backup//'
let &g:directory.='/vim/swap//'
" Create directories if they doesn't exist
if ! isdirectory(expand(&g:directory))
silent! call mkdir(expand(&g:directory), 'p', 0700)
endif
if ! isdirectory(expand(&g:backupdir))
silent! call mkdir(expand(&g:backupdir), 'p', 0700)
endif
if ! isdirectory(expand(&g:undodir))
silent! call mkdir(expand(&g:undodir), 'p', 0700)
endif
""" VIM Options {{{1
"""""""""""""""""""""""
" Note: Options are sorted as in the :options window
" Default settings from Bram {{{2
source $VIMRUNTIME/defaults.vim
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
augroup END
" https://vim.fandom.com/wiki/Faster_loading_of_large_files
" file is large from 10mb
let g:LargeFile = 1024 * 1024 * 10
augroup LargeFile
au!
autocmd BufReadPre * let f=getfsize(expand("<afile>")) | if f > g:LargeFile || f == -2 | call LargeFile() | endif
augroup END
" 2 - moving around, searching and patterns {{{2
set whichwrap=b,s,<,>,[,]
set nowrapscan
set ignorecase
set smartcase
" 4 - displaying text {{{2
set nowrap
set list
set listchars=tab:→\ ,trail,nbsp
set relativenumber
set numberwidth=2
" 5 - syntax, highlighting and spelling {{{2
"if has("gui_running")
"endif
set background=light
set hlsearch
colorscheme morning
highlight Cursor guifg=#000000 guibg=#FF0058
highlight StatusLine guifg=#dd1234 guibg=#ffffff
set spell
set spelllang=en_gb
" 6 - multiple windows {{{2
set hidden
set splitright
set splitbelow
" 9 - using the mouse {{{2
set mousehide
" 10 - GUI {{{2
if has("gui_running")
" Menu
set guioptions-=m
map <silent> <F10> :call ToggleFlag("guioptions","m")<CR>
" Toolbar
set guioptions-=T
" No scrollbars
set guioptions-=rL
set guiheadroom=0
endif
" 13 - selecting text {{{2
" set clipboard+=unnamedplus
" Doesn't work as expected with Yoink & Subversive
" 14 - editing text {{{2
" Flags that tell how automatic formatting works (see help fo-table)
" o: Automatically insert the current comment leader after hitting 'o' or 'O' in Normal mode
set formatoptions-=o
set nrformats+=alpha
" 15 - tabs and indenting {{{2
set shiftwidth=2
set expandtab
" 16 - folding {{{2
set foldmethod=marker
set foldlevel=1
" 19 - reading and writing files {{{2
set backup
set undofile
" 21 - command line editing {{{2
set wildignore+=*.o
" 23 - quickfix {{{2
" External grep
"
" Tutorial: https://phelipetls.github.io/posts/extending-vim-with-ripgrep/
"
if executable("rg")
set grepprg=rg\ --vimgrep
set grepformat=%f:%l:%c:%m
" Quickfix
" :gr[ep]
" :grepa[dd]
" Location
" :lgr[ep][!]
" Unimpaired shortcuts:
" [^Q ]^Q [Q ]Q [q ]q (file/first/previous)
" Same with L
endif
" 26 - various {{{2
set gdefault " use the 'g' flag for ":substitute"
""" Key Mapping {{{1
"""""""""""""""""""""""
" To test whether a mapping is already used: try the command with no {rhs}
" See :map-special-keys, :map-special-chars & <Char>
" To see all mappings into a buffer:
" redir @a | silent nmap <leader> | redir END | vnew | put a | let @a="" | sort r /\S\+$/
"" For French AZERTY keyboard {{{2
""
let mapleader="²"
" Use with <leader>
nmap ( [
nmap ) ]
omap ( [
omap ) ]
xmap ( [
xmap ) ]
map è `
map èè ``
"" Generic {{{2
""
" Movements
map <space> <c-F>
inoremap <c-h> <left>
inoremap <c-j> <down>
inoremap <c-k> <up>
inoremap <c-l> <right>
noremap j gj
noremap k gk
nmap Q gq$
nmap Y y$
map g<c-t> :tabclose<cr>
noremap <c-w>w <c-w>W
nnoremap <F1> :vert help <c-R><c-W>
vnoremap <F1> y:vert help <c-R>"
nnoremap <leader><F1> :tab help <c-R><c-W>
vnoremap <leader><F1> y:tab help <c-R>"
nnoremap <S-F1> :NERDTree ~/.vim<cr>
nnoremap <C-S-F1> :tabedit ~/.vim/vimrc<cr>
"" External Tools {{{2
""
noremap <leader>k :py3f /usr/share/clang/clang-format-14/clang-format.py<cr>
"" Operator-pending {{{2
" (see Subversive module)
" ie = inner entire buffer
onoremap ie :exec "normal! ggVG"<cr>
" iv = current viewable text in the buffer
onoremap iv :exec "normal! HVL"<cr>
" vim-swap package: select "swappable" items
omap i, <Plug>(swap-textobject-i)
xmap i, <Plug>(swap-textobject-i)
omap a, <Plug>(swap-textobject-a)
xmap a, <Plug>(swap-textobject-a)
"" Abbreviations {{{2
""
"cabbr <expr> %% expand('%:p:h')
" Use %:h<tab> and learn more!
""" File types {{{1
"""""""""""""""""""""""
let g:markdown_folding=1
""" Base Packages {{{1
"""""""""""""""""""""""
" This section contains packages that extend VIM without changing its basic
" behaviour so that an advanced user shouldn't be (too) disappointed.
"" CtrlP {{{2
""
"" Full path fuzzy file, buffer, mru, tag, ... finder
let g:ctrlp_cmd = 'CtrlPBuffer'
let g:ctrlp_prompt_mappings = { 'ToggleMRURelative()': ['<F2>'] }
let g:ctrlp_switch_buffer = 'et'
let g:ctrlp_max_depth = 5
" \v === Very Magic
let g:ctrlp_custom_ignore = {
\ 'dir': '\v\/(build|\.git$)',
\ 'file': '\v\.o$'
\ }
" From Mixed mode, C-F moves to Files & Ctrl-B moves to Buffers
let g:ctrlp_types = ['fil', 'mru', 'buf']
" MRU options
let g:ctrlp_tilde_homedir = 1
let g:ctrlp_mruf_relative = 0
"" NERDTree {{{2
""
"" File system explorer
nnoremap <silent> <C-N> :NERDTreeFocus<cr>
nnoremap <silent> <leader>n :NERDTreeFind<cr>
nnoremap <silent> <leader><C-N> :NERDTreeMirror<cr>
let NERDTreeChDirMode=2
let NERDTreeNaturalSort=0
let NERDTreeQuitOnOpen=1
let NERDTreeSortOrder=['\/$', '*', '\.swp$', '\~$']
"" Secure Modelines {{{2
""
"" Secure alternative to Vim modelines
set nomodeline
let g:secure_modelines_verbose=1
let g:secure_modelines_allowed_items = [
\ "list", "nolist",
\ "foldlevel", "fdl",
\
\ "textwidth", "tw",
\ "softtabstop", "sts",
\ "tabstop", "ts",
\ "shiftwidth", "sw",
\ "expandtab", "et", "noexpandtab", "noet",
\ "filetype", "ft",
\ "foldmethod", "fdm",
\ "readonly", "ro", "noreadonly", "noro",
\ "rightleft", "rl", "norightleft", "norl",
\ "cindent", "cin", "nocindent", "nocin",
\ "smartindent", "si", "nosmartindent", "nosi",
\ "autoindent", "ai", "noautoindent", "noai",
\ "spell", "nospell",
\ "spelllang", "spl"
\ ]
"" Sensible {{{2
""
"" Universal set of defaults
"" Repeat {{{2
""
"" Enable repeating supported plugin maps with "."
""" Extra Packages {{{1
"""""""""""""""""""""""
" This section contains packages that:
" - change VIM behaviour (and could mislead an advanced user);
" - a user could live without.
"" Asterisk {{{2
""
"" Improved star motions
map * <Plug>(asterisk-z*)
map g* <Plug>(asterisk-gz*)
map # <Plug>(asterisk-z#)
map g# <Plug>(asterisk-gz#)
" z = don't move
"" Cutlass {{{2
""
"" TODO: Check origin repository
"" Optional: Redirect all change and delete operations to the black hole register
" Forked from https://github.com/svermeulen/vim-cutlass.git
" - Removed the "d" mappings from vim-cutlass/autoload/cutlass.vim
" since I cannot get used to using an "m" mapping to "cut",
" - Changed "x" to behave like "d", without yanking
"
" Use [Y to "forget" the last cut.
packadd! cutlass
"" Easymotion {{{2
""
"" Simpler way to use some motions
" Use french key "é" as prefix
map é <Plug>(easymotion-prefix)
map éé <Plug>(easymotion-W)
let g:EasyMotion_keys = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
" Use Upper J/K to move to line #0 (lower j/k keep cursor in column)
let g:EasyMotion_startofline = 0
nmap <Plug>(easymotion-prefix)z <Plug>(easymotion-overwin-line)
map <Plug>(easymotion-prefix). <Plug>(easymotion-repeat)
map <Plug>(easymotion-prefix)" <Plug>(easymotion-next)
map <Plug>(easymotion-prefix)& <Plug>(easymotion-prev)
"" Mark {{{2
""
packadd! ingo-library
nmap <leader>* <Plug>MarkSearchAnyNext
nmap <leader># <Plug>MarkSearchAnyPrev
nmap <leader>M <Plug>MarkToggle
nmap <leader>N <Plug>MarkConfirmAllClear
if &t_Co == 256 || has("gui_running")
let g:mwDefaultHighlightingPalette = 'extended'
endif
" The plugin will create the following mappings if they are not already
" assigned to key sequences (and vim will complain when redefining).
"
" <Plug>MarkSet ²m
" <Plug>MarkRegex ²r
" <Plug>MarkClear ²n
" <Plug>MarkSearchCurrentNext ²*
" <Plug>MarkSearchCurrentPrev ²#
" <Plug>MarkSearchAnyNext ²/
" <Plug>MarkSearchAnyPrev ²?
" <Plug>MarkSearchNext *
" <Plug>MarkSearchPrev #
nmap <Plug>IgnoreMark1 <Plug>MarkClear
nmap <Plug>IgnoreMark2 <Plug>MarkSearchCurrentNext
nmap <Plug>IgnoreMark3 <Plug>MarkSearchCurrentPrev
nmap <Plug>IgnoreMark4 <Plug>MarkSearchNext
nmap <Plug>IgnoreMark5 <Plug>MarkSearchPrev
"" Mundo {{{2
""
"" Allow for removing, changing, and adding surroundings.
nnoremap <F5> :MundoToggle<CR>
"" Subversive {{{2
""
"" Optional: Two new operator motions to perform quick substitutions.
packadd! subversive
" subversive-substitute-motion
" Note: Works with yoink plug-in to swap through yanks
" Note: This is shadowing the change character key 's'
" so you will have to use the longer form 'cl'.
"
nmap s <plug>(SubversiveSubstitute)
nmap ss <plug>(SubversiveSubstituteLine)
nmap S <plug>(SubversiveSubstituteToEndOfLine)
" Visual mode substitution
xmap s <plug>(SubversiveSubstitute)
" RTFM: subversive-substitute-over-range-motion
" For range motion, see vim help: text-objects
" See also above in "Key Mapping" — "Operator-pending", in vimrc,
" for new substitution ranges.
" Current text is saved into register W
" Use <c-r>"w to insert the register content or
" use <c-r><c-w> to insert the word under the cursor.
" See vim help: cmdline-history, c_CTRL-R & c_CTRL-R_CTRL-W
let g:subversiveCurrentTextRegister='w'
nmap <leader>s <plug>(SubversiveSubstituteRange)
xmap <leader>s <plug>(SubversiveSubstituteRange)
nmap <leader>ss <plug>(SubversiveSubstituteWordRange)
" subversive-confirming
nmap <leader>c <plug>(SubversiveSubstituteRangeConfirm)
xmap <leader>c <plug>(SubversiveSubstituteRangeConfirm)
nmap <leader>cc <plug>(SubversiveSubstituteWordRangeConfirm)
" subversive-abolish-integration
nmap <leader>S <plug>(SubversiveSubvertRange)
xmap <leader>S <plug>(SubversiveSubvertRange)
nmap <leader>SS <plug>(SubversiveSubvertWordRange)
"" Surround {{{2
""
"" Allow for removing, changing, and adding surroundings.
"" Tabular {{{2
""
"" Optional: Aligning text
" packadd! tabular
"" Unimpaired {{{2
""
"" Provides several pairs of bracket maps
" unimpaired-next next & previous
" unimpaired-lines space & exchange
" unimpaired-toggling Options [ ] y
" unimpaired-encoding encoding & decoding
"" Yoink {{{2
""
"" Optional: History of yanks that you can choose between when pasting.
packadd! yoink
let g:yoinkAutoFormatPaste=0
let g:yoinkIncludeDeleteOperations=1
let g:yoinkMoveCursorToEndOfPaste=1
let g:yoinkSyncSystemClipboardOnFocus=0
" Yank mappings
"nmap y <plug>(YoinkYankPreserveCursorPosition)
"xmap y <plug>(YoinkYankPreserveCursorPosition)
" Note: Using upper-case Y because lower-case are already used by Unimpaired
nmap [Y <plug>(YoinkRotateBack)
nmap ]Y <plug>(YoinkRotateForward)
"" Paste mappings
nmap p <plug>(YoinkPaste_p)
nmap P <plug>(YoinkPaste_P)
nmap <c-j> <plug>(YoinkPostPasteSwapForward)
nmap <c-k> <plug>(YoinkPostPasteSwapBack)
nmap <M-=> <plug>(YoinkPostPasteToggleFormat)
""" Dev Packages {{{1
"""""""""""""""""""""""
" This section contains packages that are mostly useful when developing
" software -- for basic editing of text files they are overkill.
" Doxygen C++ comments
" See https://vim.fandom.com/wiki/Continuing_doxygen_comments
"
" Fold method & initial level
autocmd FileType cpp setlocal comments^=:/// foldmethod=syntax foldlevel=0
"" Abolish {{{2
""
"" Find, substitute, and abbreviate several variations of a word at once.
" :help abolish-coercion
"" ALE {{{2
""
"" Optional: Asynchronous Lint Engine
packadd! ale
let g:ale_linters = {'sh': ['shellcheck']}
let g:ale_linters_explicit=1
"" Commentary {{{2
""
"" C++ {{{2
""
"let g:cpp_function_highlight = 0
"let g:cpp_attributes_highlight = 1
"let g:cpp_member_highlight = 1
"let g:cpp_simple_highlight = 1
"" Direnv {{{2
""
"" Optional: Run direnv's hook appropriately
let g:direnv_cmd = $HOME . '/.asdf/installs/direnv/2.32.1/bin/direnv'
packadd! direnv
"" FSwitch {{{2
""
"" Switch between companion files of source code
let g:fsnonewfiles=1
nnoremap <silent> <leader>oo :FSHere<cr>
nnoremap <silent> <leader>ol :FSRight<cr>
nnoremap <silent> <leader>oL :FSSplitRight<cr>
nnoremap <silent> <leader>oh :FSLeft<cr>
nnoremap <silent> <leader>oH :FSSplitLeft<cr>
nnoremap <silent> <leader>ok :FSAbove<cr>
nnoremap <silent> <leader>oK :FSSplitAbove<cr>
nnoremap <silent> <leader>oj :FSBelow<cr>
nnoremap <silent> <leader>oJ :FSSplitBelow<cr>
augroup fswitch_vimrc
au!
au BufRead */src/*/src/*.cpp let b:fswitchlocs='reg:!\(src/.*/\)src!\1include!'
" ProtoBuf
au BufRead *.proto let b:fswitchdst = 'options' | let b:fswitchlocs = '.'
" RTmaps
" au BufRead *.u/src/*.cpp let b:fswitchlocs='reg:/src/local_interfaces/'
" au BufRead *.u/local_interfaces/*.h* let b:fswitchlocs='reg:/local_interfaces/src/'
augroup END
"" Fugitive {{{2
""
"" Optional: Set of commands defined as a gateway to Git
packadd! fugitive
"" GitGutter {{{2
""
"" git diff in the 'gutter' (sign column).
"" HexDec {{{2
""
"" Optional: Converts numbers under cursor
" packadd! hexdex
"" Indent Guides {{{2
""
"" Optional: Visually displaying indent levels
" packadd! indent-guides
" let g:indent_guides_enable_on_vim_startup=1
" let g:indent_guides_guide_size=1
" let g:indent_guides_start_level=2
"" MediaWiki {{{2
""
"" Optional: Syntax highlighter for MediaWiki-based projects
"packadd mediawiki.vim
"" PlantUML {{{2
""
"" Optional: Plugin for PlantUML syntax
packadd plantuml-syntax
"" Rooter {{{2
""
"" Changes the working directory to the project root
"" Scriptease {{{2
""
"" Optional: Plugin for making/debugging Vim plugins
"packadd! scriptease
"" Sleuth {{{2
""
"" Automatically adjusts 'shiftwidth' and 'expandtab' heuristically
"" Swap {{{2
""
"" Swap delimited items
"" Visual Increment {{{2
""
"" Increase and decrease number or letter sequences via visual mode
" See also
" - https://github.com/nishigori/increment-activator.git
" Increment the list that you have defined.
" - https://github.com/vim-scripts/VisIncr
" Charles Campbell's visual increment plugin
"" Vue.js {{{2
""
"" Optional: Syntax Highlight for Vue.js components
"packadd! vue
"" YouCompleteMe {{{2
""
nnoremap <leader>yd :YcmDiags<cr>
nnoremap <leader>yf :YcmCompleter<space>FixIt<cr>
nnoremap <leader>yg :YcmCompleter<space>GoToImprecise<cr>
nnoremap <leader>yG :YcmCompleter<space>GoToReferences<cr>
nnoremap <leader>yR :YcmCompleter<space>RefactorRename
nnoremap <leader>yy :YcmCompleter<space>GetTypeImprecise<cr>
nnoremap <leader>y= :YcmCompleter<space>GetDocImprecise<cr>
nnoremap <C-W>yg :split<bar>YcmCompleter<space>GoToImprecise<cr>
let g:ycm_always_populate_location_list = 1
let g:ycm_auto_hover = ''
let g:ycm_autoclose_preview_window_after_insertion = 0
let g:ycm_complete_in_strings = 0
let g:ycm_error_symbol = '!>'
let g:ycm_warning_symbol = ':>'
" }}}1
" Tim Pope's sensible.vim is loaded after vimrc.
"
" vim:ts=50:fdm=marker:foldlevel=1:nolist