This “vi” tutorial is intended for those who wish to master and advance their skills beyond the features of the editor. It covers buffers, “vi line instructions, interfacing with commands, and ctags. The  editor is an enhanced version of vi. The improvements are clearly noticed in the handling of tags.

Basic control and conventions

  • Key combination – several keys pressed together, e.g. Ctrl+AE means Ctrl+Shift+aE.
  • Key sequence – several keys or combinations pressed gradually, e.g.. Ctrl+wwE means a press of combination Ctrl+wand than a key w.
  • Working mode – it is displayed bottom right:
    •   C – basic command mode, EscE,
    •   E – insert mode InsCiC,
    •   V – visual, used for highlighting of text blocks vC/VC,
    •   ex – command line ex-mode :C.
  • Repeat command with typing number  of repetitions before command, e.g. 5xC deletes 5 chars from cursor position. Option set showcmd shows command sequence in bottom right.
  • Help: help topic, use Tab for command completion.
  • Similar commands uses same keys, e.g. fC / FCtC / TC (see below). The difference is direction or range of operation.
  • Repeat last:command .C, macro @@C
  • Undo:uC  ≈  undo, redo Ctrl+rC / redo
  • Set or unset options with set option a set nooption. Alone set displays current options.
  • Shortening of options:seignorecase  ≈ se ic
  • Line addressing of ex-commands:
    • xcommandexecute on line x
    • x,ycommandfrom x to line y
    • .,$commandfrom actual position to the end
    • %command  ≈  1,\$commandfor whole
    • .,+xcomm  ≈  xcommfrom cursor to next x lines
    • .,$–xfrom current position to x. line from the end
    • /pattern1/,/pattern2/from the first occurrence of line with pattern1 to the first pattern2
  • More ex-commands together:command1 | command2
  • Comparison of two files:vimdiff file1 file2

Edit and save

  • Edit:edit file
  • Open new empty file:enew
  • Insert file to current position:read file
  • Insert output of system command:read !command
  • Save/append file:write file / w >> file
  • Save/append lines n to m:n,mfile / n,mw>> file
  • Save and quit:wq, enforced wq!
  • Quit without save:q!  ≈ ZQC
  • Quit and save when file was changed:x  ≈  ZZC
  • Go to file under the cursos:gfC
  • Change name of current file:jméno
  • Insert before/after:char iC / aC, line IC / AC
  • Insert new line above/below cursor:OC / oC
  • Rewrite:char rC, word cwC, line ccC, text RC  ≈  InsInsC
  • Delete char/line and continue edit:sC / SC

