view · edit · history · print

Description: CheatSheet

I suppose everybody knows the basic "ex" and "vi" commands, can yank words and move text with the ex editor? If you don't then go and find other basic cheatsheets for vi on the World Wide Web!

Here are some forgotten ones - the capital commands:
  A  add text to the end of the line
  I  add text to the beginning of the line, the opposite of A
  X  can be used as backspace
  P  pastes before cursor (like on column 1, at the start of the line)

Here are some overlooked ones - to jump around:
  z.       position line for easy reading and code editing
  m<a..z>  creates invisible (book)marks
  '<a..z>  move to marker
  `<a..z>  move to [non-whitespace] marker
  :marks    list markers
  %        go to matching parenthesis
  ctrl f   page forward
  ctrl b   page backward

Here are some multi window commands:
  ctrl+W s          split window
  ctrl+W c          close window
  ctrl+W k or up    switch to above window
  ctrl+W j or down  switch to above window
  ctrl+W o          no split windows

  d     delete
  u     undelete
  "2p   recover from 2nd deletion
  "xyy  yank line in buffer x
  "xp   pase from buffer x

Multiple files:
  :e!   re-open the file as it is saved, shorter then :q! end re-open
  :e x           open another file, x=filename
  :n             edit next opened file
  ctrl+^ or :e#  previous file

Supersafe mapping for macro's:
  Letters: g, K, q, V, v
  Control keys: ?
  Symbols: _, *, \, =
  Note: to type control characters type [ctrl]+[v] [ctrl]+[character]

  :%s/...   replace in entire file  -  "%"
  :1,.s/... replace from line 1 to cursor position  -  "."
  :.../c    confirm each replacement with y/n  -  "c"


Windows & multiple files

vim better supports window splitting and editing multiple files!

:ls       for file list
:b2       for selecting buffer 2
:split    horizintal split
:vsplit   vertical split
:qa       quit all
:cw       open new compiler error window
:bd       buffer delete, close current open file (buffer)
:vert res resize to full witdh ( also CTRL-W + | )
:res      resize to full height ( also CTRL-W + _ )
ctrl-W =  equaly resize windows
:help res
:Sex      Let VIM have Sex with your directory sructure (explore,browse)

See also :


:mksession ~/mysessionfile
$ vim -S ~/mysessionfile


Special characters (ex)

This is like variables but more elaborate.

:help cmdline-special
:help internal-variables

Convert to upper case

viwU     convert word to UPPER case
v$U      convert line from here to UPPER case
vllU     convert 3char to UPPER case

U(in visual mode) convert to UPPER case
u(in visual mode) convert to lower case

Or also: use the "gUgU" command to change the current line to uppercase and the "gugu" command to change the current line to lowercase.


#               Search for word currently under the cursor (cool!)
:syntax off     Turn off syntax coloring

Online references


Back to basics...

s=Substitute (regex type)
c=Change Line
a=Append Text after current line
i=Input Text before current line
.=Current Line
p=Print Line(s)
l=Print Line(s) unambiguously
//=find string and jump to goto line

:1,.d    delete from line 1 to the current line

If you want more features or use the vi command mode on the command line or in scripts then use the ex editor (see also WikiScripting):

ex -c"s/xxx/yyy/g" -c"wq" file

admin · attr · attach · edit · history · print
Page last modified on November 14, 2008, at 02:34 AM