Jim Cheung

Vim

Moving Around

word

line

sentence and paragraph

screen

Ctags

set tags=./tags;/

Add these lines in vimrc

map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>

After the tags are generated. You can use the following keys to tag into and tag out of functions:

vimgrep

:vim[grep][!] /{pattern}/[g][j] {file}

recursive search

use ** in file pattern to search recursively

:vimgrep /findme/j ../**/*.js

search using word under cursor

use <cword> for word under cursor, and % for current file

view results

back to original file

C-^ will do

disable noautocmd to increase search speed

:noautocmd vimgrep /{pattern}/[flags] {file(s)}

:e to reload file in order to enable syntax highlighting (disabled by :noautocmd)

map as shortcuts

nnoremap <leader>nav :vimgrep /function /gj % <esc>:cw<CR>
nnoremap <leader>* :execute 'vimgrep /'.expand('<cword>').'/gj '.expand('%') <esc>:cw<CR>

auto format

js-beautify

an easy way to re-format js files. download js-beaufify, i use php version.

put few lines to support read from stdin :

$opts = new BeautifierOptions();
$opts->indent_size = 2;
$jsb = new JSBeautifier();
echo $jsb->beautify(file_get_contents('php://stdin'), $opts);

add #!/usr/local/bin/php on first line and chmod +x jsbeautifier.php makes it executable.

test it by

> cat sample.js | ./jsbeautifier.php

when editing js files, to replace current buffer with re-formatted text:

:%!/path/to/jsbeautifier.php

you can do the same with other formatter: tidy, astyle, phpCB, autopep8

Chiel92/vim-autoformat is a vim bundle to do it but I failed installing it.