Você está na página 1de 2

Vim cheat sheet. Note, there are two primary modes, insert mode and command mode.

Go into Insert Mode. a append text right after the current cursor location A append text at the end of the line o insert a new line below the current cursor line and start insert mode there O insert a new line above the current cursor line and start insert mode there i insert text right before the current cursor location I insert text before the first non-blank in the line Go into Command Mode. <Esc> Go to "command mode", if already in command mode, it will flash & beep. Many users habitually type <Esc><Esc> for visual confirmation of command mode before entering a command. Some also use <Esc>i to enter insert mode to avoid leaving an unintentional i's in the file they're editting Saving, Quitting and Loading files. :q quit vi :q! forcibly quit vi (dont ask questions) :wq write quit, saves the file before quitting, politely observes permissions. :wq! write quit, forcibly saves the file if at all possible before quitting. ZZ a shortcut for save if modified then quit. ZQ a shortcut for :q! :e :e! :e file :e! file :r file :r !cmd Reload current file if changed outside of vim. Forcibly reload current file Load a new file to edit, warns if youve made changes Forcibly load a new file to edit inserts the contents of file below the cursor executes cmd and inserts the STDOUT from the command below the cursor

Navigation in vim Note: While the arrow keys often work, the defacto standard for vi navigation is the keys: 'h', 'j', 'k', 'l' k h l

j Note 2: The reason that dependance on arrow keys is not recommended is because the implementation of arrow key (often an extended character) is not consistent, even among machines of the same type. This often leads to unpredictable, adverse behavior, especially on commands which require {motion}. [count]h move cursor left, you can also move left [count] times by specifying the count [count]l move cursor right, you can also specify [count] [count]j move cursor down one line, you can also specify [count] [count]k move cursor up one line, you can also specify [count] 0 move cursor to beginning of current line $ move cursor to end of current line ^ move cursor to the first non-blank character of current line [line]G move cursor to line number [line] (e.g. 1G moves to first line of file) G a shortcut to move cursor to the last line of file w jump by start of words (punctuation considered words) W jump by words (space separate words) e jump to end of words (punctuation considered words) E jump to end of words (no punctuation) H jump to first line of screen (High) M jump to middle line of screen (Medium) L jump to last line of screen (Low) /{pattern}search forward in the text file for {pattern} ?{pattern}search backwards in the text file for {pattern} n repeat the last '/' or '?' search N repeat the last '/' or '?' search in the opposite direction Undo/Redo/Repeat u undo changes, you can also specify [count] in front. U undo all changes on one line <Ctrl>-R redo changes, you can also specify [count] before pressing Ctrl-R . <period> Repeat last change, you can also specify [count] Yanking (Copying) and Pasting "[regist] Use register {a-zA-Z0-9.%#:-"} for next yank, delete or paste. :reg Display contents of registers (also commonly referred to as a buffer) :reg {arg}Display contents of registers mentioned in {arg}. y{motion} Yank text, "xy yanks text into register x, a common motion is y'a (after ma) You can also yank the contents of a Visual Mode block. To yank a range, :[range]y[x] will yank [range] lines into register [x] yy Yank a full line into a register, can also manually specify register "xyy p Paste the text after the cursor, one can specify register, e.g. "xp You can also prepend [count], e.g. 5p will paste 5 times. P Paste the text before the cursor, one can also specify register or count. gp Just like p, but places cursor just after the new text. gP Just like P, but places cursor just after the new text. Visual Mode (aka Selecting Text) [use navigation keys to highlight] v start Visual Mode (select by character) [newer versions of vim only] V start Visual Mode, linewise (select by lines) <Ctrl>-V is start of Visual block mode, use arrow keys to highlight a literal block <Esc> exits Visual Mode (goes back to Command Mode) aw highlight a word ab highlight a () block (everything within parens) aB highlight a {} block (everything within curly braces) ib highlight a inner () block iB highlight a inner {} block After you have highlighted text, common operators to use on the text are: ~ switch case (upper to lower and vice versa) d deletes block c change (deletes block, then goes to Insert Mode) y yanks block > shift right (tab) < shift left (untab) ! filter the contents though an external command = filter through 'equalprg' (by default, reformats/indents source code) o move to other end of marked area O move to Other corner of block Marks A common usage of marks is copying or deleting, e.g. ma then y'a or d'a m{a-z} Set start of mark '{a-z} to the first non-blank character on the line w/ mark {a-z} (linewise). :marks List all current marks Window operations (^w) <Ctrl>-w s <Ctrl>-w w <Ctrl>-w q <Ctrl>-w v :sp filename

