Linux tutorial-Vi entry and exit

Source: Internet
Author: User
Article title: Linux tutorial-Vi entry and exit. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Vi entry
  
After the user logs on to the system, the system prompts "$ ". After the prompt, Type Vi and the file name you want to edit (or create) to go to Vi.
  
[Example 1] enter the following command:
  
$ Vi example. c
  
The screen is shown as follows:
  
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
~
  
"Example. c" [New File]
  
If you only Type Vi without a file name, you can also enter Vi. Then, enter the file content at the cursor. when exiting Vi, you only need to enter the file name after exiting the command.
  
After entering Vi, you first enter the command mode, that is, waiting for command input instead of text input. The entered letters are interpreted as commands. The cursor stops at the first line of the screen (expressed as "~"). Symbol, indicating that the behavior is empty. The last line is also called the status line, showing the name of the currently edited file and its status. In this example, [New File] indicates that example. c is a New File. If example. the c file already exists in the system. after you enter the preceding command, the content of the file is displayed on the screen and the cursor is placed at the first line, the file name, number of lines, and number of characters are displayed in the status line.
  
[Example 2] enter the following command:
  
$ Vi example. c
  
Then type:
  
# Include
  
Main ()
  
{Int k;
  
For (k = 0; k <3; k ++) add ();
  
}
  
Add ()
  
{Static int x = 0;
  
X ++;
  
Printf ("x = % d \ n", x );
  
}
  
  
~
  
~
  
~
  
~
  
"Example. c" [New File]
  
Note: To save space, only 15 lines are displayed on the screen. In fact, the initial number of displayed rows is related to the terminal used by the user. generally, 25 rows can be displayed on the CRT terminal. In the window system, the number of lines displayed is related to the window in which Vi is run. However, we can set the number of displayed rows. For example, on a CRT terminal that can display 25 rows, let Vi display only 15 rows. The setting method is described later in this section.
  
When you use Vi to create a new file, you can leave no file name in the Vi command. after editing the file, you must specify the file name to save the data.
  
When entering Vi, you can not only specify a file name to be edited, but also have many additional operations.
  
If you want to move the cursor to a specific row in the file after entering Vi, you can add the option + n after Vi, where n is the specified number of rows.
  
[Example 3] enter the following command:
  
$ Vi + 5 example1.c
  
The screen is displayed as follows:
  
# Include
  
Main ()
  
{Int k;
  
For (k = 0; k <3; k ++) add ();
  
}
  
Add ()
  
{Static int x = 0;
  
X ++;
  
Printf ("x = % d \ n", x );
  
}
  
~
  
~
  
~
  
~
  
~
  
"Example. c" 12 lines, 125 characters
  
The cursor will be placed on line 5th in file example1.c.
  
If you want to move the cursor to the last line of the file after entering Vi, you only need to omit the number n after the additional item "+" in the command.
  
When entering Vi, in addition to specifying a starting line number of the cursor, you can also specify a mode string in the command. after entering Vi, the cursor is on the first line in the file that matches the specified pattern string.
  
[Example 4] enter the following command:
  
$ Vi +/int example1.c
  
The screen is shown as follows:
  
# Include
  
Main ()
  
{Int k;
  
For (k = 0; k <3; k ++) add ();
  
}
  
Add ()
  
{Static int x = 0;
  
X ++;
  
Printf ("x = % d \ n", x );
  
}
  
~
  
~
  
~
  
~
  
~
  
"Example. c" 12 lines, 125 characters
  
The cursor will be placed on line 3rd in file example1.c.
  
You can use Vi to edit multiple files at the same time. you only need to write the file to be operated into the Vi command, and you can also use wildcards.
  
[Example 5] enter the following command:
  
$ Vi *. cat
  
You can edit all files suffixed with cat. Of course, you can also use the built-in pattern matching in shell to load files, which is more skillful. Once you include all the files to be edited in the command line, you can easily move them in Vi.
  
Exit Vi
  
You can use one of the following methods to exit Vi and return to shell after editing the file.
  
In command mode, the upper-case letter Z is used for two connections. if the current edited file has been modified, Vi will save the file and exit, and return to shell; if the current edited file has not been modified, Vi exits directly and returns to shell.
  
In the last line mode, enter the command
  
: W
  
Vi saves the current editing file, but does not exit, but continues waiting for the user to enter the command. When you use the w command, you can create a new file name for the editing file.
  
[Example 6]
  
: W newfile
  
At this time, Vi will save the content of the current file to the specified newfile, while the original file remains unchanged. If newfile is an existing file, the Vi prompts in the status line of the display window:
  
File exists (use! To override)
  
If you really want to replace the original content in newfile with the current content of the file, you can use the command
  
: W! Newfile
  
Otherwise, you can select another file name to save the current file.
  
In the last line mode, enter the command
  
: Q
  
The system exits Vi and returns to shell. If the editing file is not saved when you exit Vi with this command, the last line of the Vi display window displays the following information:
  
No write since last change (use! To overrides)
  
The user is prompted that the file is not saved after modification, and then Vi does not exit, continue waiting for the user command. If you want to force exit Vi without saving the modified file, you can use the command
  
: Q!
  
Vi abandons the modification and directly returns it to shell.
  
In the last line mode, enter the command
  
: Wq
  
Vi will save the file first, and then exit Vi and return to shell.
  
In the last line mode, enter the command
  
: X
  
This command has the same functions as the ZZ command in command mode.
  
From Turbolinux
Related Article

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.