Daily vim – Grand collection

working with Blocks

di> (“delete inner angle-block”)

ci” (“change inner double-quote block”)

daw, dap, dab

caw, cap, cab

>ib, <ib, >iB, >i}, >i)

%

= (auto indent selected text & auto format base on file type)

q     start/stop record macros

“+y (copy to clipboard)

“+p (paste from clipboard)

dst, cst, cs”) (vim surround)

ysiw) , yssB (vim surround) – ys then select operator fast (y-s-s fast)

“dyy, “a7yy (make use of buffer a, d)

“dp, “ap ( use a,d buffer)

“a5dd

-> “d command (do command with buffer d)

:reg

y,d,c + movement (H,M,L,),},])

:g/pattern/s/old/new/g (ex search combine with replace)

study unix commands for this \(^o^)

:.,$!sort -g

:%!sort -d

(and etc…)

working with tags

:tags (show tags browse route)

:tn, :tp (tag next, tag pre)

:ts [ident] list the tags match [ident] , :sts [ident] with split window (can specify ‘vert’ for vertical split)

g] (list)

^] (go direct to 1st tag def.)

working with completion

^X [^F, ^L, ^D, ^I, ^K, ^T, ^], ^V, ^O, ^N, ^P]     file,line,def,directory,words,tags,vimcmd,include f,next,pre

working with tabs

:tabs list all tabs

:tabm 0 move tab to first

:tabm move tab to last

:tabm {i} move tab to i+1

gt, gT, {i}gt move to tab in position i

gi : go to insert mode where you left it last time

g+,g- : undo state / branches ?

zo,zc,zR(open all folds),zM(close all folds),zj,zk(navi)

Vim navi

(), {}, [[]], -/+ (movement)

fx, Fx, tx, Tx, ; ,

:Explore (vim default file explorer)

<C-d> : list posibilities – combine with Tab

:vimgrep, :Ack (o, go, t, T, h, H, v, gv, q)

:go NNN      go to byte NNN

‘.     jump to last modification line

`.     jump to exact spot in last modification line

<C-O>     retrace your movements in file (backward)

<C-I>     retrace your movements in file (forward)

:ju(mps)     list of your movements

:history     list of all your commands

:source $MYVIMRC reload vimrc

Vim command help

<C-d> : list all commands base on input chars

<C-f> : browse history base on input chars

<TAB> : auto complete, loop through all commands base on input chars

Set filetype for syntax highlighting & auto format

:set filetype=[file type]

file type can be php, html, sql, xml , js or cpp …

Working with diff

vert diffsplit [file to compare]

:[range]diffg[et]

:[range]diffpu[t]

do     :[range]diffg[et] without range

dp     :[range]diffpu[t] without range

:diffoff

Tools

:TOhtml     export content of current file to html (theme included).

Sessions (not so useful with plugin -> turn off plugin like NERDtree before mksession)

:mksession [session name].vim     make session

:source [session name].vim     restore session

vim -S [session name].vim     start vim with specific session.

Vim and terminal

:%! [cmd]

:read ! [cmd]

:!php -l %     check php syntax

Auto complete base on context – Object

place DocBlock notation under variable:

/*@var $_product Mage_Catalog_Model_Product*/

Working with text case

guu     : lowercase line

gUU     : uppercase line

~       : invert case (upper->lower; lower->upper) of current character

gf      : open file name under cursor (SUPER)

ga      : display hex, ascii value of character under cursor

g8      : display hex value of utf-8 character under cursor

ggg?G   : rot13 whole file

xp      : swap next two characters around

CTRL-A,CTRL-X : increment, decrement next number on same line as the cursor

CTRL-R=5*5    : insert 25 into text

=             : (re)indent the text on the current line or on the area selected (SUPER)

=%            : (re)indent the current braces { … }

G=gg          : auto (re)indent entire document

pssh secret!!!

help 42 -> help holy-grail

collaborate with shell

:r!ls                 : reads in output of ls (use dir on Windows)

:r !grep “^ebay” file.txt  : read output of grep

:20,25 !rot13        : rot13 lines 20 to 25

:r!date              : insert date (use  date /T on Windows)

:.!sh                : execute contents of current line in buffer and capture the output

Sorting with external sort

:%!sort -u           : contents of the current file is sorted and only unique lines are kept

:’v,’w!sort          : sort from line marked v thru lines marked w

:g/^$/;,/^$/-1!sort  : sort each block (note the crucial 😉

!1} sort             : sorts paragraph; this is issued from normal mode!)

Entering !! in normal mode is translated to  :.!

Appending a command sends the current line to the command replacing it with command’s result

!!date              : Replace current line with date

!!which command     : Replace current line with the absolute path to command

!!tr -d AEIO        : translate current line deleting As, Es, Is, and Os from the current line

You can also use ! on a visual selection. Select an area with one of the visualmode

commands, and then type !command to pipe the whole selection through command.

This is equivalent to :'<,’>!command.

For example, after selecting multiple lines with visualmode:

!sort              : sort selected lines

!grep word         : keep only lines containing ‘word’ in the selected range.

Leave a Comment