vim tab autocompletion

Original: http://www.cnblogs.com/gunl/archive/2011/08/15/2139588.html

I'm a vim fan, I've been using it for a long time, I have some feelings of my own, and I've dug up some other people's skills everywhere. I feel like I'm right about vim
Fans are more useful, so just record it here. I hope that this article will describe everyone's own skillful work, which is just in response to the ancient times of throwing bricks and attracting jade
Talked.

Let me introduce vim first. vi is a very common text editor under unix/linux.
Yes. vi has various variants, and different variant software is commonly used on different machines. Among them, vim is easier to use and more useful.
Extensive. vim is short for Vi IMproved, which means better vi. I personally think it is a very good editor (for
In order to avoid the attention of Emacs fans, I won't say it's the best). If you haven't used it, it is recommended to give it a try. Of course, vim is good for writing text.
The file is very convenient and easy to use, such as writing programs, html documents, etc., but it cannot be used to write word documents.

About the installation of vim, basic usage methods, etc. can be found on the Internet, so I won't be wordy here, if you have knowledge about vim
If you are interested, then take a look here (Chinese document): http://vcd.gro.clinux.org/

This article will talk about some of the more useful and commonly used commands. If you can use these commands proficiently, you will find editing files.
Very comfortable.

illustrate:
In the following example, xxx means input xxx in command mode and press Enter
In the following example: xxx means input xxx in extended mode and press Enter
Commands in parentheses indicate related commands.
Commands entered in edit mode or visual mode are noted separately.

