Quick vim tips to make vim more efficient
A compilation of settings to add to your vimrc
This post is a few years old
This post has not been updated for some years. It may no longer be up to date. Opinions may have changed.
Here are some small Vim tweaks I discovered in the past few weeks that makes working with Vim much more efficient. Many of these tips can already be found elsewhere, but these are the ones I found most worthwhile.
Move vertically by visual line
This way, when a line is wrapped, you don’t unexpectedly jump over it when navigating your file.
" move vertically by visual line
nnoremap j gj
nnoremap k gk
Highlight 81st column when needed
Credits go to Damien Conway.
" highlight just the 81st column of wide lines... (Damian Conway)
highlight ColorColumn ctermbg=magenta
call matchadd('ColorColumn', '\%81v', 100)
Open/create folds with the spacebar
This is a really handy setting. You can create folds using a visual selection and spacebar, and toggle them using the spacebar in normal mode.
" Enable code folding
set foldenable
" Use space to toggle folds and create folds
nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
vnoremap <Space> zf
" Set foldmethod to indent and allow manual
augroup vimrc
au BufReadPre * setlocal foldmethod=indent
au BufWinEnter * if &fdm == 'indent' | setlocal foldmethod=manual | endif
augroup END
set foldlevelstart=10 " Open most folds by default
set foldnestmax=10 " 10 nested folds max
Use Ag over grep or Ack
Because Ag is awesome and much faster. Let CtrlP use Ag without caching for up to date results, and define an :Ag command that is mapped to backward slash.
" Use ag over grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects
" .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0
" define :Ag
if !exists(":Ag")
command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
endif
" bind K to grep word under cursor
nnoremap K :silent! grep! "\b<C-R><C-W>\b"<CR>:cw<CR>
" bind \ (backward slash) to grep shortcut
nnoremap \ :Ag<SPACE>
Syntastic: show only icons in the gutter
I found it really annoying that every time there was a small style error in a script, Syntastic opened the location list with the error. Here is how to hide the location list by default and have Syntastic show only an appropriate icon in the gutter.
" Syntastic {{{
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_loc_list_height = 5
let g:syntastic_auto_loc_list = 0
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 1
let g:syntastic_id_checkers = 1
let g:syntastic_aggregate_errors = 1
let g:syntastic_javascript_checkers = ['eslint']
let g:syntastic_python_python_exec = '/usr/bin/python3'
let g:syntastic_python_checkers = ['python', 'pylint']
let g:syntastic_enable_signs = 1
let g:syntastic_error_symbol = '❌'
let g:syntastic_style_error_symbol = '⁉️'
let g:syntastic_warning_symbol = '⚠️'
let g:syntastic_style_warning_symbol = 'ℹ'
highlight link SyntasticErrorSign SignColumn
highlight link SyntasticWarningSign SignColumn
highlight link SyntasticStyleErrorSign SignColumn
highlight link SyntasticStyleWarningSign SignColumn
" }}}
Copy from Vim to clipboard
This lets you use Ctrl+y to copy text from Vim to your clipboard.
" Clipboard shortcuts {{{
nnoremap <C-y> "+y
vnoremap <C-y> "+y
" }}}
Switch CapsLock and Escape
setxkbmap -option caps:swapescape
In Vim, you have to use the Escape key a lot, but it is awkwardly positioned on the keyboard. With this command, you can change the functionality of the CapsLock and Escape keys. This is much better because CapsLock is next to your home row. I tried two other mappings for a time: jk and kj (because jk does occur quite often in Dutch words). However, I found that hitting CapsLock is much faster.
And of course I use a couple of plugins. If you have any cool vim tips to share, please let me know.
P.S.: This post was copied from my old blog, I’ll likely won’t edit it any more.
