Two good vim skills

Source: Internet
Author: User
Tags imap alphanumeric characters
Two good vim Tips: 1. copy and paste the text into the visual mode. you can select multiple lines. After multiple rows are selected, you can use yy or dd to copy and cut them. P is to paste y and d. you can directly copy or cut the selected content. yw is to copy a word. if you want to copy the entire line, two good vim Tips 1. select text/copy and paste v as the visual mode. you can select multiple lines. After multiple rows are selected, you can use yy or dd to copy and cut them. P is to paste y and d. you can directly copy or cut the selected content. yw is to copy a word. The simplest way to copy the entire line is V, y, p is the line. v can select any character in a row, and V is the row Selected. a whole row at a time, and multiple rows are selected by moving the cursor down or up. For the files selected by v, there are so many copies after the copy operation. for the files selected by V, the paste operation is automatically switched to the next command mode, which is also the block selection, however, column blocks are selected. 2. the folding code can be folded by zf. you can use zo to open the folding, or press the arrow key to the right to open the folding, and zc to close the folding (as long as any statement in the collapsed block). 3. the indent code is right indent selected by v, and then press = to automatically format the code, Auto Indent, the internal recursive indent has achieved the row left shift>, this command is easy and quick to adjust the source code indent format. 4. move the cursor % from the beginning of the braces to the end position of the braces: adding a row number to the back is to jump to this line and the cursor returns to the previous position. It is equivalent to the "undo" move of the cursor. the cursor returns to a later position. It is equivalent to "restore" when the cursor moves. 5. the multi-file editing/buffer command vim corresponds to a buffer for each opened file ). Multi-file editing involves two situations. One is that the parameter used before entering vim is multiple files (this case is called argument list ). Another scenario is to open other files (called buffer list) after entering vim ). However, they can all be collectively referred to as buffer. 5.1 open the file vi flname1 flname2... FlnameN transfers multiple files to the buffer, which is an argument list.: E filename: after entering vim, open other files without leaving vim. as long as the file you want to edit is in the current directory, you can still use the Tab to complete the key. Is the buffer list. Note: e or: new or: split can be followed by the directory, so that you can slowly find the file to be opened in the directory. The 5.2 buffer jump: n edits the next file.: 2n: edit two files.: N: Edit the previous file. Note that this method can only be used in the case of argument list.: Rew returns to the first file: args to view the status of the currently edited buffer File: e # or Ctrl-^ edit the previous file, which is quite useful for mutual editing of the two files. This method can be used between argument list or buffer list files. The Ctrl-^ command is more convenient, but the function will be invalid if the terminal type is incorrect. Use: ls to display the files in the buffer. # indicates the previous file, which can be entered through: e #. % a indicates the current file, if there is nothing, you can enter through: bn, where n represents the file number.: Move the file name or number B to the file. In: ls, the number of each file is displayed, which will not be changed before vim is left. This command is also available for elvis. Of course: e # numbers are also acceptable. this rule is applicable to all vi clones. In buffers, minus sign-indicates that the buffer is not loaded. However, you don't have to worry about it. loading is quite fast. The plus sign + indicates that the buffer has been modified.: Bn buffer next. N indicates the letter n: bl buffer last. The preceding two commands are not applicable to elvis. If you use the vim GUI, the Buffers option will be available on the menu, so you can easily know and move the buffer. 5.3 remove the buffer: The bd (elete) buffer will not be removed before it leaves vim. you can use this command to remove it. What is it about to remove it? Vim is loaded only when you call it. Therefore, these buffers do not occupy memory like cache. 5.4 re-edit the modified file and re-edit it. (1) run the following command: q! Force exit and then re-enter vi flname. (2) use the command: e! Force re-edit is more convenient. In this way, the file will be opened, but the changes to the current edited file will be abandoned. Otherwise, if the file has changed, vim defaults will not allow you to leave.: E! If nothing is received, all modifications are discarded and the files in the editing are reloaded. 5.5 Other commands: files or: buffers or: ls will list all files in the current buffer. In elvis, you can use B to call buffers.: F or Ctrl-g: displays the currently edited file name, whether the file is modified, and where the current cursor is located.: F file name changes the file name in editing. (File): The r file name inserts the content of a file at the position of the cursor. (Read): The 35 r file name inserts the file into 35 rows. Gf: this is a special method for vim to open a file. it will open the file named "word" at the place where the cursor is located. of course, this file must be in the current directory; otherwise, a new file will be created. 6. after the search command uses/to find a word, n can jump to the next one, and N is the previous one. when nohls can cancel the highlighted search, The: set ignorecase option causes VIM to ignore the case sensitivity. ": set noignorecase to disable this function. 7. modify the text cw: Delete a word and enter the insert mode. cc: Delete a row and enter the insert mode. R: the entered letters will replace the current letter and keep the command mode. R will not stop replacing (one by one ). 0 to the beginning of the line, $ to the end of the line. 8. the jump ctrl +] and ctrl + T between functions are the definition and return of the search function. it seems that the support of ctag is required to jump to the definition line of the identifier pointed by the cursor, is to open a new small window display, remember to add an I, it is best to find the file under your project, otherwise find the library function is not as good as man. capital K to see the man help page of the identifier referred to by the cursor 9. window command = ctrl + w: The split file name displays the content of multiple files on a page at the same time. similar to multiple windows, use the switch current window f to split the file name at the cursor, VIM searches for the file name in path. for example, if it is commonly used to open files in # include statement _ when several files are opened at the same time, press _ to maximize the current window. run Ctrl-W to specify the cursor to move: ctrl-W + expand the window Ctrl-W-zoom out the window Ctrl-W h move to the left of the window Ctrl-W j move to the bottom of the window Ctrl-W k move to the top of the window Ctrl-W l move to the right of the window after pressing, Loosen the keyboard, and then press the next command. if you want to close the split window, you can use close. if there is only one window left, it cannot be closed. Multiple windows are split, just like opening multiple files with e is to put the files in the buffer zone. 10. auto-complete I-up search, complete one word I-down search, and complete one word I-complete one line. For example, you have written a line for (int I = 0; I. If the line is not the one you want, you can search for it in the file system by or by the previous or next matching line, if a file name is supplemented by or by one word, no matching is found in the current file. VIM searches for the file in the # include statement, and the file location is searched in the path. I copied the characters in the corresponding column of the previous row. I copied the characters in the corresponding column of the next row. both of them can be pressed down until the end of the row stops. 11. comment The entire content comment block method: after selecting the line I // Esc I to be commented out, the input is inserted to the front edge of the selected row until Esc. to remove the comment, select the annotator //. After column d is selected, column I is inserted at the front and side, and column A is selected in column block mode, is row selected, v is normal selected 12. other commands u can undo the previous operation, ctrl + r can restore I command to execute a common mode command, and return to the insert mode after the execution is complete, you do not need to enter special ASCII characters or keys for subsequent Esc I characters multiple times.. Repeat the previous operation at the current position of the cursor :! Execute an external command ********************************** **************************************** * ******* I am a fan of vim, after a long time, I had 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 article describes some of the useful and commonly used commands. if you are familiar with these commands, you will find it quite 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. in vi, moving the cursor and editing are two things. because they are distinguished, you can easily perform optical calibration and editing. 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 Last modified row. '. 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. complex usage and mailing of copy, delete, and paste Memory, you can query it by yourself. 4. the combination of numbers and commands in vi often indicates repeated execution of this command. if it appears at the beginning of the extended mode, it indicates line number location. 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 command completion (in fact, the file name completion 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 sprt System. out. after println is input, sprt is input, and other non-alphanumeric characters are entered. it is automatically extended to System. out. println 6. 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/\/bb/g replace all aa in the row where the cursor is located with bb. replace the word aa only: % 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 empty lines that do not contain characters (spaces are not included) Delete. 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 lost. It is very difficult, so I define them to-, 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: nohls cancel the highlight of the searched string: Transfer the cursor to different windows in nmap w command mode: Run in imap input mode: nmap: % s = * $ = delete unnecessary spaces at the end of all rows.: imap is the same as that in java: (Note: Why is it in java? because the following definition does not work for other file formats, we will discuss how to implement this here): nmap: comp javac: mak-d. % 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 and in javac. vim contains only two lines: setlocal makeprg = javac setlocal errorformat = % A % f: % l: \ % m, % -Z % p ^, %-C %. % #: nmap: comp ant: mak this command uses ant to compile the java file, which automatically positions the cursor to the 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: cl this command is used to view all compilation errors.: imap: nmap: cc this command is used to view the current compilation error.: imap: nmap: cn this command is used to jump to the next error location.: imap: nmap: cp this command is used to jump to the previous error location.: imap: nmap: javabro this command is used to split a new window in the left of the window. the content in the window is the java resource tree, including the classes in this file, class member variables and member methods, as shown in JCreator. in this window Input? You will see help. hey, it's very easy to use, but it needs support from ctags.: imap 9. TAB is a TAB, which is taken out separately for a section because it is really useful.> 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.: set expandtab after entering the tab, vim fills the tab with appropriate spaces. 10. the autocmd command is very powerful. 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 file And so on. 12. common configuration 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 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 according to your color configuration.
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.