[Linux] vi-classic usage FAQ

Source: Internet
Author: User
0.0-introduction-how to use this FAQ

This document is divided into several parts. First, sections 0th and 1st introduce what VI is, and section 2nd introduces many new
Some people who do not have much experience with VI often ask these questions, including "command mode and insert
Differences between modes and problems such as "how to copy and paste". Section 3rd is intended for intermediate users of VI.
The question "how do I find and replace" begins and goes deeper until we discuss the usage of macro (macro) in VI. 3rd
This section also contains a quick reference for VI, which has a standardized VI command list. Next, it is a reference for the ": Set" command.
The list contains all variables that can customize the VI environment. These variables can also be defined in the. exrc file.

We have verified the quick reference on a machine running SunOS and ucb vi. Apart from SunOS VI,
Each command can be run in System V and ucb vi, but I personally did not verify this.

Unless specified in the document, we assume that you are in command mode by default.

We also try to keep as many terms as possible, because these terms are used in the original VI document, even though I may
Forget its original meaning.

If you use the World Wide Web (WWW) to read the FAQ, you can find this FAQ:
Http://bbs.cn90.net/read.php? Tid = 14596 & page = 1

0.1-Index

First file:

0.0-introduction-how can I use this FAQ?
0.1-Index
0.2-can I share this FAQ?
0.3-what can you do for this FAQ?

1.0-What is Vi?
1.1-is there any major transaction about Vi? Why do everyone use it? Further, why should I use it?
1.2-oh! This sounds good! Is there any reason not to use Vi?
1.3-how many different operating systems can VI run under?
1.4-okay, you convinced me. I decided to start using vi. Where should I start?
1.5-Does VI have other available variants?

2.0-vi
2.1-is there any game that helps us learn Vi?
2.2-what is the difference between the command mode and the insert mode?
2.3-Wait. My keyboard has no keys. What should I do?
2.4-those ~ What is s?
2.5-I am not used to using hjkl. Do you have any suggestions?
2.6-how can I exit without saving the disk?
2.7-how do I insert a file?
2.8-how do I find text?
2.9-how do I search for a control sequence?
2.10-how do I format the text again?
2.11-how do I copy text?
2.12-ah! I knocked on a DG just now, and my paper was gone! What should I do?
(Or, what should I do if I make a mistake just now ?)
2.13-I am writing my paper. I was told that I must put each section in a different file. What should I do?
2.14-ALL: How are commands handled?

3.0-how to find and replace it?
3.1-how do I run a program in VI?
3.2-ah! I'm writing my paper, and the system crashes! What should I do?
3.3-is there any tips to make vi more friendly to programmers?
3.4-Macro -- how do I write Macros?
3.5-how do I define a function key as a macro?
3.6-is there a way to abbreviated text?
3.7-how do I perform a spelling check in the current document?
3.8-I have a hard copy terminal on hand. Can I still use Vi?
3.9-Okay. Why is it in the open mode ?! However, I do not use a hard copy terminal.
Open Mode?

The second file:

4.0-where is the online help of VI files?
5.0-fun VI tips, boring macros
5.1-fun VI tips
5.2-fun macro
6.0-quick reference to the alphabetic vi
6.1-command mode input option (: Command)
6.2-Set)
7.0-create a. exrc File
7.1-sample of the. exrc File
8.0-VI Vulnerabilities
9.0-Glossary
10.0-bibliography on VI

0.2-can I share this FAQ?

Yes! As long as you haven't made any changes to it, or use it to make money.

0.3-what can you do for this FAQ?
First, let me know your mistakes. If you have any suggestions, we are also very welcome. What else do you think is unclear?
We are also welcome.
Also, use Vi! (Spread the word) I'm sorry, I cannot list anyone who contributes to this FAQ.
Come on, because many people have given me suggestions and help. If I list all of them, I am afraid this contribution will be made.
The list of users will be as long as the document itself.

1.0-What is Vi?
VI is a visual editor (VI means visualization-visual). So what is a visual editor?
(Compared to a non-visual editor )? A visual editor allows you to view the text when editing the text.
Today, it sounds very common. On the contrary, non-visual editors seem strange. Examples of non-visual editors
There are many examples: such as Ed, sed, and edlin (it was the last editor in DOS for a relatively short period of time)
And so on.
VI was written by William qiaoyi as part of bsd unix. Later at&t began to use VI, so standard UNIX started.
VI is used.

1.1-is there any major transaction about Vi? Why do everyone use it? Further, why should I use it?
VI is the default editor under UNIX, so almost all UNIX has been bound with VI (recently around 1984 ).
This means that whenever you use Unix across platforms, you know that there is a powerful editor at your fingertips. Why?
Nothing else? VI is a powerful editor. Similarly, once you understand Vi, you can quickly edit your file because it
It can save the number of times you press the key. because it uses different modes in both insert and command cases
The editor based on this mode is faster. Besides, VI is very small (the version above my machine is only 200 K). Also, Vi can almost do it.
Everything, as long as you know how to make it do what you want to do.

