Introduction to Linux programming-VI

Source: Internet
Author: User

VI is a character-only text editor standard in Linux/Unix. Because the mouse function is not supported, and there is no graphical interface, the operations must be completed through keyboard commands, and a large number of commands need to be remembered. Therefore, many people do not like it very much, but because the keyboard is usually faster than the mouse, once skilled, It is very smooth to use, and some people like it very much.

Whether you like it or not, VI is the standard editor of Linux, and most of the time you only have this editor available. If you want to be a Linux Coder, it is necessary to be familiar with VI.

PS: because there are many VI commands, this article mainly introduces some basic commands. In addition, many of these basic commands are not commonly used. I will display these commands in gray text. Beginners do not need to pay too much attention to these gray commands.

 

Two working modes

As mentioned above, because VI does not have a graphical interface, operations such as turning pages, moving the cursor, saving, and exiting must be completed through the keyboard. In addition, VI also provides a large number of shortcut keys to accelerate operations. Therefore, in the editing mode of VI system, a new working mode-command mode is introduced.

  • Edit mode:In this mode, the input of visible characters is the same as that of traditional Windows. Press Esc to switch the value command mode.
  • Command mode:In this mode, the input visible characters correspond to commands. After executing the INSERT command, enter the editing mode.

PS: some articles also list a Last Line mode, which can be regarded as a special command mode, except that the command is displayed in the Last Line and can be executed only after you press Enter. These commands usually use :/? $ And other special characters.

Mode query: to determine the current mode, you can judge by the last line (status bar): If you are in edit mode, the status is displayed as follows.

Switch between modes: In edit mode, you can use the Esc key to Enter command mode. In command mode, run some insert commands and then enter edit mode. If you do not know the current mode, Press ESC to enter the command mode and press Insert to enter the edit mode.

 

Start VI

There are many startup parameters for VI, but the common usage is as follows: viFilename

If a file exists in the parameter, the file is opened. If the file in the parameter does not exist, the file is created (not immediately, but saved ).

 

Insert text

When I first entered VI, it was out of the command mode. This is because the text cannot be entered, so I need to enter the insert mode. The commands used to enter the insert mode include the following:

  • I insert text from the current cursor
  • A. Insert text from the next character of the current cursor
  • O insert a blank row down
  • O insert a blank row above

After entering these commands, the editing mode is displayed. You can see that the current status is insert in the last line at the bottom of the screen:

You can use the Insert key to switch the Insert and rewrite statuses (the same as in the image interface ).

In command mode, you can also use the Insert key to enter the edit mode (Insert Status). However, the four commands are usually used to enter the edit mode.

 

Exit and save files

In VI, the commands corresponding to the exit and save files are as follows (remember to enter the command mode before using ):

  • : W save
  • : W filename save
  • : Wq: Save and exit [common]
  • : Q! Force exit
  • SHIFT + zz save and exit. function: wq [common]

 

Move cursor

In VI, moving the cursor is not a common WASD in our game, but h, j, k, and l, respectively controlling the cursor to move one cell to the left, down, up, and right, the four fingers are distributed on the right hand. It takes some time for new users to adapt. It is frequently used and requires proficiency.

Other cursor control commands are as follows:

  • Ctrl + B: move one page behind the screen.
  • Ctrl + F: Move the screen one page forward.
  • Ctrl + U: Move the screen half a page.
  • Ctrl + D: Move the screen half a page forward.
  • Gg: Start of moving an article.
  • G: Move to the end of the article.
  • W: The cursor jumps to the beginning of the next word.
  • E: move the cursor to the end of the next word.
  • B: The cursor returns to the beginning of the previous word.
  • $: Move to the end of the row where the cursor is located.
  • ^: Move to the first non-blank character of the row.
  • 0: Move to the beginning of the row.
  • #: Move to the # position of the row, for example, 51 and 121.

In addition, there are several commands related to line operations for a brief introduction.

  • : Set nu display row number
  • : Set nonu hidden row number
  • : # Move to row #, for example, 15
  • Ctrl + G: displays the current progress in the status bar
  • # G: Move to row #, for example, 15G

 

Delete text

  • X: delete a character after the cursor position.
  • # X: for example, the 6x table deletes the next six characters of the cursor position.
  • X: X of the big character. Each time the cursor is deleted, the first character is deleted.
  • # X: for example, in the 20X table, the first 20 characters of the cursor position are deleted.
  • Dd: Delete the row where the cursor is located.
  • # Dd: for example, the 6dd table deletes six lines of text from the row where the cursor is located.

 

Copy and paste

Personally, I am not very familiar with the copy and paste operations provided by VI. It is not very convenient to use because it is not highlighted. I usually use the copy and paste function of terminal tools, so I am grayed out the commands here. If you are interested, you can check them.

  • Yw: copy the character from the cursor position to the end of the word to the buffer zone.
  • P: paste the characters in the buffer to the cursor position (the command 'yw' and 'P must be used together ).
  • Yy: copy the row where the cursor is located. [Extraordinary use]
  • P: copy a single row to where you want to paste it. (The command 'yy' and 'p' must be used together)
  • # Yy: for example, 6yy indicates copying six lines of text from the row where the cursor is located. [Common]
  • P: Copy multiple rows to where you want to paste them. (The command '# yy' and' P' must be used together)
  • Ayy: Put the replication row into buffer a. vi provides the buffer function to store common data in the buffer.
  • Ap: paste the data stored in buffer.
  • B3yy: store three rows of data into buffer B.
  • B3p: paste data with buffer B

 

Undo and redo

Undo and redo are useful commands in any Editor:

  • U: If you misoperate a command, you can immediately press u to return to the previous operation.
  • .: Re-execute the last command.

 

Search and replace

The syntax notes for searching and replacing are complex. First, let's briefly introduce the syntax for searching:

  • /Pattern <Enter>: searches down for the pattern matching string
  • ? Pattern <Enter>: search up for the pattern matching string

After the search command is used, use the following two keys for quick search:

  • N: Continue searching in the Same Direction
  • N: search by reverse direction

As for the replacement syntax, we will not introduce it here for your reference at http://blog.csdn.net/lanxinju/article/details/5731843.

 

VI programming support

VI provides some support for programming. For example, it supports parentheses matching and syntax highlighting.

However, because the Syntax Parsing function is not supported, there is a big gap between the coding tool and professional code editors such as SourceInsight and Visual Studio. I personally feel that it is okay to use small projects and temporary code modifications, but it is a little powerless to do large projects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.