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