Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux Chinese text Editor vim

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Linux Chinese text editor vim. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Vim is a powerful text editor and an enhanced version of vi.

Vim [options] [file..]

The most common commands for editing a file using vim are:

Vim file

Where file can be a new file or an existing file. When such a command is executed, the editor opens to display the contents of the file file. As shown in the figure:

If it is a new file, the words "file" [new file] will be displayed on the left side of the bottom, and 0Power0-1 on the right indicates the number of lines and characters of the current cursor. If you open an old file, the words "file" 3L, 66C are displayed on the left at the bottom, indicating the file name, the line number of the current cursor, and the total number of bytes of the file. Where there are no characters in the file, it starts with the character ~.

It is not possible to edit the vim file yet. There are several modes for editing the file. The current mode is normal, where you can move the cursor and execute commands such as copy and paste. There are also two common modes: insert mode and bottom line mode.

The initial entry is usually in normal mode. Press the following key to enter the insert mode:

An enter An at the end of the line where the cursor is located, enter I at the end of the line where the cursor is located, enter I at the beginning of the line where the cursor is located, enter o at the beginning of the line where the cursor is located, add a new line under the line where the cursor is located, and enter O at the beginning of the new line to add a new line on the line where the cursor is located, and start typing at the beginning of the new line

When you enter insert mode, the word-- insert-- appears at the bottom; you can enter it at the cursor position. Return from insert mode to normal mode and press ESC.

In normal mode, press the following key to enter the bottom line mode:

: execute command / forward search? Reverse search

To return from bottom line mode to normal mode, press the ESC key twice.

Bottom line mode execution: Q (quit) indicates exit from the editor. If changes have been made to the contents of the file, you need to execute: wq (write quit) to save the exit; if you do not save the exit, you need to execute: Q! Force exit; force save exit as: wq!

Here are some commands that can be executed in normal mode and what they do:

Text modification:. Repeat the previous command x delete the character at the cursor position d cut the entire line where the dd cut cursor is located (saved in a temporary buffer) ndd n is a number, indicating that starting from the current line, cut n line p from top to bottom to put the contents of the buffer below the current line np n is a number It is equivalent to executing the p command n times to put the contents of the buffer above the current line y copy yy from the cursor to copy the current line (saved in the temporary buffer) nyy n is a number, indicating that starting from the current line, copy the n line r from top to bottom to replace a character R at the position of the cursor. And enter text input mode (ESC exit) u undo the last operation ZZ save exit move cursor: h or left arrow key cursor move left l or right arrow key cursor move one grid j or down arrow key cursor moves down one grid k or up arrow key cursor moves up one grid 0 cursor moves to the beginning of the current line ^ cursor moves to the current line non-blank characters (such as spaces, Tab key, etc.) the beginning $cursor moves to the end of the current line g _ cursor moves to the end of the current line non-blank character w cursor moves to the beginning of the next word e cursor moves to the end of the next word * matches the word where the cursor is located Move to the next same word # match the word of the cursor, move to the previous same word f {the cursor moves to the next character {on the current line {can be replaced by another character F} the cursor moves to t in the previous character} of the current line, to T one character before the next comma on the current line, and% to the other half of the parentheses in pairs of parentheses, including () {} []. You need to move the cursor over the parentheses first. Gg cursor moves to file * Line header G cursor moves to file * one line first enter cursor moves to the beginning of the next line

Only part of it is listed here, and beginners may find it too much to remember. In fact, as long as you remember a few of them, you can use them; most of the other functions are to make your operation easier and faster.

Most of the commands in vim can be combined:

For example, to delete the current position of the cursor all the way to the end of the line, execute d$

For example, to insert 50 'word', in the current location, simply execute 50iword ESC and press the ESC key and the 50 words are inserted.

For example, if you want to paste the copied content, but want to paste it 5 times, then execute 5p

For example, if you want to execute the last command five times again, execute 5.

Skillful use of these commands will greatly improve the speed of writing documents.

In insert mode (normal mode press a, I, o, etc.), enter the beginning of a word, and then press CTRL-P or CTRL-N to complete it automatically.

The bottom line mode has three start characters (:, /,?), of which / and? Used to match pattern search:

For example, search for the string centos in the file:

/ centos

Enter this string and press enter, vim will highlight all matching strings, press the n key, the cursor will jump to the next matching string, press the N key, the cursor will jump to the previous match. (think of man queries)

Use? It has the same effect as /, but in the opposite direction.

You can execute many commands, such as the save exit command described earlier: wq. Some of the bottom line mode commands are described below:

