[vim] Misc improvements to vimrc

* More verbose comments for obscure features
* Shorten .viminfo memory for privacy/security purposes
* Setup .viminfo for neovim
* Remove unused Q mapping
* Change ReflowParagraph() to gwip -> conserve cursor position
* Fix trailing spaces cleanup so that it doesn't pollute the undo tree
* Fix header/source swap function to work with both C and C++
This commit is contained in:
lara 2020-12-23 17:10:05 +01:00
parent a67751e67c
commit d04a1766d2
2 changed files with 49 additions and 23 deletions

View file

@ -24,6 +24,9 @@ fi
if which nvim > /dev/null 2>&1; then
alias vim='nvim'
fi
if [ -f "$HOME/howto/how_to" ]; then
alias howto="vim $HOME/howto/how_to"
fi
case $(uname -s) in
Arch)
alias redwm='cd ~/aur/dwm-git; updpkgsums; makepkg -fi --noconfirm; killall dwm'

69
vimrc
View file

@ -58,6 +58,7 @@ if has("unix") || has("mac")
Plugin 'tikhomirov/vim-glsl'
Plugin 'jamessan/vim-gnupg'
Plugin 'romainl/vim-cool'
Plugin 'machakann/vim-highlightedyank'
let g:highlightedyank_highlight_duration = 200
"[Needed only with old vim versions]"
@ -77,9 +78,10 @@ set number
set relativenumber
"[Hide/show the white-space and more invisible symbols]"
set list
set listchars=nbsp,trail:-
"set listchars+=tab:│\ ,
set listchars+=tab:▸\ ,
set listchars=tab:▸\ ,nbsp,trail:-
" | | + Trailing spaces
" | + Non breakable spaces
" + Tabulations: `tab xy`, x: first char, y: following chars
set nojoinspaces
"[Indent & Tab/mode-line settings]"
set breakindent
@ -106,7 +108,6 @@ set textwidth=0
set modeline
set modelines=1
set history=200 " keep 200 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set laststatus=2
@ -115,6 +116,8 @@ set noshowmode
"[Command mode autocompletion]"
set wildmenu
set wildmode=longest:full,full
" `------------|---- First complete till longest common string, open wildmenu
" `---- Then, complete next full match
set ttimeout " time out for key codes
set ttimeoutlen=100 " wait up to 100ms after Esc for special key
@ -126,6 +129,20 @@ set history=1000
set nowritebackup
set undolevels=5000
"[Setup history file]"
set viminfo=%,<0,'10,/16,:16,h,f0
" | | | | | | + file marks 0-9,A-Z 0=NOT stored
" | | | | | + disable 'hlsearch' loading viminfo
" | | | | + command-line history saved
" | | | + search history saved
" | | + files marks saved
" | + lines saved each register (old name for <, vi6.2): NOT STORED
" + save/restore buffer list
if !has('nvim')
"[Declutter $HOME]"
set viminfo+=n~/.vim/cache/.viminfo
endif
" Show @@@ in the last line if it is truncated.
set display=truncate
"[Splitting rules]"
@ -202,8 +219,6 @@ nnoremap <leader>o o<Esc>
nnoremap <leader>O O<Esc>
"[Clear search highlights]"
nnoremap // :nohlsearch<CR>
" Don't use Ex mode, use Q for formatting.
map Q gq
"[Reflow current paragraph]"
"[http://stevelosh.com/blog/2010/09/coming-home-to-vim/]"
nnoremap <silent> <leader>q :call ReflowParagraph()<CR>
@ -242,14 +257,15 @@ endif
"[Reflow current paragraph]"
function! ReflowParagraph()
let l:view = winsaveview()
normal gqip
normal gwip
call winrestview(l:view)
endfunction
"[Remove tabs and spaces at the end of lines]"
function! DeleteTrailingTWS()
if &ft =~ 'diff'
"[Do not clean up trailing spaces in binary mode or in diff files]"
if &binary || &ft =~ 'diff'
return
end
endif
let l:view = winsaveview()
silent %s/[ \t]*$//g
silent %s/\s\+$//ge
@ -268,8 +284,12 @@ endfunction
function! SwapExtension()
let [rest, ext] = [expand('%:r'), expand('%:e')]
if ext ==? 'h'
let ext = 'cpp'
elseif ext ==? 'cpp'
if filereadable(rest . '.c')
let ext = 'c'
elseif filereadable(rest . '.cpp')
let ext = 'cpp'
endif
elseif ext ==? 'cpp' || ext ==? 'c'
let ext = 'h'
"swap between vertex and fragment shader"
elseif ext ==? 'vsh'
@ -283,10 +303,9 @@ endfunction
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
" Enable file type detection. Use the default filetype settings, so that
" mail gets 'tw' set to 72, 'cindent' is on in C files, etc. Also load
" indent files, to automatically do language-dependent indenting.
" Revert with ":filetype off".
filetype plugin indent on
@ -300,16 +319,20 @@ if has("autocmd")
" (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
\ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' |
\ exe "normal! g`\"" |
\ endif
augroup END
"
"[Do not clean up trailing spaces in binary mode]"
if !&binary
autocmd BufWritePre * call DeleteTrailingTWS()
endif
"[Try not to pollute the undo tree (https://vi.stackexchange.com/a/13401)]"
autocmd BufWritePre *
\ try |
\ undojoin |
\ catch /^Vim\%((\a\+)\)\=:E790/ |
\ finally |
\ call DeleteTrailingTWS() |
\ endtry
if has("unix") || has("mac")
autocmd BufWritePost * call ChangeScriptMode()