Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The UNIX vi editor uses wizards


May 23, 2021 UNIX Getting started


Table of contents


The vi editor uses wizards

In the UNIX operating system, there are many types of document editors, the friendliest of which is vi. The document editor allows users to perform context-based editing in a file.

Users can now use the vi document editor's upgraded version of VIM. Here, M in VIM is what the improvement means.

vi is often considered the implementation standard for UNIX editors for the following reasons:

  • Works in almost all UNIX series operating systems.
  • Its specific implementation is very similar.
  • It takes up very little resources.
  • The user interface is more user-friendly than other editors.

Users can use the vi editor to edit files that already exist, and of course they can use it to create a new file. I n addition, users can use it to browse a text file.

Get started with the vi editor

There are several ways users can turn on the vi editor:

Instructions Describe
vi filename If the file does not exist, create the file or open it
vi -R filename Open an existing file in a read-only manner
view filename Open an existing file in a read-only manner

Here is an example of creating a new file testfile, provided, of course, that the file does not exist in the current directory.

    $vi testfile

The user will then see the following on the screen:

    |
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    ~
    "testfile" [New File]  

At this point, it should be noted that there will be a wavy sign at the beginning of each line, which represents that the line is not being used. I f a line does not begin with a wavy sign, this means that a space exists, or it may be a line break or other symbol that is not easily visible.

So before we officially use the vi editor, let's understand some small but important ones.

The operating mode

When using the vi editor, you typically switch back and forth between the following two modes:

  • Instruction mode: In this mode, the user can perform administrative functions such as saving files, executing instructions, moving cursors, cutting or pasting a line (characters), finding or replacing them. In this mode, everything the user types is executed by the system as an instruction.
  • Insert mode: In this mode, the user can type text into the file. I n this mode, everything the user types is taken as file input and eventually saved in text.

The vi editor is usually opened in instruction mode. I f the user wants to type text, the user must switch the current mode to insert mode. W hen switching mode to insert mode, the user can simply press the i key. A way from insertion mode, you can press the Esc key.

It is important to note that if the user does not know which mode they are currently in, the user can press the Esc key twice and the editor returns to instruction mode. U sers can use the vi editor to open a text, type some characters, and then switch to instruction mode to experience the difference before and after.

Leave the vi editor

The instruction to leave the vi editor :q . I n instruction mode, once you type the colon followed by the letter q, click Enter. I f the document is modified, the editor reports a prompt. U sers can use :q if they don't want to :q! This instruction can leave the vi editor without saving the changes.

When you save instructions for a :w U sers can combine it with the departure instructions above, such as :wq and then click Enter.

The quickest way to save file updates and exit is the ZZ directive. When a user types a zz instruction in instruction mode, the effect is equivalent :wq

Users can also save by specifying a specific file name after re:w to specify a different file name. F or example, if the user wants to save the filename2, the user can type : w filename2 and click Enter.

Move the cursor within the file

In order to move the cursor within the file without affecting the text content, the user must operate in instruction mode (pressing the Esc key twice in a row). Here are the instructions for moving the cursor within the file:

Instructions Describe
K Move one line up
J Move one line down
H Move the cursor one character to the left
l Move the cursor one character to the right

Here are two key points to note:

  • The vi compiler is case sensitive, so users should pay attention to case when operating in instruction mode.
  • Most instructions in the vi editor can indicate the number of times you want to occur by adding a number before the action behavior. For example, 2j means you want the cursor to move down two lines.

There are other ways to move the cursor within a file. I t is important to note that you must operate in instruction mode. H ere are other ways to move the cursor within the file.

Instructions Describe
0 or l The cursor returns to the top of the line
$ The cursor returns to the end of the line
W The cursor moves to the next word
B The cursor moves to the previous word
( The cursor moves to the beginning of the current sentence
) The cursor moves to the beginning of the next sentence
E The cursor moves blank to split the end of the word
{ The cursor moves back for a period of time
} The cursor moves forward for a period of time
[[ The cursor moves one section back
]] The cursor moves forward by one section
n| The cursor moves to the n column of the current row
1G The cursor moves to the first line of the file
G The cursor moves to the last line of the file
nG The cursor moves to line n of the file
:n The cursor moves to line n of the file
Fc The cursor moves forward to c
Fc The cursor moves back to c
H The cursor moves to the first part of the screen
Nh The cursor moves to the screen from the top to check line n
M The cursor moves to the middle of the screen
L The cursor moves to the bottom of the screen
nL The cursor moves up the nth line from the bottom of the screen
:x The cursor moves to a line with the line number x

Control instructions

Here are some instructions to use with the Control key:

Instructions Describe
CTRL+d Move half the screen forward
CTRL+f Move the entire screen forward
CTRL+u Move half the screen back
CTRL+b Move the entire screen back
CTRL+e Move the screen up one line
CTRL+y Move the screen down one line
CTRL+u The screen moves half a page up
CTRL+f The screen moves a page down
CTRL+l Redraw the screen

