VI Entry-clever form method [Image and text]

Source: Internet
Author: User

This free, DW exclusive tutorial explains how to use the powerful visual editor VI. It uses a fast "clever form" method, so that you do not have to spend a lot of time to master VI. You will soon learn how to move the cursor, edit the text, use the insert mode, copy and paste the text, and use important Vim extensions, such as visual mode and Multi-Window editing.

Prerequisites
No. Beginners of the VI editor will be proficient. Experienced users will be well reviewed and deepened.

System Requirements
You need Netscape 4.x or later, or IE 4.x or later with JavaScript.

For technical questions about this tutorial, contact the author Daniel Robbins, whose email address is a drobbins@gentoo.org.

Daniel Robbins lives in Albuquerque of New Mexico, which is Gentoo Technologies, Inc. president/CEO of Gentoo Linux, founder of Gentoo Linux. Gentoo Linux is the advanced Linux of PC and the next-generation ports system of portage and Linux. He is also a writer of MacMillan books Caldera OpenLinux unleashed, SuSE Linux unleashed, and Samba unleashed. Daniel has been involved with computers in some fashion since the second grade, when he was first exposed to the logo programming language as well as a potentially dangerous dose of Pac Man. this probably explains why he has since served as a lead graphic artist at Sony electronic publishing/psygnosis. daniel enjoys spending time with his wife, Mary, and his new baby daughter, hadas_3.

Learning vi-fast

It is a little difficult to learn about vi-VI has many commands. To use VI efficiently, you must remember some commands. This may take a long time, and one of the goals of this tutorial is to spend less time. Therefore, it is a challenge to remember many commands in a short time.

To deal with this challenge, as the tutorial goes deeper, we will gradually put these commands together in a "clever form. This table contains all important VI commands. After learning the tutorial, if you forget a command, you can refer to the clever form. After a while, when you remember these commands, you don't have to rely too much on clever forms.

Learning Process

This tutorial will use several technologies to help you learn. First, let's discuss how a command works as you expected. Then, you are required to try these commands (exercises) in VI, and finally, let you transcribe the commands into a clever form (for reference ). If you want to quickly learn VI, it is important to follow this step by step. Try to use a command in VI and remember it on your form for easy memory.

Introduction to VIM

There are multiple VI versions. Here we will introduce how to use a version called vim. Vim is very popular and has some better extensions (so I'll record it whenever I'm demonstrating a specific VI command), and if you need to install vim, you can download it from the http://www.vim.org. In addition to enhanced command line VI, VIM also comes with a good graphic editor gvim, which you can configure to use the GTK + GUI library. Below is a screen captured from my system:

If you are a new user of Vi, you should run gvim on the system. For beginners, using vi from the GUI is much easier.

Select a file

Before using VI to edit a file, you need to know how to use VI to move the file. VI has many mobile commands. Let's introduce them. As a part of this exercise, find an unimportant text file and load the file into VI in the following ways:

$ VI myfile.txt

If you have installed vim, enter "Vim myfile.txt ". If you are willing to use gvim, enter "gvim myfile.txt ". Myfile.txt is a file name on the system.

In vi

After the file is installed in VI, part of the text file is displayed on the screen. Congratulations -- you are already using Vi! Unlike many other editors, VI is in a special "command mode" after it is started ". This means that if you press "L", the cursor will move a character to the right, instead of inserting "L" at the current position of the file ". In command mode, the characters on the keyboard are used to send commands to VI, rather than inserting text characters. One of the most basic command types is mobile commands. Let's introduce them.

Move in VI