1.2-oh! This sounds good! Is there any reason not to use Vi?
There is a very good reason: VI is a little difficult to learn. If you start learning, VI is slow and painful,
Of course, once you have learned it, VI will be much faster. But the learning process is very slow. Someone once asked me if VI is a comparison.
Is the easy-to-learn editor intuitive? I generally replied:/"yes, some of us think so.
But most people think we are crazy ./"

1.3-how many different operating systems can VI run under?
Unix. That's it. Despite this, there are still many variants of VI running on different operating systems.
VI: Unix, DOS, OS/2, and MAC system 7 have been used in the operating system. (A list of details is provided later)

1.4-okay, you convinced me. I decided to start using vi. Where should I start?
Well, the tricky guy. There are a lot of good books about VI: Most Unix-speaking books have at least one chapter for VI, and
There are a lot of books dedicated to VI. I have no preference, so you 'd better ask the VI masters there.
Where did you learn? And try it everywhere. Open an unimportant document with VI (your paper is not like a good experiment for learning VI ),
Then try it everywhere. I can't imagine someone learning VI, but it doesn't take a lot of time to try it everywhere. Remember: if you have some
I don't understand where it is. Just press the key twice and you will return to the command mode again.
One thing I want to mention is that I want to make it more appropriate here, that is, if you want to become a system administrator or other
If you want to modify it, you 'd better learn ed or ex, because some versions of UNIX do not put VI in the root partition, So you
It may be impossible to implement. Ed is a good choice.

1.5-Does VI have other available variants?
Only one part can be listed: stvi (Stevie), Elvis, vile, Vim, and nvi, XVI.
Elvis is applicable to: Amiga, DOS, OS/2, UNIX, VMS, Atari.
Stevie applies to: Atari St, DOS, UNIX, MAC system 7.
MAC system 7 can be obtained from many Info-Mac image sites, under/info-MAC/text.
(For example, ftp.hawaii.edu)
Nvi is a vi that is bound with BSD 4.4.
Vim is applicable to: Amiga, DOS, MAC system 7, UNIX.
Amiga, DOS, and source code can be exchanged at the following sites: ftp.fu-berlin.de/MISC/Editors/Vim
MAC system 7 can be obtained from many Info-Mac image sites, under/info-MAC/text.
(For example, ftp.hawaii.edu)
Vile is applicable to DOS, OS/2, UNIX, and VMS.
XVI is applicable to DoS and UNIX.
There are many differences between the variants of vi. Many of them have improved VI. Most of them can be used in this document.
But there are some differences. For more details, refer to these variants.
Attached Documents.

2.0-vi
For beginners of VI, there are some basic tips. First, there must be a command summary at all times.
We will provide a quick reference/command summary later. 2. Find a better VI book.
This document is not the best way to learn VI (at least not yet). I cannot guarantee that this document can teach
No one knows about VI, and many books have already done this. However, we have provided a lot
Tip. There is a standard principle for choosing a book: read this book carefully before buying it.
Whether it makes sense to you. Be sure it can be used when you practice VI. make a comparison with other books-no matter what,
VI may be confusing to you, and you need to be sure that it is a book that makes you confused, not
You underestimated the difficulty of learning VI.
Also, find software such as/" VI/" (vilearn) And/"VI wizard/" (vitutor) to help you master
Basic knowledge of VI.

2.1-is there any game that helps us learn Vi?
This seems boring, but in fact there are many games in UNIX systems that can help you learn how to use VI.
I do not know which game can help you learn all the commands of VI, but I
I do know that there are some things that can help you learn how to use hjkl to move the cursor. nethack, a rogue-like game,
This is very helpful, and it is a big game that can be played for a long time. I didn't degrade other games.
These games are rogue, Moria, Omega, worm, and Snake.

2.2-what is the difference between the command mode and the insert mode?
VI separates the command mode from the insert mode, which is often considered a major problem of VI, but this is often considered
The advantage of VI is that the difference is that the key to understanding VI is that VI is started in command mode.
Next, we can move around in the file, change a specific area of the text, cut, copy and paste the text, and more
Multiple. The insert mode means that the user can actually insert text. In other words, the command mode is used to move in the file, while the insert
The mode is used to type text in the file.
Some commands, such as A, I, C, C, O, O, and others, can be switched from the command mode to the insert mode.
The key or ^ C (crtl + C) can be returned to the command mode out of the insert mode.
You need to adapt to this difference. It makes VI different from other editors. It also enables you not to leave the standard on your hand.
The keyboard does many things.

