Why are these dead brains using vi?

Source: Internet
Author: User

Source: www.ijser.cn /? P = 1026 Author: ijser

Whether you believe it or not, there are many people who are infatuated with the VI editor that has been around for more than 30 years (the best clone and improved version Vim is now 15 years old ).

They are not old school and cannot keep up with the trend of the times-VI usersCommunityI have been using VI for 2 years (after 10 years of programming experience), and many of my friends have started to use VI. Interestingly, A large number of VI users were not born before the appearance of VI.

Of course, the VI/Vim editing mode is better than any other editing mode. You don't have to be a Unix expert to use it. On the contrary, VIM can be used on any platform for free, it can also be used as a plug-in for other mainstream ides. Let's clarify some misunderstandings and use some real examples to illustrate why it is an excellent editor.

Misunderstanding #1: modal editing

The first time you get stuck with VI/vim, you may feel shocked and disgusted when you start editing with "I". You cannot remember to use "hjkl" to move the cursor, in addition, you need to press "a" to append the content after the cursor. because you may be used to other edits and the direction keys work in the insert mode (in 90% cases, if the system configuration is correct, it is stored in the insert mode and will not be returned to the normal mode. you may spend 20 minutes in the insert mode and complain: "How can I remember whether it is in the insert mode or normal mode?"

Obviously, this is a completely wrong way to use VI/Vim. The correct method is that you don't care about the mode. You will always be in normal mode, the insert mode is enabled only when you need to insert or modify characters. After editing, press <ESC> to return to normal mode. therefore, how to remember that the current mode does not exist. when editing text, do not answer the call in the insert mode. Instead, exit the insert mode, answer the call, or press <ESC> to enter the normal mode. do not consider the insert mode as a status.

Let me explain this philosophy.

Commands in VI/Vim can be used in combination. For example, "D" indicates deletion, and "E" indicates "moving to the end of a word ", the "de" function is to delete the characters from the current cursor position to the end of the word (a bit like Ctrl-shift-right, left, del shortcut keys in other editors)

,". "The command can repeat the combined commands completed last time (excluding the Mobile Command). After" DW "is executed ,". "repeatedly execute the" DW "command. You can move the cursor and then press ". "will quickly delete the next word at the current cursor, which is incredibly powerful.

Now let's look at the insert mode. some commands (such as I, A, S) allow you to enter the insert mode and type the text until you press <ESC>. Speaking of these commands, the entire command also contains all the characters you typed between the "I" command and <ESC>.

For example, "ihello <ESC>" will insert the "hello" character at the current cursor, but now ". "The command can insert" hello "repeatedly at the current cursor. Now you should be able to understand the power, but there are more powerful ones, "A" moves the cursor to the end of the current row and enters the editing mode. Therefore, when you press <ESC> to end the input, you can press ". "insert the same character at the end of the line at any position.

Another more powerful example is the "CE" command, which is composed of "C" and "E" (modified) the command will delete the text within the specified range and enter the insert mode. It is the same as the "D" command, but the only difference is that it will enter the insert mode instead of staying in the normal mode. the advantage is that the text you typed after this command will also be part of this command. therefore, if you enter "cehello <ESC>", the content from the current cursor position to the end of the line is "hello", and ". the command will also repeat this operation.

Actions (move the cursor) can also be more complex. There are many commands that enter the insert mode ("O" inserts a new row under the current row, "O" inserts a new row on it, "S" deletes the content at the end of the row, and so on... All these commands enter the insert mode), so you can imagine how powerful the edit command can be created and executed repeatedly using.

Example #1: Perfect point command

Let's take an example. for example, you have already declared three new functions in your header file, and then you need to implement them in the module. copy the following text to your implementation file:

Almost all programming started like this, isn't it?

Now you must delete these semicolons and add an empty function body. At the cursor point, you can enter "a" to move the cursor directly to the end of the row and enter the editing mode:

After you press "A", pay attention to the position of the cursor after insertion

Now you can use the <backspace> key to delete the semicolon:

Deleting is simple.

Then type <return >{< return >}< return> to add the function body:

Insert is the same as other edits.

Finally, press <ESC> to return to normal mode:

Now we return to the normal mode.

Now you have to repeat the preceding steps to modify the other two functions. How can this problem be solved? Very simple. First move the cursor down by "J" and then click ". "Copy the last command (" A ", delete the semicolon and insert the text). If you press" J. j. ", twice, you will get the following results:

After the J. J. Command

The command architecture of VI is critical. Both <backspace> and normal input are part of the command, and the command can be executed repeatedly. In retrospect, how many operations are repeated in daily editing?

Misunderstanding #2: This is not just a regular expression

VI/Vim supports regular expressions very well. Many editors support regular expressions for searching, replacement, and other operations, but only VI (as far as I know) can use regular expressions in a more advanced manner, for example, execute a regular query and replace the next line of the second row that appears with "begin", including "proc", or any complicated operation that you can think.

This is not to describe the regular expression, or the regular expression support of VI/Vim. The main strength of VI is that, when you are used to using it, the powerful dependency is its basic editing mode:

    • One or two keys can be moved to a specified position in one row or one screen.
    • Commands such as "D" or "C" can be combined with other operations to directly edit some text, or enter the insert mode, and you can use "." To repeat any number of times
    • You can do anything without leaving the primary key area! Now you don't have to worry about using the laptop keyboard.
Example #2: Smart range tag let's take a look at the following typical example. This is a function call embedded in a complex expression:

Function calls in complex expressions

As you can see, the cursor is the start position of the function call. Now imagine that we want to expand it and save it in an internal variable. The first thing we need to do is to select this call function, copy and then move it to the above, enter the variable name, and then move it to the above input function declaration. In editing a model, you need to use ctrl-right and the left and right keys to constantly search for positioning. This is not required in vim. "%" Will be automatically moved to the matching parentheses (or similar grouping characters), but if you do not locate these special characters, it will scan the right one character by one character, until the first character is found, and then the matching character is found. Therefore, in this case, it moves to the matching parentheses on the right.

We know that the "c" command can delete characters (it will also be copied to the cut version. Note: The other is the buffer in VI, not the system cut version, the same below) and enter the insert mode, we can enter "C %" in the above case, and then we will get the following result:

Enter 'C % '("Change match ")

Function callCodeIt is put in the cut version, and now we can enter the variable name. It is not too difficult to enter two characters. after entering the variable name, press <ESC> to return to the normal mode, as shown below:

Enter the character and press <ESC>


Now in normal mode, you can type "O" to create a new row and enter the insert mode, and then start to type the declaration statement:

After entering "O", enter the first half of the declaration statement of the function.

Now, we need to insert the previous function expression. Because it is already in the cut version, we can use ctrl-R, "insert and paste in insert mode (I admit it is a bit difficult to remember, but it supports the compound cut version function ). this inserts the previously declared function call statement into the current position. "enter a semicolon and press <ESC> to return to the insert mode:


Misunderstanding #3: only a nerd or a genius can use it

Well, I hope I have read the above explanations and examples. You have learned the power of VI/vim. Learning it is difficult (as shown below), but if you will repeat the 8-hour or above day of the year, this will be the second best learning investment after you learn typing (you understand, do you? If no, do not blame VI. Learn it first ). The hard work of a few weeks will benefit you throughout your life, and at least no "dumb assistant" will bother you to death ?)

