In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the vi command in Linux. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
The vi editor is the standard editor for all Unix and Linux systems, and it is as powerful as any of the latest text editors.
What is vim?
Vim is a text editor developed from vi. Code completion, compilation and error jumping are particularly rich in convenient programming functions and are widely used among programmers.
Simply put, vi is an old-fashioned word processor, but it has all the features, but there are still areas where it can be improved. Vim is a useful tool for developers.
Even vim's official website (www.vim.org) itself says vim is a programming tool, not a word processor.
vim keyboard diagram:
Use of imgvi/vim
Basically vi/vim is divided into three modes: Command mode, Insert mode and Last line mode. The three models are:
Command mode:
As soon as the user starts vi/vim, he enters command mode.
In this state, keyboard strokes are recognized by Vim as commands rather than input characters. For example, if we press i, we don't enter a character, i is treated as a command.
Here are some common commands:
i Switch to input mode to input characters. x Delete the character at the current cursor position.: Switch to bottom-line command mode to enter commands on the bottom line.
To edit text: Start Vim, enter command mode, press i, switch to input mode.
Command mode has only the most basic commands, so you still have to rely on bottom-line command mode to enter more commands.
input mode
Pressing i in command mode enters input mode.
In input mode, you can use the following keys:
Character key and Shift combination, enter character ENTER, Enter key, Linefeed BACK SPACE, Backspace key, Delete cursor one character before DEL, Delete key, Delete cursor one character after arrow key, Move cursor HOME/END in text, Move cursor to beginning/end of line Page Up/Page Down, Page Up/Down Insert, Switch cursor to enter/replace mode, Cursor will change to vertical line/underline ESC, Exit input mode, Switch to command mode Bottom line command mode
Pressing: in command mode enters baseline command mode.
Bottom line command mode allows you to enter single or multi-character commands, and there are many available commands.
In baseline command mode, the basic commands are (colons have been omitted):
q exit program w save file
Press ESC to exit baseline command mode at any time.
Simply put, we can think of these three patterns as the following icons:
imgvi/vim usage examples Use vi/vim to enter general mode
If you want to use vi to create a file called runoob.txt, you can do this:
$ vim runoob.txt
Enter vi file name directly to enter vi general mode. Please note that remember to add the filename after vi, whether the file exists or not!
img Press i to enter input mode (also known as edit mode) and begin editing text
In normal mode, just press i, o, a and other characters to enter the input mode!
In edit mode, you can find the words-INSERT-appear in the status bar in the lower left corner, which is a prompt that you can enter any character.
At this point, except for Esc, all the other keys on the keyboard can be regarded as normal input buttons, so you can edit anything.
img Press ESC button to return to normal mode
Well, assuming I've edited it in the style above, how do I exit? Yes! That's right! Just press Esc for him! Soon you'll notice that-INSERT-in the bottom left corner of the screen is missing!
In normal mode press:wq save and leave vi
OK, we want to save, save and leave the command is very simple, enter:wq can save to leave!
img
OK! We have successfully created a runoob.txt file.
vi/vim key description
In addition to the simple example i, Esc, :wq above, vim has a lot of buttons to use.
Part I: Cursor movement, copy and paste, search and replace methods available in general mode
h or left arrow key (←) cursor moves one character to the left j or down arrow key (DOWN) cursor moves one character down k or up arrow key (↑) cursor moves one character up l or right arrow key (→) cursor moves one character to the right If you place your right hand on the keyboard, you will find hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, move down 30 rows, you can use the combination key of "30j" or "30 DOWN," that is, after adding the number of times you want to move (number), press the action!
[Ctrl] + [f] screen "down" to move a page, equivalent to [Page Down] key (common)[Ctrl] + [b] screen "up" to move a page, equivalent to [Page Up] key (common)[Ctrl] + [d] screen "down" to move half a page [Ctrl] + [u] screen "up" to move half a page + cursor to move to the next line of non-space characters-cursor to move to the next line of non-space characters n means "number", such as 20. Pressing the number followed by the space bar moves the cursor to the right n characters in the line. For example, 20 will move the cursor 20 characters backward. 0 or function key [Home] This is the number "0": move to the first character of the line (common)$or function key [End] move to the last character of the line (common)H cursor move to the first character of the line at the top of the screen M cursor move to the first character of the line in the center of the screen L cursor move to the first character of the line at the bottom of the screen G move to the last line of the file (common)nGn is a number. Move to line n of this file. For example, 20G will move to the 20th line of this file (can be combined with:set nu)gg to move to the first line of this file, equivalent to 1G ah! nn is a number. Cursor down n lines (common) Search Replace
/word Look under the cursor for a string named word. For example, to search for the vbird string in a file, type/vbird! (usually)? word Look above the cursor for a string named word. N is the English key. Repeat the action of the previous search. For example, if we had just executed/vbird to search down for the string vbird, pressing n would continue searching down for the next string named vbird. What if it is executed? vbird, then press n to continue searching upward for the string named vbird! N This N is the English key. In contrast to n, the previous search action is performed for "reverse." For example, after/vbird, pressing N means "up" search vbird. Using/word with n and N is very helpful! It allows you to repeat some of the keywords you search for!
:n1,n2s/word1/word2/gn1 and n2 are numbers. Find the string word1 between lines n1 and n2 and replace it with word2 ! For example, searching for vbird between lines 100 and 200 and replacing it with VBIRD would result in: ":100,200s/vbird/VBIRD/g." (Common):1,$s/word1/word2/g or:%s/word1/word2/g Find the word1 string from the first line to the last line and replace it with word2! (common):1,$s/word1/word2/gc or:%s/word1/word2/gc Find the word1 string from the first line to the last line and replace it with word2! And before replacing display prompt character to user confirm whether need to replace! Delete, copy and paste
x, X In a line of words, x is to delete one character backward (equivalent to the [del] key), X is to delete one character forward (equivalent to the [backspace] key) (commonly used)nxn is a number, continuously deleting n characters backward. For example, I want to delete 10 characters in a row,'10x'. dd Delete the entire row where the cursor is located (common)nddn is a number. Delete the cursor down n lines, for example 20dd is to delete 20 lines (commonly used)d1G delete cursor to the first line of all data dG delete cursor to the last line of all data d$delete cursor location, to the last character of the line d0 that is a number 0, delete cursor location, to the first character of the line yy copy cursor location line (commonly used)nyyn is a number. Copy the cursor down n lines, for example 20yy is copy 20 lines (commonly used)y1G copy cursor line to the first line of all data yG copy cursor line to the last line of all data y0 copy cursor character to the first line of all data y$copy cursor character to the end of the line of all data p, Pp is to copy the copied data in the cursor next line paste, P is paste on the cursor line! For example, I am currently at line 20 and have copied 10 lines of data. After pressing p, the 10 rows of data will be pasted after the original 20 rows, i.e. starting from row 21. But what if you press P? The original 20th line is pushed to 30. J combines the row where the cursor is located with the data in the next row into the same row c repeatedly deletes multiple data, for example, deletes 10 rows downward,[ 10cj ]u restores the previous action. Ctrl +r Redo the previous action. This u and [Ctrl]+r are very common commands! One is restore, the other is redo ~ use these two function keys, your edit, hehe! Very happy!
. Do not doubt! This is the decimal point! It means repeating the previous action. If you want to double-delete, double-paste, etc., press the decimal point. It's all right! (commonly used)
show details
Part II: General Mode Switch to Edit Mode Available button descriptions Enter input or replace edit mode
i, I Enter Insert mode: i is "Input from the current cursor position", I is "Input from the first non-space character in the current row". (Commonly used)a, A Enter the input mode (Insert mode): a is "input from the next character where the current cursor is located", A is "input from the last character of the row where the cursor is located". o, O Enter Insert mode: This is the upper case of the English letter o. o is to enter a new line at the next line where the current cursor is located; O is to enter a new line at the previous line where the current cursor is located! (Common)r, R enters Replace mode: r replaces the cursor character only once;R replaces the cursor character until ESC is pressed;(Common) Among the above keys, the words "-INSERT-" or "-REPLACE-" appear in the lower left corner of the vi screen. You know the name of the action! Special note is that we also mentioned above, you want to enter characters in the file inside, be sure to see INSERT or REPLACE in the lower left corner to enter Oh!
[Esc]Exit edit mode and return to normal mode (common)
show details
Part III: General Mode Switch to Command Line Mode Available Buttons Description Command Line Store, Leave, etc.
:w Write edited data to hard disk file (common):w! If the file attribute is "read-only," force the file to be written. However, whether you can write it or not depends on your file permissions for the file!: q leave vi (often):q! If you have modified the file and do not want to save it, use it! Do not save files for forced departure. Pay attention to that exclamation point! In vi, there is often the meaning of "compulsion"~
:wq Save and leave if:wq! ZZ This is a capital Z! If so, save the current file and exit! The effect is equivalent to (save and exit)ZQ does not save, forced exit. Effect equivalent to:q!.: w [filename] Save edited data as another file (similar to Save New File):r [filename] Read data from another file in edited data. Also, add the filename to the line where the cursor is located:n1,n2 w [filename] Save the contents of n1 to n2 as filename.:! command temporarily leave vi to command line mode to execute command display results! For example:! ls /home 㠀�You can see the file information output as ls under/home in vi! Changes to vim environment
:set nu Display line number, after setting, will display the line number at the prefix of each line:set nonu is the opposite of set nu, to cancel the line number!
Note in particular that in vi/vim, numbers make sense! Numbers usually mean a few repetitions! it may also mean something that represents the number of people to go to.
For example, if you want to delete 50 rows, you should use 50dd, right? Add the numbers before the action. What if I want to move down 20 lines? That's "20J" or "20 DOWN."
Thank you for reading! About "how to use vi command in Linux" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.