Vim Tab key auto-completion

Source: Internet
Author: User
Tags imap alphanumeric characters
I am a fan of VIM. I have been using it for a long time. I have some feelings and found some other people's skills everywhere. I think it is useful to VIM fans. Remember it here. I hope that this article will help you develop your own clever work, so we will be able to introduce the old saying. let's take a look at vim. VI is a widely used text editor in Unix/Linux, which is available on most machines. VI has various variants, and different variant software is often used on different machines. among them, VIM is relatively easy to use and widely used. vim is short for VI improved, indicating better VI. I personally think it is a very good editor (in order to avoid the eye-catching of Emacs fans, it is not the best ). we recommend that you try it if you have never used it. Of course, VIM is easy to use for compiling text files, such as programming and HTML documents, but it cannot be used to compile Word documents. the installation and basic usage of VIM can be found on the network, so it is not here. If you are interested in Vim, take a look here (Chinese Document): Http://vcd.gro.clinux.org/this document describes some useful and commonly used commands. If you are familiar with these commands, you will find it very comfortable to edit files. note: In the following example, xxx indicates entering XXX in command mode and press Enter. In the example below: XXX indicates entering XXX in extended mode and entering the command in parentheses indicates related commands. the command entered in edit mode or visual mode will be noted separately. 1. search/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates searching up. here, xxx can be a regular expression. I will not talk about the regular expression much. generally, it is case-sensitive. To make it case-insensitive, you must first enter: Set ignorecase to find it, then enter n to find the next match, and enter n to search for it. * (#) when the cursor stays on a word, enter this command to search for the next (top) word that matches the word. similarly, input n to search for the next match, and input n to search for the inverse. g * (G #) this command is similar to the previous command, except that it does not fully match the word at the cursor, but matches all strings containing the word. GD this Command finds the word that matches the word where the cursor is located, and places the cursor in the non-comment section of the document where the word appears for the first time. % This command looks for the parentheses that match the cursor, including () [] {} f (f) X. This command indicates searching in the row where the cursor is located, search for the first X character on the right (left) side of the cursor. after finding: enter; continue to search for the input, Indicates reverse lookup 2. moving the cursor quickly in VI allows you to easily locate and edit the cursor because the cursor is moved and edited. it is useful to move the cursor faster. W (e) move the cursor to the next word. b. move the cursor to the previous word. 0 move the cursor to the beginning of the line. ^ move the cursor to the starting character of the line. $ move the cursor to the end of the row. h move the cursor to the first line of the screen. M moves the cursor to the middle line of the screen. l move the cursor to the end of the screen. gg move the cursor to the first line of the document. g move the cursor to the end of the document. c-F (press Ctrl and F together) this command is page down. c-B (that is, press Ctrl and B together, and then the same) this command is page up. ''this command is quite useful. It moves the cursor to the previous tag, for example, using Gd, *, and so on to find a word, and then entering this command will return to the position where it was last stayed. '. this command is quite good, it moves the cursor to the previous The number of modified rows. '. this command is quite powerful, and it moves the cursor to the last modification point. 3. copy, delete, and paste in VI, y indicates copying, d Indicates deleting, and P indicates pasting. copy and delete are combined with the cursor movement command. You can see the following examples. YW indicates copying the content from the current cursor to the end of the word where the cursor is located. DW indicates deleting the content from the current cursor to the end of the word where the cursor is located. y0 indicates copying the content from the current cursor to the beginning of the row where the cursor is located. d0 indicates deleting the content from the current cursor to the beginning of the row where the cursor is located. Y $ indicates copying the content from the current cursor to the end of the row where the cursor is located. d $ deletes the content from the current cursor to the end of the row where the cursor is located. yfa indicates copying the content from the current cursor to the first a character after the cursor. DFA indicates to delete the content from the current cursor to the first a character after the cursor. in particular, YY indicates copying the row where the cursor is located. dd indicates deleting the row where the cursor is located. d: Delete the content from the current cursor to the end of the row where the cursor is located. the complex usage of copying, deleting, and pasting is related to registers., Which can be queried by yourself. 4. the combination of numbers and commands in VI often indicates that the command is repeated. If the command appears at the beginning of the extended mode, it indicates that the row number is located. for example, 5fx indicates 5th x characters after the cursor. 5 W (e) move the cursor to the next five words. 5yy indicates copying 5 rows below the cursor. 5dd indicates deleting 5 rows below the cursor. y2fa indicates copying the content from the current cursor to the second a character after the cursor.: 12, 24y indicates copying content between 12th rows and 24th rows.: 12, y indicates copying the content between 12th rows and the row where the cursor is located. :, 24y indicates copying the content between the row where the cursor is located and the second row. deletion is similar. 5. you can enter characters quickly in VI without entering each character. if you use Linux/Unix, you must have an experience. when you enter a command on the command line, you can enter the first few characters and then press TAB. Then, the system will automatically complete the remaining characters, if there are multiple matches, it will be printed. this is the famous life (In fact, the file name filling function is also available in Windows ). VI has many character string completion commands, which are very convenient. c-P (c-N) in editing mode, enter a few characters and then enter this command, then VI starts to search for and complete the matched words at the beginning (bottom, enter this command repeatedly to search cyclically. this command will match all files opened in this Vim program. in C-X-l editing mode, this command is used to quickly complete the entire line of content, but only matches the content in the document that appears in this window. in C-X-F editing mode, this command indicates filling in the file name. if you enter/usr/local/Tom and then enter this command, it will automatically match:/usr/local/tomcat/abbr. this is a macro operation. You can use an abbreviation to replace another string in editing mode. for example, if you write a Java file, you often enter system. out. println, which is very troublesome, so we should use abbreviations to reduce the number of words. you can do this: Abbr sp RT system. out. after println is input, SPRT is input, and other non-alphanumeric characters are entered. It is automatically extended to system. out. println6. replacement is Vi's strength, because regular expressions can be used to match strings. the following are examples.: S/AA/BB/g replace AA in all strings containing AA in the row where the cursor is located with BB: s/\ <AA \>/BB/g replace all AA in the row where the cursor is located with BB. Replace only the word AA: % S/AA/BB/g replace AA in all strings containing AA in this document with BB: 12, 23 S/AA/BB/g replace AA in all strings containing aa that appear from row 12 to row 23 with BB: 12, 23 S/^/#/Add the beginning of the row from 12 to 23 # character: % s = * $ = Delete extra spaces at the end of all rows: g/^ \ s * $/d delete all blank lines that do not contain characters (space is not included. 7. Multi-file editing is convenient to open many files in a Vim program for editing.: SP (: VSP) file name Vim Splits a horizontal (vertical) window and opens a new file in the window. starting from vim6.0, the file name can be the name of a directory. In this way, VIM will open the Directory and display the file list. Press enter on the file name to open the file in this window, if you enter o, open the file in a new window. Enter? You can see the help information.: E file name Vim will open a new file in the original window. If the old file has been edited, it will be required to save. how does C-w Vim split several windows? Enter this command to cyclically locate the cursor in each window.: ls this command shows how many files have been opened in the vim program. The following data is displayed at the bottom of the screen: 1% a "usevim.html" line 162 2 # "xxxxxx.html" line 0 where: 1 indicates the number of opened files, which is very useful. % A indicates the file code, % indicates the current edited file, # indicates the Last edited file "usevim.html" indicates the file name. row 162 indicates the cursor position.: B serial number (CODE) This command opens the file with the specified serial number (CODE) in this window, where the serial number (CODE) is seen by the LS command.: Set DIFF: this command is used to compare two files. You can use the: VSP filename command to open another file and enter this command in each file window to see the effect. 8. macro replacement VI can not only replace text with abbr, but also define macro commands. some commands are very popular. Therefore, I define them to <F1>-<F12>, which makes it very convenient. These configurations can be written in advance ~ /. In vimrc ($ Vim/_ vimrc in Windows), you do not need to write the preceding colon.: NMAP <F2>: nohls <CR> deselect the highlighted: NMAP <F9> <C-W> W command mode to move the cursor to different windows: imap <F9> <ESC> <F9> run in input mode <F9>: NMAP <F12>: % s = * $ == <CR> Delete unnecessary spaces at the end of all rows.: imap <F12> <ESC> <F12> same as in Java: (Note: Why is the definition in Java not effective for other file formats, the following describes how to achieve this.) NMAP <F3>: comp javac <CR>: Mak-D. % <CR> This command uses javac to compile the Java file. It automatically positions the cursor to an error point. however, you need to define a javac. the Vim file is in $ Vim/compiler Javac. vim contains only two lines: setlocal makeprg = javac setlocal errorformat = % A % F: % L: \ % m, %-Z % P ^, %-C %. % #: NMAP <F4>: comp ant <CR>: Mak <CR> This command uses ant to compile the Java file. It automatically positions the cursor to an error point. generally, compiler/ant is available after Vim is installed. vim file, so this command can be used directly. however, the current directory must contain build. XML file, of course, ant must be installed.: NMAP <F5>: CL <CR> this command is used to view all compilation errors.: imap <F5> <ESC> <F5>: NMAP <F6>: CC <CR> this command is used to view the current compilation error.: imap <F6> <ESC> <F6>: NMAP <F7>: CN <CR> this command is used to jump to the next error location. .: Imap <F7> <ESC> <F7>: NMAP <F8>: CP <CR> this command is used to jump to the previous error location.: imap <F8> <ESC> <F8>: NMAP <F11>: javabrowser <CR> this command is used to split a new window at the left of the window, the content is the Java Resource Tree, including the classes, member variables and member methods in this file, as shown in jcreator. in this window, enter? You will see help. hey, it's very easy to use, but it needs support from ctags.: imap <F11> <ESC> <F11> 9. tab tab is a tab, Which is used separately for a section because it is really useful. <enter this command to move a tab from the row where the cursor is located to the left.> enter this command to move a tab to the right of the row where the cursor is located. 5> enter this command to move a tab to the right of the 5 rows after the cursor. :> This command moves the data from 12 rows to 14 rows to the right of a tab. :> This command moves the data from 12 rows to 14 rows to the right of two tabs. how can we define the tab size? Some people prefer to use 8 spaces, 4 for some, and 2 for some. some people want tabs to be replaced by spaces, and some want tabs to be tabs. it doesn't matter. Vim can help you. the following settings are usually written to the configuration file first, so that you do not need to repeat them.: Set shiftwidth = 4 set Automatic indent 4 spaces, of course, you must set Automatic indent first.: Set STS = 4: Set softtabstop to 4. after entering the tab, 4 cells are displayed.: Set tabstop = 4 the actual tab is 4 spaces, instead of the default 8.: after entering the tab, VIM fills the tab.10. autocmd command with appropriate spaces. You can use this command to apply different configurations to different file formats; you can automatically add a copyright statement when creating a file. these commands are generally defined in ~ /. In a configuration file such as vimrc. He is very powerful, so I cannot give a very specific description. I can only give a few examples. For details, see help.: autocmd! Delete all previous automatic commands. autocmd filetype Java source ~ /. Vim/files/Java. Vim autocmd filetype Java source ~ /. Vim/files/jcommenter. Vim the preceding two commands enable me to apply the two configuration files mentioned later when opening the Java file. autocmd bufnewfile *. Java 0R ~ /. Vim/files/skeletons/Java. this command above skel allows me to automatically add Java when creating a Java file. the content of the SKEL file. autocmd bufnewfile *. the above command in Java normal GNP enables me to automatically run the GNP command when creating a Java file. This Command performs some special processing, for example, replace _ date _ in the new Java file with the date of today. 11. common scripts in vim.sf.net, you can find many scripts, which often have unexpected effects. I usually use jcommenter. vim automatically adds javadoc-style annotations. jbrowser. vim resources. c, C ++ and so on can use tlist, there are many useful, such as checkstyle. vim can test your programming style, Jad. vim can be decompiled directly. class files, etc.. 12. Common configurations are available in ~ /. You often need some personalized configurations in the vimrc configuration file, such as some macro definitions and some autocmd definitions written above. For example: Set suffixes =. Bak ,~,. O ,. H ,. info ,. SWP ,. aux ,. BBL ,. blG ,. DVI ,. LOF ,. log ,. lot ,. PS ,. in this way, when you open a file in Vim, it ignores the above files when you press the tab key to complete the file name. set nu displays the row number set AI settings to automatically indent map y $ to make y and D the same, otherwise y's intention is the same as yy. 13. there are many other interesting commands recorded here to avoid forgetting .. repeat the last edit command.: G/^/exec "s/^ /". strpart (line (". "). "", 0, 4) Insert row number at the beginning of the row: runtime! Syntax/2html. vim converts TXT to HTML, which is converted to Java based on your color configuration. tags that support tag jumping are Java identifiers, which can be the packaging name, class name, and method name, or field name. Creating a large index for all tags in your code allows you to use a single key to jump to the definition of any class, method, or field. These index files are created using the ctags program, which are available in the vim release. Ctags obtains the Java source file or a list of directories containing the Java source code, extracts the identifier information from the list, and then generates the sort index required by VI to jump to the tag. The following code is an example called by ctags. As long as the java_home variable is correctly defined, it will generate a Tag file containing all the identifiers in the project source code, as is the sun core library source code: ctags-f ~ /. Tags-R ~ /Myprojects/src $ java_home/src once the tag index is created, all you need to do is tell VI where to find it. In this step, set settags = ~ /. Add tags to your ~ /. Implemented in the vimrc file. With the available tag index file, and VI also knows where to find it, you can now jump to any identifier by pressing [CTRL. Although this is useful for jumping to the methods, classes, and fields you have defined, it is more useful if you add the Java core class library to your tag index. Now, simply press [CTRL] for the classes or methods of any core class library, and you will jump to the definition compiled by javadoc .. Although it is useful to jump to the class, method, and field definition location to view its Javadoc and check its content, the more common case is that, you just need to check the definition of a keyword. In most ides, this is done by entering keywords. However, if you have set your tag file as described in the previous section, entering the keyword in VI is just a guiding configuration. Put the setcomplete =., W, B, U, T, and I line in your ~ /. In the vimrc file, you can automatically fill up any class, method, or field name in insert mode by pressing [CTRL] n. Press [CTRL] n consecutively to move to the next matched place. When you see the tag you want to find, you only need to continue entering the remaining part of your source code. You can find other tips about label name filling on Vim's web site, including using the tab key for filling. The practical experience of method folding tells us that you should keep the class short and simple, because the complexity of the program is manifested in the hierarchy of the class, rather than the method of the 500 rows. Unfortunately, some classes have been out of control and become actually unable to be moved. IDE often provides a table listing the methods in the content in a narrow box on the left to ease the difficulty for developers to view lengthy source code. In contrast, VI provides a foldable block called methodfolding. Collapse allows you to hide all the code between the beginning and end of an indented code block. To activate fold, add set foldmethod = indent to your ~ /Vimrc. Once method folding is activated, you can use setfoldlevel = 0 to greatly streamline a Java source file. If the number of collapsed layers is set to zero, you are telling VI to only display command lines that are not scaled down, while the latter represents packaging and import in most Java source files, and define rows for classes or interfaces. Setting foldlevel = 1 tells vi that, except for the command lines that have not been scaled down, the command lines that have been scaled down once should also be displayed, they include method signatures and a few others. To open a folder and view the hidden content of a method, enter Zo on the collapsed command line. You can easily enable or cancel method folding by setting foldlevel. I like to use it during debugging, but I do not like to use it when writing new code. You have to decide how to adapt to your work. IDE is not only used for editing, but also for compiling environments. VI does a great job without using external compilers and profiling result information. Currently, many Java projects use ant, which is easy to write scripts, as a creation environment. I didn't load many ant-specific settings to the. vimrc file, but created a script named vimant separately. See listing B. This script calls an ant executable file with several parameters, and then filters out the output to include only the compiler output. Now you are almost ready to call ant from VI. You only need to add the command lines in listingc to your ~ /. Vimrc file. These command lines tell VI to call the vimant script you created, and prompt how it analyzes ant's jikes output. With the vimant script in your system path, you can now use the: make command to initialize the compilation. The current directory and all its parent directories will be searched to find a build. xml file. The class object of this file will be called. The error output is automatically parsed by VI. Once you compile with errors in VI, you can use the quick fix mode to quickly reach the location of the error and fix it in time.: The CN command will take you to the next error location, regardless of the file in which it is located.: The CC command will show you the compiler output information of the current error. cl will generate a list of all project Errors for you to browse. After fixing your errors, you are only one away from successful compilation: Make. There is also an article about Vim plug-ins written by DIAN Hu (a cool man on Shui mu): http://learn.tsinghua.edu.cn: 8080/2005212716/html/vimplugins.html
Related Article

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.