Deleting text Note: in Visual Mode you can press 'x' or 'd' to delete the highlighted text x delete the character under the cursor, X delete the character before the cursor location d<arrow> <left/rt arrow> deletes the character before or after the current cursor <up/dn arrow> deletes the current line and the line above or below dd delete a line D deletes from the cursor location to the end of line :[range]d delete [range] lines, e.g. :5,53d will delete the lines between 5 to 53 also :[line]d[cnt] deletes [cnt] lines, starting with line [line] Replacing text r[char] Replaces the character under the cursor with [char] R Similar to insert mode, but overwrites characters instead of inserting. ~ Change case of the character under the cursor, also works in Visual Mode Search and Replace (aka "substitutions" or S&R) :[range]s/{pattern}/{replacement}/[c][g][i][I] c g i I examples: Confirm each substitution, at each match of {pattern}. 'y' replaces, 'n' skips, 'a' replaces all remaining, and 'q' quits substituting Replaces all occurrances in the line, without 'g' only replaces the first occurrance of {pattern} in each line Ignore case in {pattern} Don't ignore case in {pattern} :%s/foo/bar/g replace all instances of "foo" in a file with "bar" :2,9s/foo/bar/ replace first instance of "foo" per line from line 2 though 9 :%s:foo:bar:g also note that the '/' S&R separator can be anything

[range] also commonly referred to as "scope" or "block", here are commonly useful tips: {start},{end} is the most common way to define a range, {start} and {end} are both line numbers. The lower line number should be specified first. line numbers which define a range may be specified by: {number} . $ % * 't an absolute line number the current line the last line in the file all lines in the file, equivalent to (1,$) the highlighted Visual block area, equivalent to ('<,'>) position of Mark 't' (see Marks to the right) the next line where {pattern} matches the previous line where {pattern} matches

/{pattern}[/] ?{pattern}[?]

each of the above may be followed by '+' or '-' and an optional number. example ranges: .+3 /foo/+ .,$ 0;/that three lines below the cursor the line below the line containing "foo" from current line to end of file the first line containing "that"

Split windows Switch between windows Quit (Close) a window Split windows vertically Open a file in a new split window

In depth {pattern} matching (aka Regular Expressions / regex) Basic patterns are normally just a string of text characters, but the power of vim is in the use of metacharacters. These are special characters with special meanings. These are usually distinguished by the backslash '\' in front of them. Normally, to replace all instances of 'vi' with 'VIM' you would use :%s/vi/VIM/g However, you would find that words such as "elvis" would become "elVIMs" because it replaced all occurances of vi, even inside a word. The right way is to put special word boundaries \< and \> around the pattern string. e.g. :%s/\<vi\>/VIM/g Anchor \< \> [ ] Meaning start of word boundary end of word boundary Anchor ^ $ Meaning start of line end of line

Because of its simple character based command entry structure, one can often discover and memorize advanced commands as a short sequence of keystrokes: ea cw c$ cc xp vawy :s:\s*$:: append text at end of word change (replace) to the end of current word change (replace) to the end of line change (replace) current line transpose two letters (cut and paste, technically) highlight the current word under the cursor and Yank into paste buffer drops blanks at the end of the line (prepend % to act on all lines)