Edit the file

Text can only be edited in insert mode. There are many ways to switch from instruction mode to insert mode:

Instructions Describe
Insert text before the current position
Insert text at the top of the current line
A Insert text after the current position
A Insert text at the end of the current line
O Create a new line below the cursor position to enter the text
A Create a new line above the cursor position to enter the text

Remove the character

Here's a list of instructions for removing characters or lines from an open file:

Instructions Describe
Remove characters from the cursor position
Remove the character before the cursor position
Dw Remove all characters from the cursor position to the next word
d^ Remove all characters from the cursor position to the top of the line
d$ Remove all characters from the cursor position to the end of the line
D Remove all characters from the cursor position to the end of the current line
Dd Delete an entire line

As mentioned earlier, most instructions in vi can be preceded by numbers to indicate the number of times you want to execute. F or example, 2x means that two characters under the current cursor position are deleted, and 2dd means two lines are deleted.

This tutorial recommends that you practice more of the above before you learn more about the following.

Change the instruction

Users can make changes to characters, words, and lines without deleting text. Here are the instructions:

Instructions Describe
Cc Delete the current line, leaving only the text typed by the user
cw Delete the word on which the cursor is located and enter insert mode
R Replace the characters under the cursor and vi returns to instruction mode after the replacement ends
R Override multiple characters at the current cursor, and only with Esc can you stop overcase
s Replace the current character with the character typed by the user, after which it is still in insert mode
S After deleting the line where the cursor is located and replacing it with the text typed by the user, the system is still in insert mode

Copy and paste instructions

Users can copy a line or word from one place and paste it elsewhere, with instructions as follows:

Instructions Describe
Yy Copy the current row
yw Copy the current word
P Paste behind the cursor
P Paste in front of the cursor

Advanced instructions

There are some advanced instructions to simplify day-to-day editing:

Instructions Describe
J The current row wants to connect to the next line, and defining a number can connect many rows
The current line jumps to the left, the width of a shift
>> The current line jumps to the right, the width of a shift
~ Under the current cursor, case switches
U Return the current line to the state where the cursor has just arrived
u Undoing the last change to the file and typing u again is to re-implement the update
:f Half of the current cursor and the total number of file name-level files are displayed on the screen
:f filename Rename the current file to filename
:w filename Write to filename
:e filename Open another file filename
:cd dirname Switch the current directory to the directory dirname
:e # Switch between two open files
:n In the case of using vi to open multiple files, the user uses this instruction to switch to the next file at a time
:p In the case of using vi to open multiple files, the user uses this instruction to switch to the last file at a time
:N In the case of using vi to open multiple files, the user uses this instruction to switch to the next file at a time
:r file Read the file file and enter insert mode after the current line
:nr file Read the file file and then enter insert mode on the n-line after the current line

Word or character search

The vi editor searches in two ways: strings and characters. F or string searches, you need to use / Instructions. W hen the user starts typing these instructions, they appear at the bottom of the screen so that the user can join the specified string to search.

The two instructions differ only in search direction:

  • / Instructions are searched from top to bottom.
  • Search from bottom up when the instruction is in.

The n and N instructions are used to repeat the above search instructions in the same or opposite direction. I n search instructions, some strings have a special meaning. You need to use the transdes.

Instructions Describe
^ Start the search at the beginning of the line
. Matches a single character
* Matches 0 or more preceding characters
$ Start the search at the end of the line
[ Start a matching or mismatched expression
In an expression, find the beginning or end of a word
> Refer to the above

Set instructions

Users can change the look and friendliness of the vi interface by following instructions: set instructions. T he following specifies that you must type in instruction mode.

Instructions Describe
:set ic Case is ignored when searching
:set ai Set auto-indentation
:set noai Do not set auto-indentation /td>
:set nu The line number is displayed on the left
:set sw Sets the width of the tab. For example, setting sw=4 means setting the tab width to 4
:set ws How to cycle through the search is set, and if it is not searched at the bottom of the file, the search starts again from the beginning of the file.
:set wm If this option has a value greater than zero, the editor will "line up."
:set ro Change the file read and write type to read-only
:set term The output terminal type
:set bf Ignore the control character

Run the instruction

Vi The editor can run the instruction set. T o do this, the user must enter the :! mode.

For example, if the user wants to detect if the file already exists before saving :! ls then the user can see the output of the ls instruction on the screen.

When you press any key, you return to the vi interface.

Replace the text

:s/ can quickly replace words or a set of words. Here's the syntax:

    :s/search/replace/g

g stands for global. The instruction means that all matching characters that appear on the line where the cursor is located are replaced.

Attention

Here are some important tips when using the vi editor:

  • The user must type the instruction in instruction mode (press the Esc key twice in a row to ensure that it is currently in instruction mode).
  • The user should pay attention to the case of the instruction.
  • Users can only type text in insert mode.