The key is that when you use VI, your keyboard becomes a dedicated super text input handle with hundreds of buttons. Each key on the keyboard has at least two functions, namely the top and bottom, therefore, you can implement 200 functions at least each time you press the key (excluding the Shift key. Text editing commands are very powerful, and you can even combine them to get the best results. When you enter some characters, it is a normal keyboard, but when you return to normal mode, it is the best machine for text input design.

Example #3: Restricted block control

In another simple example, this is only supported by VIM (a special action of VIM is used). Assume that your cursor is between angle brackets, as you often encounter in XML:

Life at an XML tag...

How to choose the content in the angle brackets? In other la S, you need to move your hand from the comfortable position to the arrow key area of the keyboard, or even worse, it may be very painful to use the mouse, especially when using a laptop. But what do you do in Vim? You only need Text object action commands starting with "I" or "A". "I>" indicates "block in the current angle bracket ", therefore, you can use "Di>" ("Delete content in angle brackets") to delete all content in angle brackets in the preceding situation:

After typing di> ("delete inner angle-block ")


You can use "(" or ")" to represent the content block (or "B"), "[" or "]", "{" or "}", "W" indicates words separated by syntax, or "W" indicates words separated by spaces, or prefix "I" or "A" before them to implement the insert or append function.

Misunderstanding #4: Use hjkl to move the cursor?

Many people are surprised to use hjkl to move the cursor instead of the direction key, probably because there was no direction key in the era of VI, at that time, the keyboard of the terminal computer was particularly printed with a direction prompt for the hjkl key. However, the side effect of this design is that when you move the cursor, you do not have to move your hands away from the primary key area. This is good.

Adm3a: VI Author Bill Joy initially used it to compile the hjkl key of viadm3a

Note: When Bill Joy wrote VI, he used this computer. Due to a long time ago, this computer had only one primary key zone (), and its hjkl key is also printed with a direction)

