VI use method

zhaozj2021-02-16  148

Learn VI - "List of Learning"

Original link: http://www.gentoo.org/doc/en/vi-guide.xml

Learn VI - "List of Learning"

1, starting point

Introduction

This guide wants to introduce you to the use of a strong visual editor -. Through a special step-by-step "cheat paper" model, this guide makes you quickly become VI experts in a short time. In this guide, you will learn how to browse, edit text, use the "insert" mode, copy paste, and use some important VI plugins (such as: visual plugin) and multi-window editing.

If you don't know, or feel uncomfortable to VI, so quickly use this guide, let this Linux / UNIX platform is most popular, the most powerful visual browser to add oil to your work!

About this guide

The biggest difficulty in learning VI is to - VI's a wide range of instructions. In order to effectively use VI, you need to remember some of them. This may take a lot of time; one of the purposes of this guide is to use as few times as possible. So, from the beginning, we will face a challenge - how can I help you remember a lot of orders in the short term.

In order to solve this problem in this guide, we will gradually accumulate a list of knowledge about VI. This list will contain all important VI commands. When you have completed this guide, when you forget a vi command, you can easily find a specific command from this list. Gradually, as you remember these commands, you will get less and less dependent on this list. By using the "Knowledge List" technology, you can learn to use VI unprecedented!

learning process

In this guide, I will use some ways to help you learn. First of all, as you are looking forward, I will introduce how a command works. Then, I will ask you to practice using this command in the VI, then record this command in the knowledge list, in order to preparation. Execute these steps one by one, there is a great help for your quick learning vi. Test and record these commands will help you remember them.

Introduction vim

VI has a lot of different versions, and I want to introduce you to a version called Vim. Vim is very popular; it has a considerable extension module to make VI better (when I show a VIM unique command, I will specifically pointed out). If you need to install Vim, you can get it from www.vim.org. In addition to a enhanced version of the command line VI, Vim also has a GVIM, beautiful, GTK GUI version. Below is a screenshot of GVIM in my system:

Illustration 1.1: VIM screenshot [img] http://www.ibiblio.org/web-gentoo/images/vishot.png [/ iv]

If you are a VIM newbie, try running GVIM on your system. Use from GUI to make novice feel relaxed.

2, the first step

Open a file

Before using the VI editing file, you need to know how to browse a file using VI. VI has a lot of mobile commands, and we will contact them. In this section of the guide, find a desirable text file, then open it with VI:

Code 2.1: Plug a file with VI [code] $ vi myfile.txt [/ code]

If you have a Vim, enter vim myfile.txt. If you decide to use GVIM, enter GVIM MyFile.txt. MyFile.txt is a file name in your system.

In vi

Once you start VI, you should see a part of the file is displayed on your display. Congratulations - you entered Vi! Unlike many other editors, you will enter a special "command pattern" when VI is started. This means if 1, VI on your keyboard does not enter one 1 in the current cursor position, and move the cursor to the right. In command mode, the characters entered from the keyboard will be passed to the VI as a command instead of inserting text into the file. The moving command is the most basic type in all commands, let us take a look. 3, move back and forth

Move in VI, the first part

In command mode, you can use H, J, K, and L keys to move the cursors to left, down, up, and right respectively. If you use a modern version of VI, you can also use the arrow keys to reach the same purpose. H, J, K, and L keys are useful: When you are used to them, you can move your hand from the keyboard base key position. Try to move the cursor in the file using the H, J, K and L keys (and the arrow keys). Try moving with H to the head. Note that VI does not allow you to automatically return from the lead in the previous row with the H key. Similar, you can't use L from the end of the line to the next line.

Move in VI, the second part

VI provides special shortcuts to jump to the leadership and tail. You can press 0 (zero) to jump to the lead, $ jump to the end of the line. Try it. Because VI has so many mobile commands, we can use it as a good "paging" program (like more or less command). Use VI as a paging program to help you quickly learn these move commands.