: set nu display line number: set nonu hidden line number: r file reads the contents of the file file and writes them to the currently edited file, starting at the next line of the cursor's current position. : W file writes the current edits to a new file, file. : s / pattern/string/ replaces the string that matches pattern with string: X, which has the same effect as: wq, save and exit. :! Command temporarily leaves vim and executes the shell command command. : help View help:. = Show current line number: = display total number of lines: n move the cursor to the beginning of the nth line

These commands can also be combined, such as executing the shell command and writing the result to the next line of the current line:

: rroomls-l

You can also combine commands in other modes, such as replacing all matching pattern strings on the current line:

: s/pattern/string/g # g means global

Such as replacing all matching pattern strings in this file:

:% s/pattern/string/g #% represents all lines

For example, replace the matching string of the specified line:

: n,ms/pattern/string/g

Here n and m are numbers, representing line numbers. You can use a dot. Represents the current line

For example, delete the contents from the current line to the fifth line:

:., 5d

When the current line is below the fifth line, there is a prompt for reverse deletion.

When using / pattern and: s/pattern/string, pattern is a regular expression that matches the pattern of a string.

There are some similarities between regular expressions and the wildcard characters introduced earlier (basic command introduction II), but be careful to distinguish between the two.

Wildcards are mainly used to match file names, regular expressions can not only be used to match file names, in fact, it can match any string. It is more generic than wildcards, and most programming languages and some tools (such as vim, grep, awk, sed) have direct support for regular expressions.

The following describes the concepts and usage of some of the regular expressions that will be used:

Match position: ^ indicates the beginning of the line $indicates the end of the line

< 表示单词开头 >

Indicates that the end of the word matches the character:. Means to match any single character (equivalent to? in a wildcard) [.] Represents any single character in matching parentheses [^.] Means to match any non-listed character # reference wildcard description\ a matches English characters, which is equivalent to [a-zA-Z] or [: alpha:]. \ A matches non-English characters, which is equivalent to [^ a-zA-Z]. \ d match numbers, equivalent to [0-9] or [[: digit:]]. \ d matches a non-numeric number, which is equivalent to [^ 0-9]. \ x matches hexadecimal numbers, equivalent to [0-9A-Fa-f] or [[: xdigit:]]. \ X matches non-hexadecimal numbers, which is equivalent to [^ 0-9A-Fa-f]. \ w matches the word, which is equivalent to [0-9AmurZamurz]. \ W matches non-words, which is equivalent to [^ 0-9A muri Zamurz _]. \ t matches TAB characters. \ s matches the white space character, which is equivalent to [\ t] or [[: blank:]]. \ s matches non-white space characters, which is equivalent to [^\ t]. \ u matches uppercase letters, which is equivalent to [Amurz] or [[: upper:]]. \ U matches non-uppercase letters. \ nmatch newline\ r match enter (.) Match and capture, with\ 1\ 2\ 3. To reference the captured string. | indicates logic or number of matches: * indicates that the previous character is matched zero to any number of times, equivalent to {0,}. + matches the previous character one to any number of times, which is equivalent to {1,}. ? Indicates that the previous character is matched from zero to once, which is equivalent to {0primel}. # pay attention to and wildcards? The difference {n ·m} indicates that the previous character is matched n to m times.

When using regular expressions, it is sometimes necessary to add the escape character "\" before the special character to make the special character express its literal meaning rather than its special meaning, and when using regular in a particular tool, this is also needed to prevent the special character from being interpreted by the tool itself.

Vim needs to escape special characters when using the following regular expressions:\,\ {NMagol m},\ (...\),\?,\ +,\ | the following examples are given to illustrate pattern matching and some command usage in vim.

Match the string world and pause the cursor at the beginning of the third line after the matching line:

/ world/+3

Add a comment symbol / / at the beginning of lines 3 to 8:

: 3pr 8s / ^ /

Note the use of the line initiator ^ and the escape character'\ 'here.

Such as:

:% g / ^\ sxyz/normal dd

The purpose of this command is to globally match lines that begin with blank followed by xyz, and execute the command dd in normal mode

Such as matching more than 6 lowercase letters:

/\ a\ {6,}

Such as swapping colons: strings on both sides:

: s /\ (. *\):\ (. *\) /\ 2:\ 1 / # Note how the previously matched grouping is referenced here

If all tag, tog and tug are changed to hat, hot and hut respectively

:% slap\ ([Aou]\) g 1t/g h\

Such as matching the words hello or world:

/\\ |\

Only some of the regular expressions used by vim are listed here, and more about regularities will be described and illustrated in future articles.

This is the end of the article on "how to use vim for Linux Chinese text Editor". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report