There are so many aspects to Vim that defining any subset of skills is fairly arbitrary but here is a first stab at it.

You can consider yourself a proficient VIM user if you can :-


# 1: write a simple substitute
:%s/fred/joe/gic

# 2: write a substitute using memory
:'a,'bs/\<fred\>/& &/

# 3: Combine global with substitute (only do the substitute on lines containing fred)
:'a,'bg/fred/s/dick/joe/igc

# 4: Use a recording for small and big repetitive edits

# a simple recording 'q' to change character under cursor to aa equals, then descend a line
qqr=jq
repeat the recording q 55 times
55@q

# 5: Use registers. be happy using registers to store information for later use

# store current line to register a
"ayy

#store a block to the paste register
"*y}

# customising vim with maps and abbreviations

# 6: write a map to create a temporary composite command
# or permanent if stored in .vimrc

# eg insert date into a file
:inoremap \zd <C-R>=strftime("%d%b%y")<CR>

# eg read a text file
:map <f8> :r c:/aaa/x.txt

# 7: Use abbreviations

# eg spit out a PHP snippet
:iab phpif if ($a=='2')<CR>{<CR>}<CR>else<CR>{<CR>}<CR>

 

It can be very tedious to sort blocks of text in a long file . The following command will sort all blocks individually at the same time. Place an empty line at beginning and end of the file if there are none.


:g/^\s*$/;//-1sort

Explanation:-
:g/// operate over whole file
/^\s*$/ find empty line with or without white space
; after this search for
// as empty, repeats first search for empty line
-1sort

 

I want to merge changes between two files using Vim’s difference commands:-

> gvim -d file1.txt file2.txt
opens the two files in adjacent split windows (vertical split) differences are highlighted by different colors.
I use dp (difference put) to transfer a difference from window to the next. But watch out dp is great when the difference is a single line or block but useless when different changes have been made to a line of block because dp will bluntly overwrite the changes in the other file (information lost). Gvim will highlight in color what it considers is differential block, if a complex merging is required then use ORDINARY Vim commands to merge the information as you require.

Summary the Vim difference tools are incredibly useful for viewing and merging differences but use your own common sense whether to use actual difference commands.

  • Tip 1: practice on junk files
  • Tip 2: when you do the merge, do it on COPIES of the originals (or backup originals)
  • Tip 3: Use your common sense as usual with vim, you are in charge

Gvim Diff command example

 
The paste buffer is usually the easiest way to transfer text between VIM and another application.
See below for all the copy-paste commands and examples of use.
My favorite is :’a,’by* copy lines a to b to the paste buffer

” Redirection & Paste register *
:redir @* : redirect commands to paste buffer
:redir END : end redirect
:redir >> out.txt : redirect to a file
” Working with Paste buffer
“*yy : yank current line to paste
“+y} : yank to end of paragraph
“+yi{ : yank current paragraph
“*p : insert from paste buffer
” yank to paste buffer (ex mode)
:’a,’by* : Yank range into paste
:%y* : Yank whole buffer into paste
:.y* : Yank Current line to paster
” filter non-printable characters from the paste buffer
” useful when pasting from some gui application
:nmap p :let @* = substitute(@*,’[^[:print:]]’,”,’g')”*pr

 
I’ve been using the following recently as I’ve been working on some spaghetti html pages with mysql,php,Javascript and HTML sprinkled sometimes on the same line. The following help me to precisely “grab” the code I need to alter. I’ve been favoring VISUAL MODE as this confirms or otherwise that I’ve selected just what I intend.

Note how the “a” and “i” inner variants differ the ‘i’ grabs less than
the outer ‘a’

vat,dat,yat,cat (visualize,delete,yank,change) a tag

ie dat will delete a whole table if cursor anywhere over < table>

ie yat will yank a whole table row if cursor over < tr>

vit,dit,yit,cit will empty a tag pair

da< just deletes a single tag

di< empties a tag

:help text-objects

seems to work with XML as well

Also quote based text objects (single line only though for some odd reason)

va”
da”
va’
da’
vi”
di”
vi’
di’
di<
and don’t forget
di(
da{
etc

© 2012 VIM Tips Blog Suffusion theme by Sayontan Sinha