1. Find

  /xxx(?xxx) means to search for a string matching xxx in the whole document, / means to search down, ? means
                  Look up. Where xxx can be a regular expression, I won't say much about the regular expression.
                  Generally speaking, it is case-sensitive. If you want to be case-insensitive, you must first enter
                  :set ignorecase
                  After finding it, enter n again to find the next match, and enter N to search in the opposite direction.

  *(#) When the cursor is on a word, enter this command to find the word matching the word
                  Next (upper) a word. Similarly, enter n again to find the next match, enter N for the inverse
                  to find.

  g*(g#) This command is similar to the previous command, except that it does not exactly match the word where the cursor is, and
                  is to match all strings containing the word.

  gd This command finds a word that matches the word under the cursor, and stops the cursor at a non-existent part of the document.
                  The first occurrence of the word in the comment section.

  % This command finds back brackets that match the cursor position, including () [] {}

  f(F)x This command means to search in the line where the cursor is located, to find the first x character to the right (left) of the cursor.
                  After finding:
                  Enter; means continue to look down
                  input, which means search in the opposite direction

2. Move the cursor quickly
   In vi, moving the cursor and editing are two different things. Because of the distinction, the cursor can be easily positioned.
Bit and edit. So it's useful to be able to move the cursor a bit faster.

  w(e) moves the cursor to the next word.
  b Move the cursor to the previous word.

  0 Moves the cursor to the beginning of the line.
  ^ Move the cursor to the beginning of the line.
  $ moves the cursor to the end of the line.

  H Moves the cursor to the first line of the screen.
  M Moves the cursor to the middle line of the screen.
  L Moves the cursor to the last line of the screen.
  gg moves the cursor to the first line of the document.
  G Moves the cursor to the end of the document.
  cf (ie ctrl key and f key pressed together) This command is page down.
  cb (that is, press the ctrl key and the b key together, the same below) This command is page up.

  '' This command is quite useful, it moves the cursor to the previous mark, such as using gd, *, etc.
                  After finding a word, enter this command again to return to the last position.

  '. This command works quite well, it moves the cursor to the last modified line.

  `. This command is quite powerful, it moves the cursor to the last modified point.

3. Copy, delete and paste
   In vi, y means copy, d means delete, and p means paste. Copy and delete are the same as cursor movement commands.
Combined, look at a few examples to understand.

  yw means to copy the content from the current cursor to the end of the word under the cursor.
  dw means delete from the current cursor to the end of the word under the cursor.
  y0 means to copy the content from the current cursor to the beginning of the line where the cursor is located.
  d0 means to delete the content from the current cursor to the beginning of the line where the cursor is located.
  y$ means to copy the content from the current cursor to the end of the line where the cursor is located.
  d$ means to delete the content from the current cursor to the end of the line where the cursor is located.
  yfa means to copy the content from the current cursor to the first a character after the cursor.
  dfa means to delete the content from the current cursor to the first a character after the cursor.

  specially:
  yy means to copy the line where the cursor is located.
  dd means to delete the line where the cursor is located.
  D means delete from the current cursor to the end of the line where the cursor is located.

  The complicated usage of copying, deleting and pasting is related to the register, you can check it yourself.

4. Numbers and Commands
   In vi, the combination of numbers and commands often means repeating the command, and if it appears at the beginning of the extended mode, it means the line
Number positioning. Such as:

  5fx means to find the 5th x character after the cursor.

  5w(e) Move the cursor to the next five words.

  5yy means to copy 5 lines below the cursor.
  5dd means delete 5 lines below the cursor.

  y2fa means to copy the content from the current cursor to the second a character after the cursor.

  :12,24y means to copy the content between lines 12 and 24.
  :12,y means to copy the content between the 12th line and the line where the cursor is located.
  :,24y means to copy the content between the line where the cursor is located and the 24th line. Delete is similar.

5. Enter characters quickly
   In vi, you are not required to enter every character, there are many ways to quickly enter some characters.
   Students who use linux/unix must have an experience. When entering commands under the command line, type in the first few characters and then press
The TAB system will automatically complete the remaining characters, and if there are multiple matches, it will be printed. This is the famous command
Completion (in fact, there is also a file name completion function in Windows). There are many string completion commands in vi, which are very convenient.

  cp(cn) In edit mode, enter a few characters and then enter this command, vi starts to search up (down)
                  Find the matching word at the beginning of the index and fill it up, and continue to enter this command to search in a loop. This command
                  will match all files opened in this vim program.

  cxl In edit mode, this command quickly completes the entire line, but only in this window
                  match in the document.

  cxf In edit mode, this command means to complete the file name. For example, enter:
                  /usr/local/tom and then enter this command and it will automatically match:
                  /usr/local/tomcat/

  abbr is an abbreviation. This is a macro action that replaces one abbreviation with another in edit mode
                  String. For example, those who write java files often enter System.out.println, which is very
                  is a hassle, so you should use abbreviations to reduce typing. You can do this:
                  :abbr sprt System.out.println
                  After entering sprt and then entering other non-letter symbols, it will automatically expand to System.
                  out.println

6. Replacement
   Substitution is vi's strong suit, since regular expressions can be used to match strings. Here are a few examples.

  :s/aa/bb/g replaces aa with bb in all strings containing aa that appear on the line where the cursor is located
  :s/\<aa\>/bb/g Replace all occurrences of aa on the line under the cursor with bb, only the word aa
  :%s/aa/bb/g Replace all occurrences of aa in the document containing aa with bb
  :12,23s/aa/bb/g will replace aa with bb in all occurrences of aa containing aa from lines 12 to 23
  :12,23s/^/#/ will add # character from the beginning of lines 12 to 23
  :%s= *$== remove all extra spaces at the end of the line
  :g/^\s*$/d Delete all empty lines that do not contain characters (nor spaces).

7. Multi-file editing
   It is convenient to open many files for editing in one vim program.

  :sp(:vsp) filename vim will split a horizontal (vertical) window and open the new file in that window.
                      Starting from vim6.0, the filename can be the name of a directory, so that vim will
                      Open the directory and display a list of files, press enter on the file name to start in this window
                      Open the file, if you enter O to open the file in a new window, enter ? to see
                      to help information.

  :e filename vim will open the new file in the original window, and will ask to save if the old file has been edited.

  What should I do if cww vim splits several windows? Enter this command to position the cursor circularly
                      into each window.

  :ls This command checks how many files the vim program has opened, at the bottom of the screen
                      The following data will be displayed:
                      1   %a      "usevim.html"         行 162
                      2   #       "xxxxxx.html"         行 0

                      in:
                      1 indicates the serial number of the open file, which is very useful.
                      %a represents the file code, % represents the currently edited file,
                                      # Indicates the last edited file
                      "usevim.html" represents the filename.
                      Line 162 indicates the cursor position.

  :b serial number (code number) This command will open the file with the specified serial number (code number) in this window, where the serial number (code number)
                      Just use the :ls command to see it.

  :set diff This command is used to compare two files, you can use
                      :vsp filename
                      command to open another file, then enter this command in each file window to see
                      to the effect.

8. Macro Substitution
   vi can not only use abbr to replace text, but also to define macros for commands. Some commands are very difficult to type,
So I define them on <F1>-<F12>, which is very convenient. These configurations can be pre-written to ~/.vimrc
($VIM/_vimrc under windows), do not write the preceding colon when writing it.

  :nmap <F2> :nohls<cr> unhighlight the searched string
  :nmap <F9> <CW>w Move the cursor to a different window in command mode
  :imap <F9> <ESC><F9> Run in input mode <F9>
  :nmap <F12> :%s= *$==<cr> Remove all extra spaces at the end of the line.
  :imap <F12> <ESC><F12>             同上

  :java: (Note, why is it said in java here, because the following definitions do not work for other file formats, the following
            will talk about how to achieve this)
  :nmap <F3> :comp javac<CR>:mak -d . %<CR>
       This command compiles the java file with javac, it will automatically position the cursor to the point of error. However, this needs to be specified
       Define a javac.vim file under $VIM/compiler, there are only two lines in javac.vim:
          setlocal makeprg=javac
          setlocal errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#

  :nmap <F4> :comp ant<CR>:mak<CR>
       This command compiles java files with ant, which automatically positions the cursor to the point of the error. Generally, installing
       There is already a compiler/ant.vim file after vim, so this command can be used directly. But it needs to be
       There is a build.xml file in the current directory, 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 errors.
  :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 position.
  :imap <F8> <ESC><F8>

  :nmap <F11> :JavaBrowser<cr>
       This command is used to split a new window in the left part of the window, the content inside is the resource tree of java, including
       The classes, the member variables and member methods of the classes appearing in this file are just as JCreator behaves.
       Type ? in this window and you will see help. Hey, it works great, but needs ctags support.
  :imap <F11> <ESC><F11>

9. TAB
   TAB is the tab character, and I took it out as a separate section because this thing is really useful.

   << Entering this command moves the line where the cursor is located one tab to the left.
   >> Entering this command moves the line where the cursor is located one tab to the right.
   5>> Entering this command will move one tab to the right after 5 lines of the cursor.
   :12,24> This command moves the data from lines 12 to 14 to the right by one tab.
   :12,24>> This command moves the data from lines 12 to 14 to the right by two tabs.

   So how to define the size of the tab? Some people are willing to use 8 spaces, some use 4, and some use 2.
   Some people hope that tabs are completely replaced by spaces, and some people hope that tabs are just tabs. It doesn't matter, vim can
   Help you. The following settings are generally written into the configuration file first, so as not to knock.

   :set shiftwidth=4 Set the automatic indentation to 4 spaces, of course, set the automatic indentation first.
   :set sts=4 means set softtabstop to 4. After entering tab, it jumps 4 spaces.
   :set tabstop=4 The actual tab is 4 spaces instead of the default 8.
   :set expandtab After typing a tab, vim fills the tab with the appropriate spaces.

10. autocmd
    This command is very powerful, you can use this command to apply different configurations to different file formats; you can
Automatically add copyright notices and so on when creating new files. These commands are generally defined in configuration files such as ~/.vimrc
Inside. Because he is very powerful, I can't give a very specific explanation. I can only give a few examples. For details, please see the help.

  :autocmd! Removes all previous autocommands.
  autocmd FileType        java  source ~/.vim/files/java.vim
  autocmd FileType        java  source ~/.vim/files/jcommenter.vim
    The above two commands allow me to apply the two configuration files mentioned later when I open the java file.
  autocmd BufNewFile      *.java  0r ~/.vim/files/skeletons/java.skel
    The above command allows me to automatically add the content of the java.skel file when I create a new java file.
  autocmd BufNewFile      *.java  normal gnp
    The above command allows me to automatically run the gnp command when I create a new java file, this command does some specialization
    Processing, such as replacing __date__ in the new java file with today's date or something.

11. Common scripts
    On vim.sf.net you can find a lot of scripts (scripts) that often do unexpected things.
The ones I use are:

    jcommenter.vim automatically adds javadoc-style comments.
    JBrowser.vim class resource browsing. C, C++ etc. can use Tlist

    There are many useful ones, such as checkstyle.vim to check your programming style, jad.vim to directly
    Decompile .class files, etc.

12. Regular placement
    In the ~/.vimrc configuration file you often need some personalized configuration. For example, some macro definitions written above, some
autocmd definitions, etc. For example:

    set suffixes=.bak,~,.o,.h,.info,.swp,.aux,.bbl,.blg,.dvi,.lof,.log,.lot,.ps,.toc
        This way, when opening a file in vim, it ignores the above file when pressing the tab key to complete the file name.

    set nu display line number
    set ai sets automatic indentation
    map Y y$ Let Y be the same as D, otherwise Y means the same as yy.

13. Others
    There are also many interesting commands, which are recorded here so as not to be forgotten.

    . Repeat the last edit command.
    :g/^/exec "s/^/".strpart(line(".")." ", 0, 4) insert a line number at the beginning of the line
    :runtime! syntax/2html.vim convert txt to html, it will follow your
                                                    color scheme to turn


Java support:

Support for tab skipping



Tags are Java identifiers, which can be package names, class names, method names, or field names. In your code create a large of all labels
Indexes allow you to jump to the definition of any class, method or field with a single keystroke. These index files are created using the ctags program,
The latter is available in the vim distribution.



Ctags will take a list of Java source files or directories containing Java source code, extract the identifier information from it, and then generate the required vi for jumping
Sort index to label. The following line of code is an example of a call to ctags, which will generate a ctags containing
A tag file with all the identifiers in the project source code, as well as 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. This step is done by setting the
tags=~/.tags is added to your ~/.vimrc file. With the tag index file available, and vi knows where to find it,
You can now jump to any identifier by pressing [CTRL]]. Although this is useful for jumping to methods you have defined, classes and words you write yourself
Useful for segments, but it would be even more useful if you added the Java Core Class Library to your tag index. Now, simply use any core library's
Press [Ctrl] ] on a class or method and you will jump to its definition written entirely in javadoc. .



Padding of tag names



  

While jumping to where class, method, and field definitions are located can be useful for viewing their javadoc and inspecting their content, it is more common to see
You just need to check the definition of a keyword. In most IDEs this is done by typing keywords. However, if you have set your
The signature file is set as described in the previous section, then filling in the keyword in vi is just a directional configuration.



set
Putting the line complete=.,w,b,u,t,I in your ~/.vimrc file allows you to automatically add any class, method, etc.
The method or field name is filled. Pressing [Ctrl]N continuously will move to the next matching place. When you see the tag you are looking for, just keep typing your source
The rest of the code will do. You can find other tips on tab name padding, including using the Tab key for padding, on the vim web site.



method folding



Good practice tells us: you should keep your classes short and simple, because program complexity is at the class level, not the 500-line method
superior. Unfortunately, some classes have slipped out of control and become virtually impossible to move.



IDEs often provide a table listing the methods in the content in a narrow box on the left to ease the developer's difficulty in viewing lengthy source code.
In contrast, vi provides a collapsible block called "methodfolding". Collapse allows you to hide indented
(indented) All code between the beginning and end of a code block. To activate folding, add set foldmethod=indent to your ~/vimrc.



Once the method's folding is activated, you can use :set
foldlevel=0 to greatly reduce a Java source file. If you set the number of folded levels to zero, you are telling vi to only display the command line that is not indented at all, which in most cases
Several Java source files represent wrapper, import, and class or interface definition lines. Setting foldlevel=1 is to tell vi that: except for the command line that is not indented, the command that has only been indented once
Lines should also be displayed, they include the method's signature and a few other minor things. To open a folder and see what a method hides, you would type zO on the command line that was folded.



Folding of methods can be easily turned on or off by setting the foldlevel. I like to use it when debugging, but not when writing new code. It's up to you to decide how it should fit into the way you work.

IDEs are not just for editing; they are also compilation environments. Vi does a very good job of eliminating the need to use an external compiler and dissecting the resulting information. Many Java projects will now be easy to script
Ant acts as a creation environment. Instead of loading a lot of Ant-specific settings into the .vimrc file, I created a separate contrived vimAnt script, see Listing B.



This script calls an Ant executable with a few arguments, then filters the output to include only the compiler output. You are now almost ready to call Ant from vi. you just need to put
Listing
Add the command line from C to your ~/.vimrc file. These command lines tell vi to call the vimAnt script you created and prompt it how to parse Ant's jikes output.



With the vimAnt script in your system path, you can now use the :make command to initiate compilation. The current directory and all its parent directories are searched for a build.xml file whose class object is called. Error output is automatically parsed by vi.



Once you compile with errors in vi, you can use the quick fix (quick
fix) mode to quickly reach the wrong location and make timely repairs. The :cn command will take you to the next error location, regardless of what file it is in. The :cc command will show you the current wrong compilation
browser output information; while :cl generates a list of all errors for the project for browsing the errors. Fix your bugs and you're only one step away from a successful compilation: make.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370237&siteId=291194637
Tab
Tab
Tab
Tab