In command mode, you can use the H, J, K, and l keys to move the cursor to the left, down, up, and right. If you are using a popular version of VI today, you can also use the arrow keys to move. However, once you get used to the H, J, K, and l keys, it is very convenient, because in this way, your fingers do not have to be removed from the primary keyboard line. Try to move the cursor in a text file using H, J, K, and L (and arrow keys. Try to hold down the H key until the cursor comes to the beginning of the line. Note: When the cursor is already at the beginning of the line, VI cannot use the H key to fold to the previous line. Similarly, when the cursor is at the end of a row, the L key cannot be used to fold the cursor to the next row.

VI provides some special shortcuts for the cursor to jump to the beginning or end of the current row. You can press 0 (zero) to jump to the first character of a line, press $ to jump to the last character of a line, and try again. Since VI has many convenient mobile commands, it can be made into a large "pager" (like more or less commands ). Using VI as pager can also help you quickly learn all mobile commands.

You can also use ^ F (Control-F) and ^ B (Control-B) to flip the page forward or backward at one time. Currently, popular VI versions (such as Vim) allow you to use pgup and pgdown to flip pages.

Word Movement

VI also allows you to move words left or right. To move to the first character of the next word, press W. To move to the last character of the next word, press E. To move to the first character of the previous word, press B. Try it.

Move words

After moving words, you may realize that vi considers some words (such as foo-bar-oni) as five separate words! This is because by default, VI separates words by space or punctuation. Therefore, foo-bar-ONI is considered to be five words: "foo", "-", "bar", "-", and "oni ".

Sometimes this is needed, but sometimes it is not. Fortunately, Vi can also recognize "long words", that is, VI only separates long words by spaces or new lines. This means that although foo-bar-ONI is considered as 5 words by VI, it is considered as 1 long word.

To jump to the previous or last long word, you can use the move command between uppercase words. Use W to jump to the first character of the next long word, use e to jump to the last character of the next long word, and use B to jump to the first character of the previous long word. Exercise and compare the differences between the moving command for matching words and the moving command for matching long words.

Fast Moving

Let's learn a few more commands before we can concentrate these commands on the form. You can use the (and) character to move the cursor to the beginning of the previous or next sentence. In addition, you can use {or} to jump to the current or next segment. Test!

Exit

We have learned basic mobile commands, but you need to know several other commands. Enter Q to exit VI. If it does not work, you may have accidentally modified the file. To prevent VI from saving any changes, enter Q !. These operations should be performed in command prompt mode.

In VI, all commands starting with ":" are "ex-mode" commands. This is because VI has a non-visual editor called "ex ". Similarly, it can be used to perform row-based editing operations. In addition, you can also exit VI, as described above. If you press the Q key in command mode, it is converted to the ex mode. If you are in ex mode, you will see a prompt. Press enter to scroll up the whole screen. To return to the command mode of VI, Type VI and press Enter.

Start form

I have learned many commands. It is time to transcribe them into a clever form. To make a good form, you need an A4 letter (we will put a lot of information on it !). Below is my clever form, where I have copied all the commands I have learned so far. If possible, follow my layout to fill in all the content on a piece of paper.

Various VI commands -- move the cursor

Let's continue to learn commands quickly. In command mode, enter <line number> G to jump to a specific line. To jump to the first line of the file, enter 1 GB. Note that G is in uppercase.

If you want to jump to the next location of a specific text style, enter/Regexp and press Enter. Regexp is the regular expression to be searched. If you do not know how to use a regular expression, do not worry. Input/Foo will come to the next Foo. Note that when character ^,., $, or/is involved, you can add a backslash (/) before these characters to perform normal search. For example,/Foo/. gif will match foo.gif ".

To repeat the search, press n. To search backward, press n. Similarly, test these commands in your VI editor.

Save and save...

We have introduced how to use the ex command: Q to exit from VI. If you want to save the changes, enter W. If you want to save the changes as another file, enter W filename.txt to save them as filename.txt. If you want to save and exit, enter or: WQ.

In Vim (and other advanced VI editors, such as Elvis): W, multiple buffers can be opened at a time. To open a file in a new window, Enter SP filename.txt. Filename.txt will appear in a separate window. To switch between windows, enter ^ w ^ W (Control-W twice ). If you enter: Q,: Q! ,: W and command, only valid for the current active window.

Simple editing

Now, you can learn some simple editing commands. These commands are simple because they are still used in command mode. More complex editing commands automatically switch to the insert mode, which allows you to input text data from the keyboard. We will learn such commands later.

Now, try to move the cursor and repeat the X key. You will see the X key to delete the character of the current cursor. Move the cursor to the middle of a segment of the text file and press the J key (uppercase). You will see the J command connecting the next line to the end of the current line. Now move the cursor to a character and press R, and enter a new character. You will see that the original character is replaced. Finally, move the cursor to any row of the file and type dd. You will see that DD is deleted before moving forward.

Duplicate and delete

You can repeat any edit command by typing the. Key. After testing, you will find that 4 rows are deleted and 4 rows are connected. Generally, VI provides another convenient method.

To delete text, you can also use the D command and the move command together. For example, DW is used to delete all characters from the current position to the beginning of the next word, d) is used to delete to the beginning of the next sentence, and d} is used to delete the remaining parts of this paragraph. Test D commands and other editing commands until you are proficient.

Recovery!

You have already learned how to cancel the deletion. Press the U key. Previous versions of VI only allow you to cancel the previous change. However, the new version of VI, like vim, allows you to repeatedly press the U key to undo the changes to the file step by step. Use the D command and the U command to continue the next screen.

Update clever form

This updated form! By adding all the commands you have learned, your form looks like:

Insert mode

So far, we have learned how to move the cursor in VI, execute file input and output, and perform basic editing operations. However, we have not introduced how to enter data in a free text format. This was intentionally arranged, because it would be too complicated to talk about the VI insertion mode at the beginning. However, after you have mastered the insert mode, its complexity (and flexibility) becomes your capital.

In insert mode, you can input text directly to the screen, just as in many other visual editors. After entering or modifying, press the Escape key to return to the command mode. To enter the insert mode, press I or. If you press I, the entered text will be inserted before the character at the cursor, And if you press a, the text will be appended after the character at the cursor. Remember, after entering the text, press the Escape key to return to the command mode.

Benefits of the insert mode