You can also use

F

B moves a whole next time. Some of the new versions (such as VIM) also support the same purpose with the PGUP and PGDOWN keys.

Move by word, the first part

VI also allows you to move around with the length of the word. To move to the first letter of the next word, press the W key. To the last letter of the next word, press the E key. Move the first letter to the previous word, press B. Try.

Press the word movement, the second part

After trying these moved commands, you may notice that VI uses words such as foo-bar-oni as five separate words! This is because VI defaults to space or punctuation as a word boundary, which is based on Foo-Bar-Oni to decompose five words: foo, -, bar, - and all.

Sometimes this is what you expect, sometimes not. Fortunately, VI can understand the concept of "long words". VI is only split by spaces or wraps as a long word. This means that although foo-bar-oni will be treated as a five VI words, it can also be treated as a long word.

Press the word movement, the third part

You can use your uppercase word move command to move between long characters. W Jump to the first letter of the next long word, E jumps to the last letter, b moves to the first letter of the previous word. Try it, compare with the movement of the ordinary word range until you understand their difference.

Master-wide mobile

There are several commands to see if we have learned what you have learned into the list. You can move with (and) to the beginning of the two sentences before and after. In addition, you can jump to the beginning of the current or next paragraph with {or}. Try them.

4. Exit We have said that the basic movement commands, but there are some commands you need to know. Enter: Q can exit the VI. If this cannot be exited, it may be because you have modified this file. To let VI exit without saving modifications, enter: Q !. This way you should exit it to the command prompt.

In VI, in: The beginning of the command is called the EX-mode command. This is because VI has built-in non-visual editor called EX. We can use EX to edit text in just use SED. Not only that, it can also be used to exit VI, just like we just see. When you press the Q key in the command mode, you can enter the EX mode. At this time, you will see a prompt:; if you press the back key, the entire screen will scroll up. To return to the familiar VI mode, just enter VI and then enter the car. 5, knowledge list

Beginning of the list

We have already mentioned a lot of orders; now I will add them to the list. You can make this list with a A4 or USS, because we will write a lot in it! This is a list of my list, which contains all the commands mentioned above. Refer to my format as much as possible so we can concentrate all things on a piece of paper.

Illustration 5.1: Knowledge list [img] http://www.ibiblio.org/web-gento/images/vicheat-first.png [/ iv]

Quick VI (Miscellaneous VI) let us continue our fast command line learning process. In command mode you can skip to a particular row with G. To jump to the first line of the file, enter 1G. Note that G is uppercase letters.

If you want to search for a certain string mode in the file, enter /

Then use it to enter. Use a regular expression to replace the mode you want to search. If you don't use regular expressions, don't worry - Direct input / foo will move to the next Foo. The only thing you need to pay attention is that if you need to find ^,., $ Or /, you need to add a backslash (/) before they. For example, / foo / .gif will look for the next appearance position of "foo.gif".

To continue looking down, press N. To continue looking up, enter N. Still, use your own VI editor to test these commands. You can also use // to repeat the last search.

6, save and edit

"Save" and "Save To ..." We mentioned that you can use the ex command: Q exit the VI. If you want to save you change, enter: w. If you want to save your changes to another file, type: w filename.txt will save the text as filename.txt. If you want to save, you will be exited, enter: x or: wq.

In Vim (and some other advanced editor, such as ELVIS): W, you can open several caches at the same time. To open a file in a new window, enter: SP filename.txt. FileName.txt will open in a split new window for editing. To switch between windows, enter

w

w (press two Control-W). : q,: q!,: w and: q Only one of the current active windows.

Simple editing

Now is when we learn some simple editing commands. The command we will be referred to as "simple" is because these commands only need you stay in command mode. More complex editing commands will allow you to automatically enter the insert mode - a mode that allows you to enter text from the keyboard. We will mention these content in a while.

