Ubuntu tool: vi editor and ubuntuvi Editor

Source: Internet
Author: User
Tags add numbers

Ubuntu tool: vi editor and ubuntuvi Editor
Vi Introduction

Vi is short for "Visual interface", and its position on Linux is as if the Edit Program is on DOS. It can perform output, delete, search, replace, block operations, and many other text operations, and users can customize it according to their own needs, which is not available in other editing programs.

Vi is not a typographical program. Unlike Word or WPS, it can orchestrate fonts, formats, paragraphs, and other attributes. It is just a text editing program.

Vi has no menu, only commands, and many commands. Vi has three basic working modes: command line mode, text input mode, and last line mode.

1. Command Line Mode

At any time, no matter what mode the user is in, just press the key to bring Vi to the command line mode. In the shell environment (the prompt is $), enter the command to start Vi, the editor is also in this mode.

In this mode, you can enter various valid Vi commands to manage your own documents. At this time, any character entered on the keyboard is interpreted as an editing command. If the entered character is a valid Vi command, Vi completes the corresponding action after receiving the USER command. Note that the entered command is not displayed on the screen. If the entered character is not a valid command of Vi, Vi will ring an alarm.

Move the cursor;

When we Press ESC to Enter Command mode, we can use the following key bits to move the cursor;

J. Move one line down; k moves one line up; h moves one character to the left; l moves one character to the right; ctrl + B moves one screen up; ctrl + f moves one screen down; move up arrow up; move down arrow down; move left arrow to left; move right arrow to right;

When editing a file, you can add numbers before the j, k, l, and h keys, such as 3j, to move three lines down.


Text input mode

In command mode, Enter command I, attach command a, open command o, modify command c, replace command r, or replace command s to enter the text input mode. In this mode, any character entered by the user is saved as the file content by Vi and displayed on the screen. During text input, if you want to return to command mode, press the key. I insert before the cursor; a Insert after the cursor; I insert at the beginning of the row where the cursor is located; A Insert at the end of the row where the cursor is located; o insert a line above the row where the cursor is located; O insert a line below the row where the cursor is located; s delete a character after the cursor and enter the insert mode; s. Delete the row where the cursor is located and enter the insert mode; Delete text content; X is a single character; # x is used to delete several characters, and # represents a number, for example, 3x; dw is used to delete a word; # dw is used to delete several words, # It is represented by a number, for example, 3dw indicates deleting three words, dd deletes one row, dd deletes multiple rows, and # indicates numbers. For example, 3dd indicates deleting the cursor row and the cursor's next two rows; d $ Delete the content from the cursor to the end of the line; J clear the space between the row and the last line of the cursor, and link the cursor row with the previous line; Resume modification and delete operations; U undo the modification or deletion operation;

Press ESC to return to Command mode, and then press u to cancel the previous deletion or modification. If you want to cancel multiple previous modification or deletion operations, press the number of times u. This is not much different from the Word Undo operation;



Last Row Mode

The last row mode is also called the ex escape mode.

The Vi and Ex editors have the same functions. The main difference between them is the user interface. In Vi, a command is usually a single key, such as I, a, o, etc. In Ex, the command is the text line ending with the Enter key. Vi has a dedicated "escape" command to access many line-oriented Ex commands. In command mode, you can press the ":" key to enter the last line mode. At this time, Vi will display a ": as the last line prompt, waiting for the user to enter the command. Most file management commands are executed in this mode (for example, writing the content in the editing buffer to a file is moderate ). After the last-line command is executed, Vi automatically returns to the command mode. For example:

: 1, $ s/A/a/g

Replace uppercase A with lowercase a from the first line to the end of the file.

If you have changed your mind when entering the command in the last line mode, press the button or use the return key to delete all input commands, and then press the return key, the Vi can be returned to the command mode.



Vi Entry and Exit

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

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

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 cursor will be placed on line 5th 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.

Resume modification and delete operations;

U undo the modification or deletion operation;

Press ESC to return to Command mode, and then press u to cancel the previous deletion or modification. If you want to cancel multiple previous modification or deletion operations, press the number of times u. This is not much different from the Word Undo operation;

Visual mode;

In the latest Linux release version, vi provides the visual mode, which is only available in vim. If the vi you use does not have this function, replace it with vim. Enable visual mode, Press ESC, and then press v to enter visual mode;

The visual mode provides us with an extremely friendly selection of text ranges for highlighted display; at the very bottom of the screen, there are;

-- VISUAL -- or -- VISUAL --

In the visual mode, we can use the cursor movement command in the command line mode mentioned above to select the text range.

What is the purpose of selecting a text range?

We can delete a job and press the d key to delete the selected content. After the selected content is selected, Press y to copy the content; press d to delete the content;

It is worth mentioning that deleting also means copying. We return to the command mode, move the cursor to a certain position, and press shift + p to paste the deleted content. Let's start with a sentence here. In the next article, we have to talk about it in detail.

Exit the visual mode, or use the ESC key;

3.9 copying and pasting;

In fact, deletion also involves cutting. When we delete the text, we can move the cursor somewhere and press shift + p to paste the content to the original place, then move the cursor to a certain place, and then press p or shift + p to paste it again;

P paste the post after the cursor; shift + p paste the post before the cursor

For example:

For example, if we want to copy the third line of a document and post it to the end of the fifth line, what should we do?

There are two methods;

Method 1:

First Delete the third line, move the cursor to the third line, then use the dd action, and then press shift + p. In this way, the third line of the deleted post is located in the original place.

Then we move the cursor to the fifth line with the k key, and then press the p key to post the content of the third line to the end of the fifth line;

Method 2;

Enter the visual mode, Press ESC, and then press v. Move the mouse pointer, select the content of the third line, and press y to copy; move the pointer to the fifth line, and then press p;

Therefore, the copy and paste operations are the combined use of command mode, insert mode, and visual mode. We need to learn how to switch between various modes, and usually use the ESC key; more importantly, move the cursor in command mode;

About the row number;

Sometimes, when we configure a program to run, an error occurs in Line X of the configuration file. At this time, we need to use row number-related operations;

Add row numbers for all content;

Press ESC and enter:

:set number

Cursor Position

In the lower right corner of the screen, there are similar;

         57,8 27%

In this example, 57 represents 57th rows, and 8 represents 8th characters;

Search and replace functions;

Search;

First, we need to enter the ESC key to enter the command mode. We enter/or? The search mode is displayed;

/SEARCH Note: Forward SEARCH, press n to move the cursor to the next qualified place ;? SEARCH Note: For reverse SEARCH, press shift + n to move the cursor to the next qualified

For example:For example, if I want to find the swap word in a file, I should do the following;

First Press ESC to Enter command mode, and then enter;

/Swap or? Swap

Replace;

Press ESC to enter the command mode;

: S/SEARCH/REPLACE/g note: REPLACE the SEARCH word in the row where the current cursor is located with REPLACE, and highlight all the SEARCH;: % s/SEARCH/REPLACE note: REPLACE all the SEARCH in the document with REPLACE;: #, # s/SEARCH/REPLACE/g note: # indicates a number, indicates the number of rows to the number, and replaces SEARCH with REPLACE;

Note: In this case, g indicates global SEARCH. We noticed that the SEARCH will be highlighted even if there is no replacement;

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.