2.3-Wait. My keyboard has no keys. What should I do?
Try ^ [(CTRL + [). If your keyboard has a key, try again. If not, try again ^ 3.

2.4-those ~ What is s?
They are only used to let you know where your document ends. They are not actually in your document, so
You don't need to worry about them.

2.5-I am not used to using hjkl. Do you have any suggestions?
First, you can use the arrow keys if your terminal is correctly set and your connection is normal. However,
If you think you need to use VI frequently, you 'd better get used to hjkl because they are faster,
Sometimes the connection goes wrong, and the ESC sequence may be lost. (You may not be able to use the arrow keys)

2.6-how can I exit without saving the disk?
: Q! This can be done. If VI looks frozen, you are sure you have not typed ^ s. If you typed ^ Q, you can unlock ^ s.

2.7-how do I insert a file?
: R
For example, to insert a file/etc/motd, you can press R/etc/motd.
This inserts the file into the cursor. If you specify a number before R, this command inserts the file
To that location.

2.8-how do I find text?
/Search forward ,? Search backward .?? Or // repeat the last query. in UNIX,
It is a cute standard usage. In VI, N will find the next appearance position of the text. N will repeat the last look,
However, the search direction is reversed. You can use the Reverse Method in the search.

2.9-how do I search for a control sequence?
/^ V ^
^ V tells VI to literally understand the next character. do not regard it as a command.

2.10-how do I format the text again?
If your computer has a program named FMT, all you need to do is to press it in command mode !} FMT (Remember,
Do not add :). This will reformat your file, from the cursor position to the end of the current section.
If your machine does not have the FMT program, you have to find a similar software (I remember there is
Many such programs, but I do not know much about them)

2.11-how do I copy text?
This is a bit complicated. Take this section out and read it several times. Also, you must do more experiments.
/"YY can copy a line of text to a register (the register here is the term VI, which means it can store cut and
The place of the copied item), the letter must be a./"dd can put a line between A and Z.
Cut down and put it in the register. You can use a number before YY or dd to specify the number of rows to copy or cut. If
If the letter in is in uppercase, it means that the text is inserted before the existing text in the register.
/"P inserts the text into the cursor,/" P inserts the text before the cursor. If the register contains
The beginning or end of a line will be placed on another suitable line. Y is the shortcut of YY. And, y $, YH,
Ym and so on can all be used. d commands are also the same. If you want to quickly cut and paste, you do not need to specify a register
In this case, you cannot add text to the register. And as long as there is a DELETE command, the register
Nothing in (including the X command ).
For example, if you want to move the previous paragraph, you can move the cursor to the beginning of the paragraph, TAP/"a13dd, and move the cursor
If you want to place the text, knock/"AP", you can put the text behind the row where the cursor is located.
Now, you probably want to cut and paste the text to the area not at the end of the line. Let's see how to do this: We
You can use the M command to mark a region. The letter can be the same as the letter in the cut/paste register, because
They are stored in different locations in the memory. Then, TAP/"'[yy or DD]. The register here is
The register for saving text is used for marking, and YY and DD are your operations.