Now, try moving some characters and then press the X button repeatedly. You will see x that will delete the character of the current cursor position. Now move to some part of a paragraph in your open file, then point J (uppercase). You will find that the J command allows VI to connect the next line at the end of this line. Now, move the cursor to a paragraph and then click R, then enter a new letter, you will see the original content is replaced. Finally, move to any of the files and then enter DD. You will find that DD will delete the current line of text.

Replace and delete

You can click. Repeat any editing command. If you try, you will see DD ... will delete four lines of text, and J ... will connect four lines (Translator Note: Do not understand ing, which heroes explain why not six lines ). As usual, VI provides very easy shortcuts. To delete text, you can use the D command to combine any move commands. For example, DW will delete content from the current cursor position to the beginning of the next word; d) delete the end of the next sentence; D} will delete the remaining content of this paragraph. Try to use D commands and other editing orders until you are familiar with them.

Revoke!

We are experimenting with deleting commands, and this is just a good time to learn to revoke changes. Press U. The traditional VI will only allow you to revoke the last operation. However, the new version of VI, such as Vim allows you to revoke your changes to files with a duplicate U command. Try the D and u commands.

Upgrade your knowledge list

When I update your knowledge list! Add those orders we have just said, your knowledge list should look like this:

Figure 6.1: Joining the knowledge list of editing commands [img] http://www.ibiblio.org/web-gentoo/images/vicheat-edit.png [/ iv]

7, insert mode

So far, we know how to move cursors in VI, read and write files, and some basic editing operations. However, I haven't shown you how to enter free format text! This is what I deliberately, because the insert mode of VI will appear complicated at the beginning. However, once you are familiar with the insert mode, its complexity (and usability) will become very useful.

In the VI's insert mode, you will be able to input text to the screen directly, just like many other visual editing editing. When you have completed the content you entered, you can use the ESC to return to the command mode. i or a allows you to enter the insert mode. If you press i, your text will be inserted before the current character; and a will let your text insert after the current character. Remember, after completing the input, use the ESC to back to the command mode.

Advantages of insert mode

Try A and I commands. Press any one, enter some text, then press the exit key to return to the command mode. Try to press the Enter key to see what will happen. Try to use the arrow keys and the DEL key to feel how to work. With the arrow keys and the DEL key, you can do a quite more editing work without exiting the command mode.

Insert option

Here are some other ways to enter the insertion mode. Press A (uppercase) to start inserting content from the end of the current line, regardless of your current location. Similar, i (uppercase) allows you to start in the current line. Press O to create an air line below the current row for insertion, and O will establish a new line above. Use the new blank line to replace the current whole line, enter CC. To replace the end from the current location to the end of the current line, enter C $. To replace the start of the row from the current location, enter C0.

In addition to the above mentioned features, all commands above allow you to enter the VI insert mode. After the input is complete, press the ESC button to return to the command mode.

Change the text

