Vim入门

Published on 2016 - 07 - 19

First, you need to know the two main operating modes: command and input. The vim editor always starts in command mode. Before you can add or change text in the file, you have to type a command (one or two letters, sometime preceded by an optional number) to tell vim what you want to do. Case is important, so use uppercase and lowercase exactly as shown in the examples!

Adding text

To get into input mode, type an input command letter. To begin, type any of the following letters. When you are finished inputting text, press the Esc key (sometimes twice) to return to command mode. Remember the Esc key!

  • a—The add command. With this command, you can input text that starts to the right of the cursor.
  • A—The add at end command. With this command, you can input text starting at the end of the current line.
  • i—The insert command. With this command, you can input text that starts to the left of the cursor.
  • I—The insert at beginning command. With this command, you can input text that starts at the beginning of the current line.
  • o—The open below command. This command opens a line below the current line and puts you in insert mode.
  • O—The open above command. This command opens a line above the current line and puts you in insert mode.

Moving around in the text

To move around in the text, you can use the up, down, right, and left arrows. However, many of the keys for moving around are right under your fingertips when they are in typing position:

  • Arrow keys—Move the cursor up, down, left, or right in the file one character at a time. To move left and right, you can also use Backspace and the spacebar, respectively. If you prefer to keep your fingers on the keyboard, move the cursor with h (left), l (right), j (down), or k (up).
  • w—Moves the cursor to the beginning of the next word (delimited by spaces, tabs, or punctuation).
  • W—Moves the cursor to the beginning of the next word (delimited by spaces or tabs).
  • b—Moves the cursor to the beginning of the previous word (delimited by spaces, tabs, or punctuation).
  • B—Moves the cursor to the beginning of the previous word (delimited by spaces or tabs).
  • 0 (zero)—Moves the cursor to the beginning of the current line.
  • $—Moves the cursor to the end of the current line.
  • H—Moves the cursor to the upper-left corner of the screen (first line on the screen).
  • M—Moves the cursor to the first character of the middle line on the screen.
  • L—Moves the cursor to the lower-left corner of the screen (last line on the screen).

Deleting, copying, and changing text

The only other editing you need to know is how to delete, copy, or change text. The x, d, y, and c commands can be used to delete and change text. These can be used along with movement keys (arrows, PgUp, PgDn, letters, and special keys) and numbers to indicate exactly what you are deleting, copying, or changing. Consider the following examples:

  • x—Deletes the character under the cursor.
  • X—Deletes the character directly before the cursor.
  • d<?>—Deletes some text.
  • c<?>—Changes some text.
  • y<?>—Yanks (copies) some text.

The <?> after each letter in the preceding list identifies the place where you can use a movement command to choose what you are deleting, changing, or yanking. For example:

  • dw—Deletes (d) a word (w) after the current cursor position.
  • db—Deletes (d) a word (b) before the current cursor position.
  • dd—Deletes (d) the entire current line (d).
  • c$—Changes (c) the characters (actually erases them) from the current character to the end of the current line ($) and goes into input mode.
  • c0—Changes (c) (again, erases characters) from the previous character to the beginning of the current line (0) and goes into input mode.
  • cl—Erases (c) the current letter (l) and goes into input mode.
  • cc—Erases (c) the line (c) and goes into input mode.
  • yy—Copies (y) the current line (y) into the buffer.
  • y)—Copies (y) the current sentence ( ) ), to the right of the cursor, into the buffer.
  • y}—Copies (y) the current paragraph ( } ), to the right of the cursor, into the buffer.

Any of the commands just shown can be further modified using numbers, as you can see in the following examples:

  • 3dd—Deletes (d) three (3) lines (d), beginning at the current line.
  • 3dw—Deletes (d) the next three (3) words (w).
  • 5cl—Changes (c) the next five (5) letters (l) (that is, removes the letters and enters input mode).
  • 12j—Moves down (j) 12 lines (12).
  • 5cw—Erases (c) the next five (5) words (w) and goes into input mode.
  • 4y)—Copies (y) the next four (4) sentences ( ) ).

Pasting (putting) text

After text has been copied to the buffer (by deleting, changing, or yanking it), you can place that text back in your file using the letter p or P. With both commands, the text most recently stored in the buffer is put into the file in different ways.

  • P—Puts the copied text to the left of the cursor if the text consists of letters or words; puts the copied text above the current line if the copied text contains lines of text.
  • p—Puts the buffered text to the right of the cursor if the text consists of letters or words; puts the buffered text below the current line if the buffered text contains lines of text.

