Bram moolenaar
If you need to spend a lot of time typing text, writing programs or writing HTML scripts, You can effectively use a good editor to save time for you. this article will guide you to quickly complete your editing and reduce your errors.
This article uses the Open Source Software Vim (VI improved) as an example to show you how to effectively edit, but the principles mentioned here are the same for other editors, choosing an appropriate editor is the first step for efficient editing. There are countless arguments about which editor is the best. I don't want to talk about it here. if you still don't know what editor to use or think the editing you are using is unsatisfactory, try Vim to ensure your satisfaction.
Part 1: editing a file 1. Fast Moving
Most of the time for text editing is spent browsing, checking for errors or finding the correct location for editing, entering new content or changing existing content is second to none. it is very common to roam freely in the text. therefore, the first principle of efficient editing is to learn how to quickly move and locate in the text.
Generally, you know the content to be searched, or view all text lines to find a word or phrase. you can use the search command "/pattern" to search for text, but note the following:
If youYou have found a word and want to find out where it appears.,Available"*"Command,It finds the next matched target.
If you have set the 'incsearch' option, VIM will display the first matched item in the whitelist. this can be quickly displayed when you enter keywords under the/command (similar to the incremental search function of Emacs)
If you set the 'hlsearch' option, VIM will highlight all the matching results. This policy gives you a general understanding of the content to be searched, if you use this function in program code, it will show all the places where a variable is referenced. you do not need to move the cursor to see all matching matches (on the same screen, you can see that there is no matching place ).
There are other more convenient tips for Fast movement in some structure specification text. Vim is embedded with C Programs (and C ++ and Java, which are very similar to the C language) command,Use"%"Command to jump from an opened bracket to another matching Bracket,You can also use a preprocessing command"# If"Jump to the right"# Endif"In fact, the "%" command can jump to the 'other half 'of several text elements. This is very convenient to check whether your () and {} are correct.
Use"[{"Jump to the beginning of the current code block(The code block is a program segment enclosed ).
EnableUse"GD"Jump to the word with the current cursor(Variable)Local definition. Of course, there are many other techniques. the key is that you have such a command. you may say that you cannot learn all the commands. -- There are hundreds of different mobile commands, which are simple and intelligent. However, it may take weeks to learn and use them. of course, you don't have to master it all, as long as you have a set of methods and can handle the operations you want.
There are three steps for you to learn the skills you need.
When editing a file, pay attention to what you often perform repeated operations, or what you spend most of your time doing.
Think about whether there is an editing command that can do the most headache for you. read online documents, ask a friend, or see how others do it.
Practice using these commands until your fingers can be used freely without thinking.
Here is an example to illustrate how to do this:
When you write a C program, you often need to spend time finding a function definition. Now you are using"*"Command to find out where this function name has appearedBut before you reach the real target, there may be many matches that match your search criteria (for example, those that appear in comments or the function is called elsewhere) that harass you. you may think there is a shortcut that can be achieved in one step.
After reading the reference manual, you will find a reference to tag. This document shows how to use this function to jump to the function definition. This is exactly what you want!
You already know how to generate a tags file (ctags *. [CH] Or etags *. [CH]). Use the ctags program to generate the tags file required by VIM. next, use the Ctrl-] command. to make it easier to use this function, you can also add the command to your makefile to automatically generate the tags file.
When using these three principles, be careful: "I want to use these commands, but I don't have time to read some new commands in the document ". if you still think so, you may still be in the computer Stone Age (that is to say, you are a good cook ). some people use NotePad to do things. They may think it is incredible that others do the same job in a shorter time.
Do not repeat the same thing. if you often find a common command, you don't have time to focus on what you want. you only need to find the operations that consume too much time, and practice using the corresponding shortcut commands until you can use them without thinking. in this way, you may focus on the text you want to edit.
The following are some suggestions for solutions to common problems that most people will encounter. You can use the above three principles as an example.
2. Do not type the same thing twice
The text we typed is a finite set, and even uses finite phrases and sentences, especially computer programs. Obviously, you don't have to type the same stuff twice.
The most common thing is to change one word to another.Replace the word that appears everywhere in the entire file with another one.,You can consider using": S"CommandIf you want to make changes selectively, and you 'd better decide after you read the context,You can use"*"Command to find another matching word,If you decide to change,Use"CW"Use to change these words,And then use"N"Command to the next match"."Repeat the previous command."."Command repeat the previous change. A change refers to the insertion, deletion, or replacement of some text. repeat these operations is a powerful mechanism. if you use it to organize your editing operations, many previous modifications that must be made manually only need to be simply used ". "command. pay special attention to whether you have done other things before repeat the last modification operation. Some operations in the middle may change ". "Actually repeated content of the command.Use"M"It is useful to mark a location of a text.It allows you to go back to your last stay after making repeated changes.
Some function names and variable names are hard to be correctly typed, such as "xpmcreatepixmapfromdata". It is difficult (at least annoying) to see or not view the help of a sample ). vim has a complete mechanism to make such a task a piece of cake. it will search for the text you want to type in the file, find a similar match, and insert it directly.Still in yourIncludeRecursive search in files.You can type"Xpmcr ",Then pressCTRL-NKey, VIMWill expand it"Xpmcreatepixmapfromdata ",This function not only saves time for you, but also reduces the chance of errors during manual typing. In addition, your compiler will not generate such warning errors.
If you want to repeat a phrase or sentence,There is also a quick way. VimThere is a mechanism to record macros.You type"QA"Start to input a macro into the register variable.'A'Medium.Press down and you can enter the operations you want as usual, but these operations will be recorded by VIM into the macro named 'A', and then press the "Q" key again, the recording of macro 'A' is ended. when you want to repeat the operations you just recordedOnly use"@"Command.Total26Available registers for you to record macros.
You can use macros to repeat multiple different operations, not just insert text. If you want to perform some repetitive operations, remember to use this trick.
When using macros, note that macros only mechanically repeat the actions you just typed. Be careful when moving them in the file. when you repeat using macros, the text objects you want to operate on at the beginning may be different. A four-character shift to the right during macro recording may work properly for the current environment. but when you play back these macros, the text environment that it operates may need to move 5 characters.
When you want to record a complex task, it is not easy to pass it all at once. At this time, you can write a macro or script. this is very useful for templated your program. for example, you can customize a function header as intelligent as you wish.
3. Error fixing
An error occurs during typing. There is only one way to correct it as soon as possible. the editor can help you do this automatically. but you have to tell it in advance how to calculate the error. What is the correct one.
For ordinary people, common mistakes are the same. Your fingers are not listening. This can be corrected through the abbreviation function. Some examples are:
: Abbr lnuix Linux
: Abbr accross authentication SS
: Abbr hte the incorrect word editor with one click will replace it with the correct word.
The same mechanism can also be used to replace a long word with a few characters. In particular, some words that are hard to spell out. This also avoids the chance of making mistakes. For example:
: Abbr PN pinguin
: Abbr MS manrake software however, the side effect is that the editor is always trying to extend the abbreviation it knows to the entire word, if you really want to type MS, it is a problem. therefore, try to use non-Ambiguous abbreviations.
Vim has an excellent syntax highlighting mechanism to find errors in your text. programmers, especially the biggest beneficiaries of this function.
Syntax highlighting uses special colors to display comments. It sounds like nothing, but once you use this feature, you will find a lot of benefits. you can quickly find out which parts should be a comment. but it is not highlighted by the syntax. for programmers, it is normal to forget the end mark of the comment. this is not an easy task in text that only contains both black and white colors.
Parentheses without correct matching can also be highlighted by syntax. an incorrect pair of brackets ")" will be highlighted by a bright red background. you can use the "%" command to see who it should match, and then add a "(" or ")" in the correct position ")"
Other common errors can be checked by the syntax highlighting function, such as # I ncluded <stdio. h>. in the black and white worlds, they are right and wrong. however, syntax highlighting helps you quickly identify male and false.
A more complex example: for English text, there can be a long list of available words, and words not included in them are considered as an error,Use a syntax File,You can mark all words not in the file list with the syntax highlighting function..With a special macro, you can add new words to the word list. They are no longer considered an error after they are added. This feature was previously only available in the Word Analyzer.InVimYou can use a simple script to implement,And,You can customize this function as needed..For example,You can only check the comments in the program.
Part 2: editing multiple files 4. You often need to edit more than one file
People often do not edit only one file. there are usually multiple related files. several files may need to be edited once after a single file is edited. you can also edit several files. make full use of the editor for efficient editingEdit multiple files at a time.
The tag mechanism mentioned above can be used to jump between multiple files. The common method isGenerateTagFile. After that, you can freely jump between multiple files in the project to find function definitions, structures, Type Definitions typedef, and so on. compared to searching these files, you can save a lot of time;Before you browse a project, you must createTagsFile.
Another powerful mechanism is in a project.Find different places with a name in multiple files,Use": Grep"Command. Vim generates all matching lists and jumps to the First Matching place ."CN"Command to redirect you to its next match. This is for youTo change the parameters of a functionIt is very useful.
Files contained by # I nclude contain rich information, but it takes a lot of time to find out what you want. vim can process the files contained in # I nclude. in addition, you can find what you are looking. A common requirement is to view the prototype of a function.Position the cursor on the Function Name of the prototype you want to view.,Then press"[I"Command, VIMWill displayIncludeFile matches a list of function names. If you want to view its context information, you can jump to its declaration. A simple command can be used to check whether you contain the correct header file.
VimA partition can be divided into several different parts.,Edit each part separately,After editing, you can compare the content of two or more files., Or copy/paste text between them. there are many commands that can open or close windows or jump between them. temporarily hide files. and so on. use the preceding three rules to practice new commands.
Multiple windows have multiple purposes.The preview tag mechanism is a good example..It opens a special preview window and keeps the cursor at your current position..The text in the preview window lists the declaration of the function where the current cursor is located.(Some may not be declarations)Move the current cursor to another function name,Seconds,The content in the preview window will become a declaration about the new function name.
5. Collaborative operations
The editor is used to edit text, the e-mail program is used to send and receive emails, and the operating system is used to run user programs. each program has its own business scope. combining the functions of these programs can produce powerful processing capabilities.
A simple example: InSelect and sort some structured text in a list"! Sort". The external program "sort" handles real sorting. the sorting function can be integrated into an editor. however, if you look at a "man sort", you will find that it has many available options. it has a highly optimized algorithm for sorting. do you want to write an equally powerful sorting program in your editor? Or other stream Filtering programs? This will make your editor very bloated.
UNIXThe philosophy is to use independent applets.,Each applet performs a special task.,And make it ready,Integrate their work to complete a complex task.Unfortunately, most editors cannot work with other programs. For example, you cannot replace the e-mail editor in Netscape. another approach is to include all functions in one program. in the editor field, Emacs is a model in this field (some people even say it is an operating system that can edit text)
Vim is to integrate these scattered applets, but it is not easy to do so. Currently, the vim editor can be used in developer studio and sniff of Ms, some e-mail programs also support plug-in editors, such as mutt, you can use vim. integration with Sun's workshop can also work normally. vim remains to be further improved in the future. until we find a system that is better than all of this.
6. Structured Text
There may be some internal structures in the text you often deal. it is not supported by currently available commands. You may have to create your own macros and scripts to operate on these texts. this is obviously complicated.
The simplest thing is to speed up your edit-compile-Modify cycle.VimHave its own": Make"Command,This command is used to compile your program project.,Capture compilation errors/Warning and allow you to jump directly to cause this error/Warning program line up. If you have an alternative compiler, the error messages it outputs may be unrecognizable to VIM. It doesn't matter,Change your'Errorformat'Option,This option tellsVimWhat format error message will your compiler generate?,So that it can recognize. For example, how to find the wrong file name and the wrong row number. Since it can already work with the complicated error message format generated by GCC, you can imagine that, it also deals with errors generated by most other compilers.
Sometimes, you only need to set some options for adjusting a file in a special format and write some macros. to jump to the manual help document, you can write a macro to get the current word, clear the current buffer and read the corresponding help page, which is a simple and effective way to view the cross index.
With the above three principles, you can deal with any form of structured text. just think about what you want to do to the file, find out the edit command, and practice using it. it's as simple as it sounds. the only thing is that you must actually do it.
Part 3: 7. develop habits
It takes time to learn to drive, but is that enough reason for your continued cycling? No, you realize that you need to invest time to learn a skill. text editing is the same as this. You need to learn new commands and skills.
On the other hand, you do not have to learn all the commands provided by an editor. This will only waste your time.Most people only need to learn10-20%The command is enough to cope with their work..But for everyone,Suitable for different command Sets,This requires you to review what you have done from time to time.To see if some repetitive work can be completed automatically. if you only perform a special operation once and don't expect to perform similar operations in the future, don't try to figure it out. however, you may expect that you will repeat the same operation within a few hours. search for the Swiss army knife you want in the document or write a macro to complete it. if the task is too complex, such as processing special types of text, you can go to the newsgroup to see if someone has solved similar problems with you.
The decisive step is the last step. You may have found a solution for repeated operations, but you forgot it a few weeks later. that's useless. you need to repeat your solution until your fingers can automatically perform conditional reflection to reach your desired state. don't try too many things at a time. It would be much better to do one thing at a time and do it several times. for infrequent operations, it is best to write down your processing steps for future needs. in any case, as long as the target is clear. you can find a way to make your edits more efficient.
The last thing to remind you is that people often turn a blind eye to the suggestions mentioned above: I still often see people spend half a day rolling down the screen with two fingers. it's really hard for them. operations with ten fingers won't make them faster, and this is also the easiest way to get bored. you can use a computer program for one hour every day. You only need a few weeks to practice this operation.
Conclusion
The reason for this article is Stephen R. covey's masterpiece "The 7 Habits of highly valid tive people" inspired. I recommend it to everyone I know to solve personal or professional problems. some readers may say this is from Scott Adams's book "Seven Years of highly defective people" (also recommended for blood injection ). see "recommended books and CDs" at http://www.vim.org/iccf/click1.html ".
About the author
Bram moolenaar is the main author of vim. he has written Vim's core functions and is responsible for selecting code from other authors. he graduated from Delft University of Technology as a technician and is now mainly engaged in the software industry. but he also knows how to use the soldering iron. he is the creator and cashier of iccf in the Netherlands. this is an organization that helps orphans in Uganda. he worked as a system architect for free software, but in fact he spent a lot of effort on vim.