However, we continue to learn how to use the and I commands. Press a or I, enter some text, and then press escape to return to the command mode. After you press a or I, click the Enter key to see what is going on. Try to use the direction key and delete key to feel how the insert mode works. If you use the direction keys and Delete keys, you can perform more powerful editing steps without changing the insert mode.

Insert Option

There are some other convenient ways to enter the insert mode. Press a (uppercase) to append the input text to the end of the current row, no matter where the cursor is in the current row. Similarly, I (uppercase) inserts the input text into the beginning of the current row. By o, an empty row can be generated under the current row and text can be inserted. By O (uppercase), an empty row can be generated on the current row. To replace the current whole row with a new row, press CC. To replace the current position of the cursor with the end of the row, enter C $. To replace the current position of the cursor with the beginning of the line, enter C0.

In addition to special operations, these commands also enable VI to be In the insert mode. After entering the text, follow the escape return command mode.

Change text

When we type CC, C0, and C $, we are starting to use the C (Change) command. CC is a special form of change command, similar to DD. The C0 and C $ commands are examples of how to change the command and move the command. In this form, C is similar to D, but it puts VI in insert mode so that you can enter the text to be replaced in the delete area. Try to use some mobile commands with C, and experiment in your file (prompts: CW, Ce, C (,.).

Compound command

When you start using composite commands (such as d {and CW), VI has become a powerful editor. In addition to these commands, you can also use the move command with a number, for example, 3 W, which enables the cursor to jump three words to the right. There are more examples of composite mobile commands: 12B and 4j.

In addition to numbers (mobile commands), VI also allows D and C to be used together with numbers or mobile commands. Depending on this, d3w will delete the last three words of the cursor, and d2j will delete the current row and the next two lines. Test examples of using C and D with some mobile commands, and then feel the powerful and concise VI editing function. Once you are familiar with these commands, You can edit files at an amazing speed.

Update clever form

It's time to update the form. So far, it should be similar:

Well, it's really good...

So far, we have learned how to move, save, exit, edit and delete, and use the insert mode. According to the clever form, you should be able to use VI for almost all tasks.

However, VI has many more powerful commands. This chapter describes how to cut, copy and paste, search and replace, and auto indent. These commands help make vi more interesting and effective.

Visual Mode

The best way to cut and paste is to use the visual mode, which is a special mode. A new version of VI, such as vim and Elvis, has been added. You can consider the visualization mode as "highlighted text. Once the text is highlighted, you can copy or delete it and paste it. If you use gvim, you can press and hold the left mouse button to drag the selected area to highlight it:

Visual mode, continue

In addition, you can press V to enter the visualization mode (if you are using vi, this may be the only option ). Then, use the move command to move the cursor (usually the direction key) to select a piece of text. After the selection, you can cut or copy the text.

If you want to copy the text, Press Y (representing "Yank"). If you want to cut the text, press d, and VI will return to the command mode. Move the cursor to the cut or copy text to insert, press P to insert the cursor, and press P to insert the cursor. Okay. Cut, copy, and paste the file! Test several more copy/cut and paste operations, and then proceed to the next screen.

Replace text

To replace the text style, use the ex mode. To replace the first style of the current row, enter S/Regexp/replacement/and press the Enter key. Regexp is the style to be matched, and replacement is the replacement string. To replace all the matches in the current row, enter S/Regexp/replacement/g and press Enter. To replace all matching samples in the file (which is usually required), enter % S/Regexp/replacement/g. If you want VI to provide a confirmation prompt for replacement, enter % S/Regexp/replacement/GC (representing "Confirm") and press Enter.

Indent

When editing the source code, VI supports automatic indentation. When editing source files (such as. c files), most new versions of VI (such as Vim) will automatically enable the automatic indent mode. If the automatic indent function is enabled, ^ d (Control-d) can be used to indent the left first level, and ^ t (Control-T) can be used to indent the right first level. If auto indent is not automatically enabled, You can manually enable it by entering: Set autoindent in ex command mode. You can also set the tab size according to your preferences. The command is set tabstop. Currently, set tabstop = 4 is very popular.

The last clever form

Well, we have finished learning Vi! Add all the advanced editing commands to the clever form. Now, it looks as follows:

Roll up a clever form

Roll up the form and start using VI to edit the file and write emails. If necessary, refer to the clever form. You will find that you will remember all the commands within a week, and then you will know how powerful the VI functions are!

References

Below are some references that may help you further study VI:

The VI lovers Home Page

Is a very good resource, including all VI content.

The Vim Home Page

It is a good website for vim.
If you are looking for excellent old books,

Learning the VI Editor, 6th edition

(O 'Reilly; 1998) is a good choice. This book comprehensively describes the derived versions of VI and VI.

Your feedback

Please tell us: Does this tutorial benefit you a lot? How can we do better? At the same time, we hope you can propose some other theme topics. Thank you!

If you have any questions about the content of this tutorial, please contact our Linux editor,

Developer@cn.ibm.com

.

 

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.