Although it is an old article, it is still very helpful to read it every time.
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: edit an object 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 you have found a word and want to find out where the word appears, you can use the "*" command to find 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 found matches,
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) you can use the "%" command to jump from an opened bracket to another matching bracket, you can also jump from a preprocessing command "# If" to the correct one "# 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 "[{" to jump to the beginning of the current code block (the code block is a program segment enclosed ). you can use "GD" to jump to the local definition of the word (variable) where the current cursor is located.
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 operations you need to repeat frequently. or you spend most of your time doing something. think about whether there is an editing command to 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.
For example, how to do this: when you write a C program, you often need to spend time finding a function definition. now you are using the "*" command to find out where this function name has appeared, but 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. when you look at the reference manual, you will find a reference to tag. this document describes how to use this function to jump to the function definition. this is 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. If you want to change the word that appears everywhere in the entire file to another, you can consider using the ": s" command, if you want to make changes selectively, and you 'd better decide after you read the context, you can use the "*" command to find another match of the word. If you want to change it, use "CW" to change these words, and then use the "N" command to go to the next match ". "Repeat the previous command. ". "The Last Command has been changed again. 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. it is useful to use the "M" command to mark a location of 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 similar matches, and insert them directly, and it will also Recursively search in your include file. you can type "xpmcr", then press the CTRL-N key, VIM will expand it to "xpmcreatepixmapfromdata", this function is not only for you to save time, it also reduces the chances of errors when you type them manually,
In addition, your compiler will not generate such warning errors.
There is also a quick way to repeat a phrase or sentence. vim has a macro recording mechanism. enter "QA" to input a macro into register variable 'A. 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 recorded, you only need to use the "@ A" command. there are 26 available 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 ")"
Some other common errors can be checked by the syntax highlighting function, such as # supported ded <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 it are considered as an error, using a syntax file, you can highlight all words that are not in the file list by syntax. with a special macro, you can add new words to the word list. after they are added, they are no longer considered an error. in the past, this function was only available in the Word Analyzer. you can use a simple script in VIM to implement this function. You can also 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. to make efficient edits, you must make full use of the editor's function of Editing Multiple files at a time.
The tag mechanism mentioned above can be used to jump between multiple files. the common method is to generate a Tag file for your project. 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 for these files, you can save a lot of time. Before you browse a project, you must create a tags file for it.
Another powerful mechanism is to find a different name in multiple files in a project and use the ": grep" command. vim generates a list Of all matches and jumps to the first match. the "cn" command redirects you to its next matching position. this is very useful for changing the parameters of a function.
The files contained by # include contain rich information, but it takes a lot of time to find out what you want. vim can process # include files. 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 name of the function whose prototype you want to view, and press the "[I" command. Vim will display a list of function names matching the include file. 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.
In vim, you can divide a partition into several different parts and 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 declared) Move the current cursor to another function name for several 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: select some structured text in a list and sort it "! 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.
The philosophy of UNIX is to use independent applets, each of which performs a specialized task and completes it well, integrating their work together 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. vim has its own ": Make" command, which compiles your program project, capture compiled errors/warnings and allow you to directly jump to the program line that causes this error/warning. 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 tells Vim the format of the error message that your compiler will generate so that it can recognize it. 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. that will only waste your time. most people only need to learn 10-20% of the commands to cope with their work. however, for each person, the appropriate command set varies with each other. You need to review what you have done from time to see if you can automatically complete repetitive work. 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,
For example, to process special types of text, you can go to the newsgroup to see if someone has solved the problem similar to yours.
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.