However, even if you first use hjkl, once you fully master VI/vim, you may not use "h" and "L" (left and right) at all ), "J" and "K" are rarely used ". Why? Because there are other more powerful movement commands that allow you to move the cursor faster to the position you want to reach. When moving in a row, I find that there will always be a moving command that can direct the cursor to the place I want, so I will have the moving commands: "F" and then any other character will locate the next occurrence of the character, "%" is used to match the pattern of any place you want to go, and so on... when you export a file, a command can be used to directly locate the position at the top, center, and bottom of the screen. "/" can easily enter the string you want to search, "]" can be navigated between functions, and so on.

Hjkl key of adm3a

Example #4: very useful commands

Some commands are very useful. Once you learn how to use them, you can easily place the cursor directly at the top, center, and bottom of the screen. "H", "M", and "L ." ZT "," ZZ ", and" ZB "will keep the cursor unchanged at the current position, and the scroll view will move it to the top, middle, or bottom of the screen. "×" Searches for the next appearance position of the word where the current cursor is located ("#" searches backward, so it is easy to remember the symmetric position on the keyboard ), there are more such commands...

Misunderstanding #5: Because it takes 90% of the time to think, 10% of the time to edit may have production efficiency, but it is still useless.

These are exaggerated statements, but they are often raised against the benefits that are very important for improving the development and production efficiency in the editing process. I bet this is wrong.

First of all, sometimes I have to think about a problem, instead of going to the code. I rode my bike for an hour or two. if the weather is good, this is much better than thinking in front of the computer.

Or, when I have to analyze some difficult problems or design a set of solutions, I often come up with a notebook (a notebook made of paper), a pen, then use them to sort out my ideas.

We can bet that production work has to be done in front of the computer unless in special circumstances. This is because, most of the time, you have to look at the code to think about or design, and this includes browsing the code in the editor, and you are usually typing or editing. You may have just thought about it for a minute, and then spent another minute editing and implementing what you just thought of. When you edit it, you want the best tool to work.

Comfortable editing keeps you in the field of thinking. This concentrated State gives you maximum productivity, because you have mastered a powerful tool like a text editor, and it actually disappears from your consciousness, you can freely concentrate on solving problems, you will subconsciously edit and implement your ideas. The general editing method is to find and locate the location, and use ctrl-right, Ctrl-Right to locate your location, move your hand over the mouse, open the menu, select an option, enter a series of dialog boxes, and click "OK" to confirm the application. In VI/vim, all you have to do is simply press a few keys, which is simple and clear.

Other VI users also shared this with me, so I know that not only do I have this feeling: Once you have mastered Vi, you will often feel this way. After 30 seconds of editing, there is a kind of memory that keeps hitting the keyboard sound in your mind. When you travel, move, and edit the lines and blocks of the text, it feels like this sound is playing in your mind, at this time, you will have a strong feeling.

Example #5: indent a block

VI and VIM can understand your code structure, which can be reflected in many commands. As mentioned in example #3 above, "AB": select the current "{" and "}" and the content it contains ("). next Let's combine it with the ">" operator. "<" is a useful command used to indent a region. The Code is as follows:

Inappropriate indentation

How long will this happen to you? Yes, you can paste auto-indent again (only "] P" is required in VIM), but normally you will forget, or you are not caused by pasting this code, it is caused by your addition or deletion. You only need to indent it. In other edits, move the cursor, select, and press the tab key. In vim, you do not need to be so annoying. You only need to enter three keys: "> AB" ("indent a block "):