2.12-ah! I knocked on a DG just now, and my paper was gone! What should I do?
(Or, what should I do if I make a mistake just now ?)
U can undo the operation you just performed. u can undo your modifications to the current row (of course, a row is certainly true for a paper)
Not enough).: E! You can call your file again without saving the changes. Also, all the texts you delete are saved in
0 to 9 registers. Therefore,/"P can paste the nth delete text. You can press the following
Method to quickly view all deleted text. First try it. If not, press u and try again. (to accelerate this process,
In this case, the. Command in VI is different from the common usage. It does not repeat the previous operation, but tries the next register.
All you need to do is:/"1 p u. U. Until you undo the delete operation you want to undo)

2.13-I am writing my paper. I was told that I must put each section in a different file. What should I do?
: [M], [N] W can save the text from Row M to row n to the specified file.
This method can be used in almost all commands. If you use the command [m], [N] W>,
The text will be added to the end of the file.

2.14-ALL: How are commands handled?
: The Subsequent commands are all from the ex editor. This adds a lot of flexibility to VI and makes VI more powerful.
Large. For example, there are a lot of ways to find and replace, they have a lot of similarities (in fact, they are in some
In the sense)

3.0-how to find and replace it?
There are many methods. The simplest one is:
: S/old/new/g. However, this only applies to the current row.... Therefore, we use
: % S/old/new/g, more general, we can also use
: [Range] S/old/new/[CG]. Here, [range] is an arbitrary row range, including the row number, $ (end of the file),. (current row ),
% (Current file), or add a break number between two line numbers (or you can do this:., + 5, which indicates the following five lines). [CGI] Yes
C, G, I, one or nothing in the middle. c tells VI to prompt each replacement. G indicates that
Is case insensitive when searching. If there is nothing behind the last slash (/), then
Mo VI only replaces the first matched place in the row.
I prefer to do this:
: G/foobar/S/BAR/Baz/g. This command First searches for foobar and then changes it to foobaz. It has not changed.
Jailbars, and some other commands may change jailbars. This is my method, but it may be hard to remember.
Of course, you can also use the regular expression when searching, and some other commands when replacing text. If
If you use (and) in the regular expression to strip a sequence, you will find that you can do a lot of fun things.
For example:
: G/(FOO) (bar)/S/2/1baz/g replace foobar with foobaz
There are also some special sequences:
& All matching items
[1-9] items included in (and) between 1 and 9
The next character of u will be capitalized.
All characters after u are capitalized until E or E
L The next character is changed to lower case.
L subsequent characters are converted into uppercase letters until E or E is encountered.
[EE] change the end point of the selected region in Case sensitivity

3.1-how do I run a program in VI?
:! The command can run the program.: Sh in VI to start an interactive shell.
You can run VI again if you like. When you are editing makefiles and configuration files to compile
This may be useful for a program. This is superior to E because it does not need to save files, and if
When you exit the shell, VI will return to the original place. (Of course, I suggest you save the file first)

3.2-ah! I'm writing my paper, and the system crashes! What should I do?
Don't be afraid! You will receive an email containing your paper.
(The file name is the file you are editing when the system crashes.) You can recover your paper.
VI-R will list all files that may be recovered.

3.3-is there any tips to make vi more friendly to programmers?
: Set AI allows VI to be automatically aligned.
: Set Sw = #, # Is the shiftwidth (shiftwidth), or the tabwidth (tabwidth) of the tab key. You can
Use the> command to move a line left or right. You can also use % to move the {, (or [symbol)
The enclosed text is moved left or right. At this time, you must place the cursor over these symbols ({, (or.
: Set SM will show the matched {, [or (.
: Set lisp may be helpful for programming lisp. () is moved out of the S-expression. If
If the atom (atoms) is not stopped, {} will be removed.

3.4-Macro -- how do I write Macros?
: Map, which contains a maximum of 10 characters and a maximum of 100 characters. If
After you press it, VI will replace it. All macros start in command mode, but you can
Terminate in the desired mode. Remember to add ^ V before the control character you want.
: Unmap will delete this macro.: map! To insert to the document
.

3.5-how do I define a function key as a macro?
If it is # N, N is a number between 0-9, then this macro is mapped to a function key. (You
You can try the keys between F1-F10)

3.6-is there a way to abbreviated text?
Of course. This is vi. It can do anything!
: AB email ellidz@midway.uchicago.edu can be called after you have typed the email
.

3.7-how do I perform a spelling check in the current document?
Here is a macro that can do this. These should be placed in your. exrc file (we will
Further discussed). It is a fairly simple macro. It only calls ispell to process the current file. Of course, you
The ispell program must be included in the system. When using the program, you only need to press the V Key.
V is not used, so V is a suitable key)

Map V: w ^ m :! Ispell % ^ m: E! ^ M

The second ^ m enables you to go to VI without having to press enter after the spelling check.
In my system, there is no ispell, only spell. I used it to replace ispell, and it worked well,
^ M is not a two-character, but a character that is typed in by ^ V ^ m. In addition, I personally think that the second ^ m is not
Okay, because after directly returning to VI, you won't be able to see the spelling check results)

3.8-I have a hard copy terminal on hand. Can I still use Vi?
Okay, okay. I think someone will really ask this question. This question is too biased and we don't have to worry about it.
It. (And it answers a very common question)
When VI is started, it is in a special mode called/"open mode/". In this mode, things are more or less
There are some differences. The deleted characters are displayed as some/in front of you. VI will think that your terminal window has only one line. ^ r
You can output the current row again. Z, and then redraw the window around the current row.

3.9-Okay. Why is it in the open mode ?! However, I do not use a hard copy terminal, but it is still open
Mode?
The problem here is that VI does not recognize the type of the terminal you use, so it assumes that you have the worst terminal,
So it decided to remain in the open mode. This seems useless, because not many people like the open mode, but the Open Mode
Mode, you still need to know the minimum information of your terminal.
Now, let's look at how to solve this problem. We may divide it into several special situations. However, in general,
This is useless. If you wait until the type of your terminal, you can set the terminal type at the Unix prompt (in the CSH
Run the setenv term command. Under SH and its variants, run the term = command;
Export term)
It is recommended that you write the above command in the. profile file or. cshrc file so that you can automatically give
You have set up. (Of course, you should know how to use ed, or you can set the terminal type at the Unix prompt
Use VI so that you can edit these files)
If you do not know the type of your local terminal, try vt100. currently, most local terminals or terminal simulators can simulate
Vt100. if this does not take effect, you have to find a master to help you.
 

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.