Repeating commands

After you delete, change, or paste text, you can repeat that action by typing a period (.). For example, with the cursor on the beginning of the name Joe, you type cw and type Jim to change Joe to Jim. You search for the next occurrence of Joe in the file, position the cursor at the beginning of that name, and press a period. The word changes to Jim, and you can search for the next occurrence. You can go through a file this way, pressing n to go to the next occurrence and period (.) to change the word.

Exiting vim

To wrap things up, use the following commands to save or quit the file:

  • ZZ—Saves the current changes to the file and exits from vim.
  • :w—Saves the current file but doesn't exit from vim.
  • :wq—Works the same as ZZ.
  • :q—Quits the current file. This works only if you don't have any unsaved changes.
  • :q!—Quits the current file and doesn't save the changes you just made to the file.

Consider the following tips to smooth out your first trials with vim:

  • Esc—Remember that Esc gets you back to command mode. (I've watched people press every key on the keyboard trying to get out of a file.) Esc followed by ZZ gets you out of command mode, saves the file, and exits.
  • u—Press u to undo the previous change you made. Continue to press u to undo the change before that and the one before that.
  • Ctrl+R—If you decide you didn't want to undo the previous undo command, use Ctrl+R for Redo. Essentially, this command undoes your undo.
  • :!command—You can run a shell command while you are in vim using :! followed by a shell command name. For example, type :!date to see the current date and time, type :!pwd to see what your current directory is, or type :!jobs to see whether you have any jobs running in the background. When the command completes, press Enter and you are back to editing the file. You could even use this technique to launch a shell (:!bash) from vim, run a few commands from that shell, and then type exit to return to vim. (I recommend doing a save before escaping to the shell, just in case you forget to go back to vi.)
  • Ctrl+G—If you forget what you are editing, pressing these keys displays the name of the file that you are editing and the current line that you are on at the bottom of the screen. It also displays the total number of lines in the file, the percentage of how far you are through the file, and the column number the cursor is on.

Skipping around in the file

Besides the few movement commands described earlier, there are other ways of moving around a vi file. To try these out, open a large file that you can't do much damage to. (Try copying /etc/services to /tmp and opening it in vi.) Here are some movement commands you can use:

  • Ctrl+f—Pages ahead, one page at a time.
  • Ctrl+b—Pages back, one page at a time.
  • Ctrl+d—Pages ahead one-half page at a time.
  • Ctrl+u—Pages back one-half page at a time.
  • G—Goes to the last line of the file.
  • 1G—Goes to the first line of the file.
  • 35G—Goes to any line number (35, in this case)

Searching for text

To search for the next or previous occurrence of text in the file, use either the slash (/) or the question mark (?) character. Follow the slash or question mark with a pattern (string of text) to search forward or backward, respectively, for that pattern. Within the search, you can also use metacharacters. Here are some examples:

  • /hello—Searches forward for the word hello.
  • ?goodbye—Searches backward for the word goodbye.
  • /The.*foot—Searches forward for a line that has the word The in it and also, after that at some point, the word foot.
  • ?[pP]rint—Searches backward for either print or Print. Remember that case matters in Linux, so make use of brackets to search for words that could have different capitalization.

After you have entered a search term, simply type n or N to search again in the same direction (n) or the opposite direction (N) for the term.

Using ex mode

The vim editor was originally based on the ex editor, which didn't let you work in full-screen mode. However, it did enable you to run commands that let you find and change text on one or more lines at a time. When you type a colon and the cursor goes to the bottom of the screen, you are essentially in ex mode. The following are examples of some of those ex commands for searching for and changing text. (I chose the words Local and Remote to search for, but you can use any appropriate words.)

  • :g/Local—Searches for the word Local, and prints every occurrence of that line from the file. (If there is more than a screenful, the output is piped to the more command.)
  • :s/Local/Remote—Substitutes Remote for the first occurrence of the word Local on the current line.
  • :g/Local/s//Remote—Substitutes the first occurrence of the word Local on every line of the file with the word Remote.
  • :g/Local/s//Remote/g—Substitutes every occurrence of the word Local with the word Remote in the entire file.
  • :g/Local/s//Remote/gp—Substitutes every occurrence of the word Local with the word Remote in the entire file, and then prints each line so you can see the changes (piping it through less if output fills more than one page).

Reference