Some advanced commands are shorter than others: :%s:\([.!?]\)\s\+\([a-z]\):\1 \u\2:g corrects non-capital words at the beginning of sentences. Working with tables Often you will work with text organized in tables or columns Age 25 36 98 Name Bob Sally Rick Condition Alive Alive Dead Location California Nevada Oregon

Range, matches any chars within the brackets, [012345] is the same as [0-5]

Special cases for ranges: Putting a caret (^) as the first character within the range ([ ]) indicates a negation, it is there because sometimes it's easier to define the characters you don't want to match. /[^A-Z]/ Also note, inside the range all metacharacters behave like ordinary characters, if you want to include a dash (-) in your range, put it first, e.g. /[-0-9]/ To match either "Vi" or "vi" on the start of a line, and replace it, :%s/^[Vv]i/VIM/g Note that the caret is outside the range in the example above. Metachar . \s \d \x \o \h \p \w \a \l \u Meaning any character except newline whitespace character decimal digit hex digit octal digit head of word char {a-zA-Z_} printable char word char alphabetic char lowercase char uppercase char Metachar \S \D \X \O \H \P \W \A \L \U Meaning non-whitespace char non-digit non-hex digit non-octal digit non-head of word char like \p but no digits non-word char non-alphabetic char non-lowercase char non-uppercase char

To change all "Alive" cells in the third column to "Dead": :%s:\(\(\w\+\s\+\)\{2}\)Alive:\1Dead: Age 25 36 98 Name Bob Sally Rick Condition Dead Dead Dead Location California Nevada Oregon

To swap the first and last columns: :%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1: Location California Nevada Oregon Name Bob Sally Rick Condition Alive Alive Dead Age 25 36 98

To match a date like 01/07/2008 you can use " \d\d/\d\d/\d\d\d\d " (Make sure to use a different separator than '/' in S&R.) For example, to replace all dates of type MM/DD/YYYY in a file w/ the string $date :%s:\d\d/\d\d/\d\d\d\d:$date:g To match a 6 letter word with a capital letter " \u\w\w\w\w\w " If you don't know how many letters are in the word, you can use Quantifiers. Quantifier * .* \+ \= \{n,m} \{n} \{,m} \{n,} Meaning matches 0 or more of the preceding characters, ranges or metachars matches everything including the empty line matches 1 or more of the preceding characters matches 0 or 1 more of the preceding characters matches from n to m of the preceding characters (n & m are integers > 0) matches exactly n times of the preceding characters matches at most m of the preceding characters matches at least n of the preceding characters

The quantifiers above are "greedy" meaning it will match as much text as possible. e.g. /".*"/ matches everything between the first and last (") in the following sentance. Often the "greedy" algorithm isn't "the right way", in those cases you may want the the shortest string that matches, the "non-greedy" Quantifiers are: Quantifier \{-} \{-n,m} \{-n,} \{-,m} Meaning matches 0 or more of the preceding atom, as few as possible matches 1 or more of the preceding characters (n & m are integers > 0) matches at least n of the receding characters matches 1 or more of the preceding characters

so for the previous example, if we use \{-} in place of *, the new /".\{-}"/ matches Often the "greedy" algorithm isn't "the right way", in those cases Note: The \{-} quantifier itself is not without it's quirks. While useful for catching cases such as (""), it can lead to unexpected behaviour for the inexperienced. For example if you use :s/.\{-}/_/g on a line like "hello" you will end up with "_h_e_l_l_o_" because "as few as possible" also includes zero, because matching zero is STILL a match. {replacement} part of :s[ubstitute] The replacement part of S&R has it's own special characters & the whole matched pattern \0 the whole matched pattern \N the matched pattern in the Nth pair of \(\) ~ the previous substitute string \u next character made uppercase \U the following characters made uppercase \l next character made lowercase \L the following characters made lowercase \E or \e end of \U and \L \r split line in two at this point Regex Operator Precedence (ordered highest to lowest) 1 \(,\) Grouping 2 \=,\+,*,\{n},etc quantifiers 3 abc\t\.\w sequence of chars or metachars 4 \| alternations

Você também pode gostar