Vim Tips and Tricks

Published on 2017-08-06T17:14:47-04:00 by Mohd

Remove the clutter in gVim

http://vim.wikia.com/wiki/Hide_toolbar_or_menus_to_see_more_text

:set guioptions-=m  "remove menu bar
:set guioptions-=T  "remove toolbar
:set guioptions-=r  "remove right-hand scroll bar
:set guioptions-=L  "remove left-hand scroll bar

Remove the clutter in Control-P

set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe,*\\dist\\*  " Windows
let g:ctrlp_custom_ignore = '\v[\/](node_modules|target|dist)|(\.(swp|ico|git|svn|dll))$'
let g:ctrlp_max_files=0
let g:ctrlp_max_depth=40

Jump between windows easier

Control + J, K, L, H

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
nnoremap <C-Q> <C-W><C-Q>

Spell check

Binds leader sp to spell check current buffer. Use :set nospell to turn it off.

nnoremap <leader>sp :setlocal spell spelllang=en_us <cr>

Ignore swp/swo/bak/orig files from git forever!

Create a .gitignore file with the files you want to ignore.

*.swp
*.swo
*.bak
*.orig

Then execute

git config --global core.excludesfile C:\.gitignore

Assuming that the .gitignore file you created resides in C:. That's it!

Nerd Explorer

show hidden files:

let NERDTreeShowHidden=1

shift + I to toggle

Control P

Control + f and Control + b to swap between file search modes

Disable highlighting

:noh

Vimgrep

search for text in files:

:vimgrep /search_term/j ./**/*.extension

Create mapping to search for text under the cursor:

map <F4> :execute "vimgrep /" . expand("<cword>") . "/j **" <Bar> cw<CR>

Filter directories from the search in your vimrc:

set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe,*\\dist\\*,*\\node_modules\\*

Command history

: + up/down - arrow keys to browse through command history

q + : - open command history window

q + / - open search history window

Registers

https://stackoverflow.com/questions/3997078/how-to-paste-yanked-text-into-vim-command-line

Auto-open quick-fix window

augroup myvimrc
    autocmd!
    autocmd QuickFixCmdPost [^l]* cwindow
    autocmd QuickFixCmdPost l*    lwindow
augroup END

Dictionary search

<C-x> <C-k> - complete from the dictionary
set dictionary? - to check current dictionary
set dictionary=/path/to/dictionary - set dictionary

Moving around

http://vim.wikia.com/wiki/Moving_around

Open command prompt

:!cmd
example:
!post.bat
or:
silent !post.bat

The silent keyword makes it so that you don't have to hit a key to close the command window once the command is done executing.

Find and Replace

http://vim.wikia.com/wiki/Search_and_replace

:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:s/foo/bar/g
Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
:%s/\/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.
:%s/foo/bar/gci
Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation.
:%s/foo\c/bar/gc is the same because \c makes the search case insensitive.

Language specific plugins

https://github.com/python-mode/python-mode
https://github.com/fatih/vim-go
https://github.com/ternjs/tern_for_vim

JavaScript

let g:syntastic_javascript_checkers = ['eslint']
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
let g:syntastic_javascript_eslint_exe = 'eslint'

TernDef: Jump to the definition of the thing under the cursor.
TernDoc: Look up the documentation of something.
TernType: Find the type of the thing under the cursor.
TernRefs: Show all references to the variable or property under the cursor.
TernRename: Rename the variable under the cursor.

Vimdiff for merge conflict resolution

https://www.rosipov.com/blog/use-vimdiff-as-git-mergetool/

redraw screen

:redraw!