We tried CC, C0, and C $. CC is a special form of modifying the command, similar to DD. The C0 and C $ commands are examples of combining using modifying commands and motion commands. From this perspective, C and D are similar to the way, except: it lets you stay in the insert mode. This way, you can enter text used to replace the delete portion. Try to combine some move commands and C to try in your file (for example: CW, CE, C () .8, composite command

When you start using the compound ("Combo" command (similar to D {and CW), VI really shows its power. Not only these commands, we can also use a number and mobile command to combine, such as 3W, let VI skip three words to the right. 12B, 4J is a combination command formed by other numbers and mobile commands.

VI, in addition to support (numbers) (mobile command) combination, D and C are allowed to combine with a number or moving command. D3W will delete the following three words, D2J will delete the current and two lines below, and so on. Try these C and D's combination commands to see how VI can make your text editing simple and easy. When these commands become hands-on, you will be able to edit files with an incredible speed.

Update knowledge list

It has reached the time of updating knowledge list. It looks like this now:

Illustration 8.1: Join the knowledge list of the composite command [IMG] http://www.ibiblio.org/web-gento/images/vicheat-compound.png [/ iv]

Featured productivity

So far, we have learned to move cursors, save, and exit, make simple editing and deletion, and use insert mode. Using the contents of the knowledge list, you can use VI to complete all tasks.

However, Vi has many more powerful orders. In this chapter, you will learn how to cut, copy, and paste, search, and replace, and use "automatic indentation". These commands make VI more interesting and useful.

Visualization mode

The most convenient way to use clip is to use visualization patterns, an additional model of modern version VI (such as VIM and ELVIS). You can imagine the visualization pattern into a "highlight text" mode. When the text is inverse, we can copy or remove it, and then paste. If you are using GVIM, you can easily hold down the left mouse button to reach the effect of the anti-white text:

Illustration 8.2: Vim with the reverse white text [img] http://www.ibibiblio.org/web-gentoo/images/vihighlight.png [/ iv]

In addition, you can also press V (if you call VI through the console, this may be your unique choice). Then, use the mobile command (especially the arrow keys) to control the cursor, you can reverse a paragraph. After we do, we can copy or cut this text.

If you want to copy this text, press y (indicating "Yangk", Tug. To cut, point D. You will return to the command mode for this. Now, move to where you want to paste the content that just copy / cut, then press P to insert the content after the cursor, or P to insert the content before the content is in the cursor. Hey, the scrap copy is so easy! Try clipping the copy paste operation before entering the following section.

Replace text

We use the EX mode for text replacement. If you want to replace the first matching mode that appears in the current row, enter: S /

/

/ Press Enter. Here is the model you want to match, and

It is a string you used to replace. To replace all matches of the current row, type: S /

/

/ g is then carried on. To replace all matching locations in the file (this is usually you needed), enter:% s /

/

/ g. If you want to make a prompt before each replacement, enter:% s /

/

/ GC (c represents confirm confirmation) and then carries.

Regenerate

VI supports automatic rearrangement; this feature may be needed when you edit the program source code. Most modern version of VI (for example, VIM) will automatically enable automatic regeneration functions in your open source file (eg, .c file). You can use it after the automatic rearrangement starts

D (Control-D) moves a zone level to the left, or

T (Control-T) moves a regular level to the right. If the auto-shrink mode is not automatically enabled, you can start it with an EX Mode command: set autoindent. You can also use: set tabstop command tells VI your Tab width; set tabstop = 4 is a quite popular setting.

Our final version of knowledge list

Well, we completed our VI guide! After joining all advanced editing features, this list should look like this:

Illustration 8.3: Final version list [img] http://www.ibiblio.org/web-gento/images/vicheat-final.png [/ iv]

Put this list on your hand and start using the VI editing document, write email. When you need it, refer to this list; you will find it within this week, you will remember almost all of these orders, and Vi's productivity will let you fly the sky!

If you call VI as your default editor, modify your /etc/rc.conf

Code segment 8.1: Set the VI into the default editor (set Editor for your editor.) # Editor = "/ bin / nano" editor = "/ usr / bin / vim" #Editor = "/ usr / bin / emacs "

Reference

Here are information you might think to help continue to learn VI: · VI homepage, everything about VI · Vim Home is where you look for Vim related information · If you are looking for a good outdated book, "Learning The Vi Editor, 6th Edition" will be a good choice, covering information on many VI and VI varieties.

About this document

This original document is all all of the WestTech Information Services, which was originally published in IBM DeveloperWorks. This is a newer document contains many improvements to the Gentoo Linux documentation team.

Copyright 2001-2004 Gentoo Foundation, Inc. If you have any questions, suggestions and corrections, please contact www@gentoo.org.

Posted on August 04, 2004 1:00 AM

转载请注明原文地址:https://www.9cbs.com/read-8893.html

New Post(0)