Moving throught file

  • Open file on 23rd line:vim file +23
  • Cursor:C  ≈  lCC  ≈  hCC  ≈  kCC  ≈  jC
  • Paging:Ctrl+fC  ≈ PageUpE / Ctrl+bC  ≈ PageDownE
  • Half-paging up/down:Ctrl+uC / Ctrl+dC
  • Jump to nth line:n  ≈  nGC , the last $  ≈  GC
  • Jump to percentual line position:n%C
  • Display current line number:Ctrl+gCset number
  • Display number of lines:=
  • Jump to byte position:goto n
  • Jump in line before char a forward/backward:taC / TaC
  • Jump in line to char a forward/backward:faC / FaC
  • Repeat last line search forward/backward:;C / ,C
  • Go to beggining of next/previous word:wC / bC
  • Go to end of a word:eC, with interpunction EC
  • Go to declaration:gdC
  • Go to begin/end of paragraph:{C / }C, block: [{C / ]}C
  • Go to begin/end of line:HomeC  ≈ ^C / EndC  ≈ $C
  • Go to next/previous sentence:)C / (C
  • Go to next/previous line:EndC  ≈ +C / C
  • Go to column:first 0C  ≈ |Cnth n|C
  • Jump to pair bracket:%C
  • Print position, number of lines and chars, etc.:gCtrl+gC
Marking of position
  • Insert invisible mark a:maC
  • Jump to mark a:‘aC
  • Print marks:marks
  • Erase mark a:delmarks a
  • Process text from mark a to b:‘a,’b příkaz, e.g. ‘a,’b !sort will use external command for sort

Deleting, moving and copying of text

  • Deletion automaticly stores text in register (for moving)
  • Delete:char xC  ≈  DelC; previous char XC  ≈  dC  ≈  dhCCtrl+hE  ≈  BackSpaceE; word dwC  ≈ Ctrl+wE; line ddC
  • Delete up to pair bracket:d%C
  • Delete to end/beginning of sentence:d)C / d(C
  • Delete the rest of line/file:DC  ≈  d$C  / dGC
  • Delete from cursor position to beginning:d0C  ≈ d^C
  • Join lines:JC add space, gJC without space
  • Copy to register:line YC  ≈  yyC, word ywC
  • Copy to the beginning/end of line:y0C / y$C
  • More registers: press kC before command will save text to register k. E.g. “kyyC copies line into k
  • Paste content of last used register or last deleted text: after pC, before PC cursor, “kppC pastes register k
  • Special register: "% – current file name, "/ – last search,…
  • Show content of registers:register a
  • Swaps the next two chars/lines:xpC / ddpC
  • ex-mode:delete d, move m, copy co  ≈  t
  • 10,20m0 – move lines 10–20 to the beginning (0)
  • .,+5d a – delete next 5 lines and store them to reg. a

Formatting

  • Set text width:set textwidth=n
  • on line wrap:set wrap
  • Justify:centerrightleft
  • Format line:gqqC
  • Format paragraph:gqapC jump to end, gwapC
  • Reverse case of char:~C, line g~~C
  • Line to upper/lower:gUUC / VuC  ≈  guuC / VUC
  • Whole file to upper/lower case:gggUGC / ggguGC
  • While line/file ROT13 encoding:g??C / ggg?GC
Indentation
  • Shift line:>>CCtrl+tE of set shiftwidth=n
  • Shift line leftwards:<<CCtrl+dE
  • Tabbing:TabE  ≈  Ctrl+iE
  • Shift line same as before:==C
  • Indent block relatively:=a{ C
  • Refresh indentation of whole file:gg=GC

Complementation

  • Complement word before/after cursor:Ctrl+pE / Ctrl+nE
  • Name of file in working directory:Ctrl+x Ctrl+fE
  • Name of directory:Ctrl+x Ctrl+dE
  • Whole line:Ctrl+x Ctrl+lE
  • Word from dictionary:Ctrl+x Ctrl+kE
  • System command (reads $PATH):Ctrl+x Ctrl+vE
  • Increase/decrease number under cursor:Ctrl+aC / Ctrl+xC
  • Name of current file:Ctrl+r%E
Completition at ex-command line
  • Insert content of register a:Ctrl+ra
  • Insert word under the cursor:Ctrl+r Ctrl+a
  • Cancel command:Ctrl+u

Insert special sumbols

  • Display unprintable characters:set list
  • Insert ASCII char:Ctrl+v code EndE
  • Show ASCII/UTF-8 of char under cursor:gaC / g8C
  • Suppress control character, e.g. Tab (^I):Ctrl+v TabE
  • Table of Unicode digraphs:digraphs
  • Insert digraphs:Ctrl+k K1 K2E
  • Digraphs: ± +- ° DG × *X ÷ -: – -N — -M „ :9 “ :6
  • Define digraphs .3 for „…“ (U+2026):dig .3 8230

Search and replace

  • Ignore case:set ignorecase, opposite set noic
  • Search forward/backward:/patternC / ?patternC
  • Repeat last search:/C / ?C
  • Ignore case:/\cpatternC
  • Jump to next searched place:nC forward, NC backward
  • Search for word under the cursor:#C forward, *C backward
  • Search by ASCII code of „M“:%d77%x4d%o0115
  • Search Unicode character „α“ (U+03B1):%u03b1
  • Substitution:substitute/from/to/flag, abbr. s
  • Repeat last substitution:s  ≈ s//~/
  • The delimiter / can be any char instead \" and |
  • Flags:c – confirmation, g – for whole line, p – print, without flag changes only first occurence
  • Cancel highlight:nohlsearch
  • Search in more files:vimgrep /pattern/g f1 f2 … fn
  • Execute command on line with:global/pattern/command
  • Execute where pattern not match:g!/pattern/command

Regular expressions

  • Begin/end of:line ^ /  $, word \< / \>, file %^ / %$
  • Last searched pattern/substitution:& /  ~
  • Matches any character:. (dot)
  • Or/and:| / \&
  • Suppress next special symbol:\.\&$\^\\\*
  • Atom:\(pattern\)
  • Nth reference:\N, first \(pattern\) = \1
  • Ranges:[abcd]  ≈  [a-d][a-zA-Z]  ≈  \a[0-9]  ≈  \d
  • Ignored chars and ranges:[^abcd]  ≈  [^a-d]
  • Zero or multiple z:z*  ≈ z\{0,\}
  • One or multiple z:z\+  ≈ z\{1,\}
  • One or zero times:z\?  ≈ z\{0,1\}
  • Repetition z:z\{n\} accurate z\{n,\} minimal z\{n,m\} min. n and max. m times

Folding of text blocks (help folding)

  • Fold text:zfC, selected zfV
  • Jump to beginning/end of fold:[zC / ]zC
  • Open fold under the cursor:zoC, recursively zOC
  • Open/close all:zRC / zMC
  • Switch fold-unfold:zaC, recursively zAC
  • Highlight folded parts:set foldcolumn=5

Edit more files, and tabs

More files (edit buffers)

  • Start vim:vim file1 file2
  • Display buffers:ls  ≈  files (numbered)
  • Switch to next/previous file:next / prev
  • Switch to first/last file:rewind  ≈  first / last
  • Perform mass command for all files:bufdo command

Windows

  • New window:new  ≈  Ctrl+wnC
  • Split window:split  ≈  Ctrl+wsC
  • Vertically split window:vsplit  ≈  Ctrl+wvC
  • Close window:close  ≈  Ctrl+wcC
  • Close all except current:only  ≈  Ctrl+woC
  • Jump to next window:Ctrl+wwC
  • Shrink/large window by n lines:Ctrl+wnC / Ctrl+wn+C
  • Shrink/large window by n columns:Ctrl+wn<C / Ctrl+wn>C
  • Same size for all windows:Ctrl+w=C
  • Mass:write wall, quit qallwqall
  • Perform mass command for all windows:windo command
  • Open name under cursor in new window:Ctrl+wfC
Moving in window
  • Jump to window line:top HC, middle MC, last LC
  • Jump to nth line from top/bottom:nHC/nLC
  • Set actual line to:top zEndC, center z.C, bottom z-C
  • Scroll window up/down by:line Ctrl+eC / Ctrl+yC, page Ctrl+bC / Ctrl+fC, half-page Ctrl+uC / Ctrl+dC
  • Redraw window without scrolling:Ctrl+lC

Tabs

  • Command:vim -p f1f2 … open files in tabs
  • Create/close tab:tabnew / tabc
  • Close all, except current tab:tabo
  • Go to next/previous:tabn  ≈  Ctrl+PageDownE / tabp  ≈  Ctrl+PageUpE
  • Move after the fold n:tabm n
  • Perform mass command:tabdcommand
  • Change tab design:set tabline
  • Open name of file under cursor in new tab:Ctrl+wgfC

Visual mode

  • Select:chars vC, lines VC, cursor sets range
  • Block text selection:Ctrl+VC
  • Change to upper/lower case:UV / uV
  • Select:word awV, ()/{} block abV / aBV, no brackets ibV / iBV; XML block with marks atV, inside marks itV
  • Perform command in selected range:‘<,’> command

Abbreviations, key maps and macros

  • Define abbreviation:abreviate abbr full expansion
  • Abbreviations for insert/ex-mode:iabbrev / cabbrev
  • List/delete abbreviations:ab / unab abbreviation
  • Map key x:map x commands
  • Map by mode:map imap cmap nmap vmap
  • Map key for insert mode:map! x commands
  • List key mapping:map/map!
  • Delete mapping:unmap x/unmap! x
  • Write macro to register a:qaC
  • End macro definition:qC
  • Perform macro in register a:@aC
  • Repeat last called macro:@@C
  • Execute macro a as ex-command:@a

Spell checking

  • Switch on spelling:set spell spelllang=cs,en
  • Search next/previous mistype:]sC / [sC
  • Add to dictionary a word under cursor:zgC
  • Add temporary to dictionary:zGC
  • Set word as bad:zwC, temporary zWC
  • Suggest correctly spelled words:z=C
  • Always take the first suggestion:1z=C

Programmer support

  • Run make:make
  • Jump to next/previous error:cprevious / cnext
  • List all errors:clist
  • Add window with list of errors:copen

Other

  • Start shell:shell
  • Open manpage of command under cursor:KC
  • Open file under the cursor:gfC
  • Save as color :runtime! syntax/2html.vim
  • Wildcard of current file %, e.g. !context % process current file with ConTeXt

this document at here vim_cheatsheet

Print Friendly, PDF & Email

Comments

comments

Bài viết liên quan