We didn't even move the cursor

Cool, right? You didn't move the cursor or choose to move the cursor. You just told Vi how to do it, and then VI did it. I believe that this "direct" editing method makes you feel the power and keeps you in your state.

Misunderstanding #6: it is just the past that is about to disappear

VI has been around for more than 30 years, but it does exist now. Vim, a fully cloned VI with more features than vi, has been in existence for 15 years and can run on almost any platform in the world. People who like Vi have found a way to use it anywhere: There are VI simulation plug-ins in eclipse, and there are VI simulation plug-ins in many Mac OS X apps, there are VI simulation plug-ins in intellij idea. None of these even have Emacs, but there is an Emacs VI simulation plug-in several and Viper. I developed and sold viemu, A home plug-in package allows VI to run in Visual Studio, SQL Server, word, and outlook. Paul Graham still uses it to write list and arc, tim o'reilly also publicly admitted that it is a VI user, and slickedit and crisp also have a VI simulator...

Of course, there are not many communities in VI/VIM: many computer users are not even proficient in typing, and the VI learning curve is steep. Those who have seen a lightweight and simple editing system, therefore, the VI/Vim editor will certainly continue to exist for many years. In fact, using the key binding function of VI/Vim makes you more accustomed to using vi and will be available in any environment in the future, from the old UNIX operating system to the latest popular IDE.

Example #6: visible

As the last example, even though it seems that VI is about some mysterious and incomprehensible commands, we will now see some more visual aspects of it (in fact, these are Vim features, rather than the original VI ). one of them: when the "hlsearch" configuration is enabled (it is disabled by default in Vim, but can be opened using ": Set hlsearch"), when you search for a character, all matching results are highlighted on the screen. Suppose you have the following HTML code:

HTML code

If you press "*", you will find the word ("Div") under the current cursor, and the result will be like this:

Powerful asterisks

As you can see, the cursor moves and the results are highlighted in different places.

More, we know that operators like "D" and "C" will be executed based on the next command. Okay, if we want visual feedback, we can use the visual mode: press "v" to move the cursor. You will see that the cursor is highlighted from the initial position to the position, and then press the operator to see the effect. The search command also applies here. If you replace "v" with "V", it is highlighted in the unit of action. Now we press "V" and then "K" (on ):

After the VK is pressed

As you can see, these two lines are highlighted. If we want to select a closed tag (such as highlighted) until Div, input "N" (the last matching result ):

After "N ",

Now we can do anything we want. If the "Gu" Operation changes all characters to uppercase (then return to normal mode ):

Gorgeous capital

Well, now let's make a correct explanation for the understanding of VI/VIM:

Correct understanding #1: steep learning curve

One thing that everyone agrees with is correct. This figure accurately illustrates this point:

The fact is that it takes a few weeks to learn VI/vim, and the first attempt is unpleasant. I think of this as the main reason why VI/Vim is not and will not be popular editors. Compared with other editors, you need to make a lot of effort to learn and remember, and then use 30 or more commands to make vi/Vim more efficient. Because these commands are rough one-click commands (even though they all have some Mnemonic Methods, and even some forms are used in the same way), this is still not a simple task, it is easy to admit defeat and return the familiar jedit, Pico, ultraedit, textmate, or even Emacs. But once you study hard, I know no one will want to return to those editors again, and I know that many people say they have been using VI for more than 10 years, and they are used to it and are looking forward to improving even the smallest details.


If you want to use it or not, if you think that learning to use VI/Vim is in vain, you should study Emacs or continue to use your poor IDE. In any case, don't say "people who use VI are dead brains". I hope I have successfully demonstrated to you why they (we) stick to VI/vim, and at least you should be able to understand its power, even if you still prefer not to use it.

If you want to study more functions of the VI/Vim Editor, here are some useful materials:

    • learn why I got started
    • use my graphical cheat sheet and tutorial to learn VI/Vim editing
    • the awesome "VI lovers" home page
    • learn Jonathan McPherson's hints for valid tive editing with vim
    • go to Vim's homepage for anything you want

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.