dotfiles

Config files from my daily system
Index Commits Files Refs README
.config/nvim/init.vim (10543B)
   1 "                              _
   2 "         _ __   ___  _____   _(_)_ __ ___
   3 "        | '_ \ / _ \/ _ \ \ / / | '_ ` _ \
   4 "        | | | |  __/ (_) \ V /| | | | | | |
   5 "        |_| |_|\___|\___/ \_/ |_|_| |_| |_|
   6 "
   7 "        by github.com/mjkloeckner
   8 
   9 " Plugins call
  10     call plug#begin('~/.config/nvim/plugged')
  11 
  12     Plug 'PhilRunninger/nerdtree-visual-selection'
  13     Plug 'alvan/vim-closetag'
  14     Plug 'ayosec/hltermpaste.vim'
  15     Plug 'chrisbra/Colorizer'
  16     Plug 'christoomey/vim-tmux-navigator'
  17     Plug 'flazz/vim-colorschemes'
  18     Plug 'flw-cn/vim-nerdtree-l-open-h-close'
  19     Plug 'hattya/vcs-info.vim'
  20     Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': 'markdown' }
  21     Plug 'junegunn/vim-easy-align'
  22     Plug 'justinmk/vim-syntax-extra'
  23     Plug 'mbbill/undotree'
  24     Plug 'preservim/nerdtree'
  25     Plug 'ryanoasis/vim-devicons'
  26     Plug 'sainnhe/edge'
  27     Plug 'sainnhe/gruvbox-material'
  28     Plug 'skywind3000/asyncrun.vim'
  29     Plug 'tikhomirov/vim-glsl'
  30     Plug 'tpope/vim-commentary'
  31     Plug 'tpope/vim-surround'
  32     Plug 'tpope/vim-fugitive'
  33     Plug 'vim-python/python-syntax'
  34 
  35     call plug#end()
  36 
  37 " Basic settings
  38     syntax on
  39     set encoding=utf-8
  40     scriptencoding utf-8
  41     set fileencoding=utf-8
  42     set noemoji
  43     set tabstop=4
  44     set shiftwidth=4
  45     set scrolloff=5
  46     set path+=./**
  47     set lcs+=space:ยท
  48     set iskeyword-=(,)
  49     set wildmenu
  50     set incsearch
  51     set smartcase
  52     " set noshowmode
  53     set number
  54     set relativenumber
  55     set cursorline
  56     set mouse=a
  57     set undofile
  58     set undodir=~/.cache/nvim/undo
  59     set dir=~/.cache/nvim/swap
  60     set backupdir=~/.cache/nvim/backup
  61     set clipboard=unnamedplus
  62 
  63     " Enable blinking together with different cursor shapes for insert/command mode, and cursor highlighting:
  64     set guicursor=n-v:block,i-ci-ve-c:ver25,r-cr:hor20,o:hor50
  65     \,a:blinkwait700-blinkoff4000-blinkon2500-Cursor/lCursor
  66     \,sm:block-blinkwait175-blinkoff150-blinkon175
  67 
  68     " Set colorscheme and transparent background
  69     function! AdaptColorscheme()
  70         hi Normal guibg=NONE ctermbg=NONE
  71         hi LineNr guibg=NONE ctermbg=NONE
  72         hi Folded guibg=NONE ctermbg=NONE
  73         hi EndOfBuffer guibg=NONE ctermbg=NONE ctermfg=12
  74         hi SpecialKey guibg=NONE ctermbg=NONE
  75         hi VertSplit guibg=NONE ctermbg=NONE
  76         hi SignColumn guibg=NONE ctermbg=NONE
  77         hi TabLineSel ctermfg=LightGreen ctermbg=NONE
  78         hi TabLineFill ctermfg=Gray ctermbg=NONE
  79         hi Title ctermfg=LightBlue ctermbg=NONE
  80     endfunction
  81 
  82     function! DarkTheme()
  83         set bg=dark
  84         let g:gruvbox_contrast_dark='hard'
  85         colorscheme gruvbox
  86         call AdaptColorscheme()
  87         hi CursorLine cterm=NONE ctermbg=235
  88     endfunction
  89 
  90     function! LightTheme()
  91         set bg=light
  92         let g:gruvbox_contrast_light='hard'
  93         colorscheme gruvbox
  94         call AdaptColorscheme()
  95         " hi CursorLine cterm=NONE ctermbg=bg5
  96     endfunction
  97 
  98     hi TrailingWhitespace ctermbg=yellow guibg=yellow
  99     call matchadd("TrailingWhitespace", '\v\s+$')
 100 
 101     if filereadable('/home/mk/.config/scheme/light')
 102         call LightTheme()
 103     elseif filereadable('/home/mk/.config/scheme/dark')
 104         call DarkTheme()
 105     endif
 106 
 107 " Key remaps
 108     let mapleader =' '
 109     map <leader>f :NERDTreeToggle<CR>
 110     map <leader>F :NERDTreeFocus<CR>
 111     map <leader>r :NERDTreeFind<CR>
 112 
 113     map <leader>s :source $HOME/.config/nvim/init.vim<CR>
 114 
 115     " Disable ex-mode keybinding (type visual thing)
 116     map Q <NOP>
 117 
 118     " Improves indenting chunks of code with '<' '>' keys
 119     vnoremap < <gv
 120     vnoremap > >gv
 121 
 122     " Type a replacement term and press . to repeat the replacement again. Useful
 123     " for replacing a few instances of the term (comparable to multiple cursors).
 124     nnoremap <silent> s* :let @/='\<'.expand('<cword>').'\>'<CR>cgn
 125     xnoremap <silent> s* "sy:let @/=@s<CR>cgn
 126 
 127     " Stops highlight after a search hitting return;
 128     nnoremap <silent> <CR> :nohlsearch<CR>
 129 
 130     " Shortcut for split navigation
 131     noremap <C-Down> <C-W>j<CR>
 132     noremap <C-Up> <C-W>k<CR>
 133     noremap <C-Left> <C-W>h<CR>
 134     noremap <C-Right> <C-W>l<CR>
 135 
 136     " Move between windows with Ctrl + hjkl
 137     noremap <C-h> <C-w>h
 138     noremap <C-j> <C-w>j
 139     noremap <C-k> <C-w>k
 140     noremap <C-l> <C-w>l
 141 
 142     " Go to next/prev quickfix
 143     nnoremap <C-n> :cnext<CR>zz
 144     nnoremap <C-p> :cprev<CR>zz
 145 
 146     " Go to tab by number
 147     noremap <leader>1 1gt
 148     noremap <leader>2 2gt
 149     noremap <leader>3 3gt
 150     noremap <leader>4 4gt
 151     noremap <leader>5 5gt
 152     noremap <leader>6 6gt
 153     noremap <leader>7 7gt
 154     noremap <leader>8 8gt
 155     noremap <leader>9 9gt
 156     noremap <leader>0 :tablast<cr>
 157 
 158     " Start interactive EasyAlign in visual mode (e.g. vipga)
 159     xmap ga <Plug>(EasyAlign)
 160 
 161     " Start interactive EasyAlign for a motion/text object (e.g. gaip)
 162     nmap ga <Plug>(EasyAlign)
 163 
 164     " Highlight whitespace problems.
 165     " flags is '' to clear highlighting, or is a string to
 166     " specify what to highlight (one or more characters):
 167     "   e  whitespace at end of line
 168     "   i  spaces used for indenting
 169     "   s  spaces before a tab
 170     "   t  tabs not at start of line
 171     function! ShowWhitespace(flags)
 172       let bad = ''
 173       let pat = []
 174       for c in split(a:flags, '\zs')
 175         if c == 'e'
 176           call add(pat, '\s\+$')
 177         elseif c == 'i'
 178           call add(pat, '^\t*\zs \+')
 179         elseif c == 's'
 180           call add(pat, ' \+\ze\t')
 181         elseif c == 't'
 182           call add(pat, '[^\t]\zs\t\+')
 183         else
 184           let bad .= c
 185         endif
 186       endfor
 187       if len(pat) > 0
 188         let s = join(pat, '\|')
 189         exec 'syntax match ExtraWhitespace "'.s.'" containedin=ALL'
 190       else
 191         syntax clear ExtraWhitespace
 192       endif
 193       if len(bad) > 0
 194         echo 'ShowWhitespace ignored: '.bad
 195       endif
 196     endfunction
 197 
 198     function! ToggleShowWhitespace()
 199       if !exists('b:ws_show')
 200         let b:ws_show = 0
 201       endif
 202       if !exists('b:ws_flags')
 203         let b:ws_flags = 'est'  " default (which whitespace to show)
 204       endif
 205       let b:ws_show = !b:ws_show
 206       if b:ws_show
 207         call ShowWhitespace(b:ws_flags)
 208       else
 209         call ShowWhitespace('')
 210       endif
 211     endfunction
 212 
 213     nnoremap <Leader>ws :call ToggleShowWhitespace()<CR>
 214     highlight ExtraWhitespace ctermbg=red guibg=red
 215 
 216     nnoremap <A-j> :m .+1<CR>==
 217     nnoremap <A-k> :m .-2<CR>==
 218 
 219     nnoremap <Leader>/ :vsplit<CR>
 220     nnoremap <Leader>" :split<CR>
 221 
 222     inoremap <A-j> <Esc>:m .+1<CR>==gi
 223     inoremap <A-k> <Esc>:m .-2<CR>==gi
 224 
 225     vnoremap <A-j> :m '>+1<CR>gv=gv
 226     vnoremap <A-k> :m '<-2<CR>gv=gv
 227 
 228     nnoremap n nzzzv
 229     nnoremap N Nzzzv
 230 
 231     function! ToggleQuickFix()
 232         if empty(filter(getwininfo(), 'v:val.quickfix'))
 233             copen
 234         else
 235             cclose
 236         endif
 237     endfunction
 238 
 239     nnoremap <silent> <leader>c :call ToggleQuickFix()<cr>
 240 
 241     set laststatus=1
 242     autocmd VimEnter * set laststatus=1
 243 
 244     let NERDTreeHijackNetrw=1
 245     let NERDTreeMinimalUI=1
 246 
 247     " Highjack nerdtree's highjacking to keep normal nerdtree from loading on directories
 248     " let g:NERDTreeHijackNetrw=0
 249     " augroup NERDTreeHijackNetrw
 250     " autocmd VimEnter * silent! autocmd! FileExplorer
 251     " augroup END
 252 
 253     " autocmd VimEnter * call CloseExtraNERDTree()
 254 
 255     " If vim is started with a file, simply show that file
 256     " If vim is started with nothing or a directory close extra NERDTree buffer
 257     " function CloseExtraNERDTree()
 258     "   wincmd l " move to right pane
 259     "   let l:main_bufnr = bufnr('%')
 260     "   let l:fname = expand('%') " get name of current buffer
 261     "   if l:fname ==# 'NERD_tree_1'
 262     "   exe bufwinnr(l:main_bufnr) . "wincmd w"
 263     "   bd
 264     "   endif
 265     " endfunction
 266 
 267     " Start NERDTree when Vim is started without file arguments.
 268     " autocmd StdinReadPre * let s:std_in=1
 269     " autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | only | endif
 270 
 271     function VerboseMark()
 272         echo ''
 273         let c = getchar()
 274         if ((c >= 0x41) && (c <= 0x5A)) || ((c >= 0x61) && (c <= 0x7A))
 275             exe "mark" nr2char(c)
 276             echon "Created mark at key '" nr2char(c) "' in line " line(".")
 277         else
 278             return
 279         endif
 280     endfunction
 281 
 282     let g:asyncrun_open = 12
 283 
 284     " Pressing Ctrl-C when input prompt breaks highlight
 285     cnoremap <C-c> <Esc>
 286 
 287     " Oposite of <Shift-k>
 288     nnoremap K :m-2<CR>J
 289 
 290     function Compile()
 291         echohl Question
 292         let l:input = split(input("Compile command: ", &makeprg, "filetype"), ' ')
 293         echohl None
 294 
 295         if empty(l:input)
 296             return
 297         endif
 298         redraw
 299 
 300         if(!empty(matchstr(l:input, "%")))
 301             if(empty(expand('%')))
 302                 echohl Question
 303                 echo 'Filename empty'
 304                 echohl None
 305                 return
 306             endif
 307         endif
 308 
 309         let string = join(l:input, ' ')
 310         " exe "set makeprg = " .. string
 311         let &makeprg = string
 312         let l:s = &makeprg
 313         exe "AsyncRun " .. l:s
 314         mod
 315     endfunction
 316 
 317     " autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif
 318     function SearchHelp()
 319         echohl Question
 320         let l:input = substitute(input("Search vim help: ", "", "help"), "\\\'\\\|\\\"", '', 'g')
 321         echohl None
 322         mod
 323 
 324         if empty(l:input)
 325             return
 326         endif
 327 
 328         let l:error = execute('silent help ' .. l:input)
 329 
 330         if !empty(l:error)
 331             echohl WarningMsg
 332             echon "No help found for: '" .. l:input "'."
 333             echohl None
 334         else
 335             exe "wincmd T | set relativenumber"
 336         endif
 337     endfunction
 338 
 339     " autocmd BufEnter *.txt if &filetype == 'man' | wincmd T | endif
 340     function SearchMan()
 341         echohl Question
 342         " let l:input = input("Search man page: ")
 343         let l:input = substitute(input("Search man page: "), "\\\'\\\|\\\"", '', 'g')
 344         echohl None
 345         mod
 346 
 347         if empty(l:input)
 348             return
 349         endif
 350 
 351         " let l:string = join(l:input, ' ')
 352         let l:cmd = "man " .. l:input .. " > /dev/null 2>&1"
 353         call system(l:cmd)
 354         if v:shell_error == 0
 355             exe "Man " .. l:input
 356             exe "wincmd T | set relativenumber"
 357         else
 358             echohl WarningMsg
 359             echon l:input .. ": nothing appropriate."
 360             echohl None
 361         endif
 362     endfunction
 363 
 364     function Search()
 365         echohl Question
 366         let l:input = split(input("Search keyword in CWD: "), ' ')
 367         echohl None
 368         if empty(l:input)
 369             return
 370         endif
 371         mod
 372 
 373         let l:string = join(l:input, ' ')
 374         echo l:string
 375         exe "silent grep -r -I -s --exclude-dir=.git " .. l:string .. " *"
 376         copen
 377     endfunction
 378 
 379     nnoremap <Leader>C :call Compile()<CR>
 380     nnoremap <Leader>v :call Search()<CR>
 381     nnoremap <Leader>m :call SearchMan()<CR>
 382     nnoremap <Leader>h :call SearchHelp()<CR>
 383 
 384     function Text()
 385         set tw=80
 386         set colorcolumn=80
 387         set spelllang=es
 388     endfunction
 389 
 390     let g:mkdp_browser = 'qutebrowser'
 391 
 392     nnoremap <Leader>t :call Text()<CR>
 393     nnoremap m :call VerboseMark()<CR>
 394 
 395     " close if quickfix is the last window
 396     au BufEnter * call MyLastWindow()
 397     function! MyLastWindow()
 398       " if the window is quickfix go on
 399       if &buftype=="quickfix"
 400         " if this window is last on screen quit without warning
 401         if winbufnr(2) == -1
 402           quit!
 403         endif
 404       endif
 405     endfunction
 406 
 407     " Switch to last-active tab
 408     if !exists('g:Lasttab')
 409         let g:Lasttab = 1
 410         let g:Lasttab_backup = 1
 411     endif
 412     autocmd! TabLeave * let g:Lasttab_backup = g:Lasttab | let g:Lasttab = tabpagenr()
 413     autocmd! TabClosed * let g:Lasttab = g:Lasttab_backup
 414     nmap <silent> <Leader>; :exe "tabn " . g:Lasttab<cr>
 415 
 416     au TextYankPost * silent! lua vim.highlight.on_yank()
 417     let g:python_highlight_all = 1
 418 
 419     " delete word in command/prompt mode with Ctrl + Backspace
 420     cnoremap <C-H> <C-w>