Vim

Moving Around

word

  • w: forward to beginning of a word
  • b: backward to beginning of a word
  • e: to the end of a word
  • 3w forward 3 words.

line

  • $: to end of line
  • 0: to beginning of line
  • ^: to first non-blank character of the line

sentence and paragraph

  • ): forward one sentence
  • (: backward one sentence
  • }: forward one paragraph
  • {: backward one paragraph

screen

  • H: top of screen
  • M: middle of screen
  • L: bottom of screen

Ctags

set tags=./tags;/
  • C-] - go to definition
  • C-T - Jump back from the definition.
  • C-W C-] - Open the definition in a horizontal split
  • g C-] - opens a quick dialog to select one between multiple definitions

Add these lines in vimrc

map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>
  • `C-‘ - Open the definition in a new tab
  • A-] - Open the definition in a vertical split

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

  • Ctrl-Left_MouseClick - Go to definition
  • Ctrl-Right_MouseClick - Jump back from definition

vimgrep

:vim[grep][!] /{pattern}/[g][j] {file}
  • g return all matches
  • j specifies vim will not jump to first match autometically

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

  • cw[indow], copen to open result window
  • cn[ext], cp[revious] to jump between results
  • cnf[ile], cpf[ile] jump between files
  • cr[ewind], cla[st] to go beginning or end of quickfix list
  • col[der], cnew[er] to iterate through historical quickfix list

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.

Search Blog: