Advanced VIM

Text Objects

word

WORD

The difference between word is that WORD is everything delimited by spaces and word can be a piece of chars, a dot, a slash... And has the same commands as word.

paragraph

sentence

Like paragraph but selects until a dot.

tags

Autocommands

It's a way to launch some commands when an event happens. The format is:

autocmd event pattern command

For example:

autocmd BufRead,BufWritePre *.html normal gg=G

The way to read this is: When BufRead or BufWritePre (when reads a file or writes a file) with the pattern *.html, execute a normal command, gg=G, in this case it will autoindent all the file from the beginning to the end.

The list of events is in :help autocmd-events

Another example, this will comment out a line in a html file with c au Filetype html nnoremap <leader>c I<!--<esc>A--><esc>

Registers

Actions in insert mode

More in regexp

More useful stuff

Functions

Example of function, call with :call AddHelloToTop(). The ! replaces the function so you can resource it without errors

function! AddHelloToTop ()
    normal HOhello thereA vim user0
    s/hello there/hi
    return "we added a message"
endfunction

If we add command! Hello call AddHelloToTop() we can use :Hello or also do something like nnoremap <leader>h :call AddHelloToTop()<cr> and use h

Better Autocompletion in VIM

function! InsertTabWrapper()
    let col = col(".") - 1
    if !col || getline(".")[col -1] !~ '\k'
        return "\<tab>"
    else
        return "\<c-n>"
endfunction

inoremap <tab> <c-r>=InsertTabWrapper()<cr>
inoremap <s-tab> <c-p>

Language-Specific Settings

Just create a folder under .vim called ftplugin and, inside this one, one called javascript.vim. Put some settings there, and this will be only applied to .js files. Same with css.vim, html.vim, etc etc.

Abbreviations

It's useful to fix some common problems when writting :abbrev teh the will fix teh with the inoreabbrev teh the will do the same in .vimrc :unabbrev the gets rid of it

Status line

:set laststatus=2 shows the status line always :set statusline=%f\ \ line:%l/%L\ col:%c\ %p%%\ %y

Node debugging in VIM

npm install -f vimdebug more info After that, you can debug a file with node-vim-inspector file.js and launch vim -nb in another term to launch the file. Shortcuts are:

  Ctrl+c -> continue
  Ctrl+i -> step in
  Ctrl+o -> step over
  Ctrl+n -> step next
  Ctrl+u -> move one stack frame up
  Ctrl+d -> move one stack frame down
  Ctrl+p -> set breackpoint at current location