Vim skills and experience

Source: Internet
Author: User

Http://www.imtxc.com/blog/2012/04/25/vim-tips-and-experience/

First, clarify some environments and terms:
I am using vim7.3 On Debian Linux;
In the following configuration,<CIndicatesCtrlKey, such<C-v>Hold downCtrlPost-PressvKey;
In the following configuration,<MIndicatesAltKey, such<M-t>Hold downAltPost-PresstKey;
The "Front" mentioned in the description generally means "right or bottom", and "back" generally means "Left or top ";
The words I mentioned generally represent an English letter or a Chinese character;
The term "word" I mentioned represents an English word. For Chinese words, even words separated by English or punctuation marks are not logical words.

I. Basic operations

This part of content does not require additional plug-ins and configuration files. In fact, I think this is some of the operations that must be familiar with before using vim.

1. Switch Mode
1
Esc C-[ C-c i I o O a A v V <C-v>

These are the keys for switching mode. You can switch between different Linux modes.

  • Esc C-c C-[It refers to operations that return from other modes to the normal mode. Personal TendencyEscBecause the strength of the left-hand thumb is too small, it always feels uncomfortable and varies from person to person.
  • i/a: Insert before/after the current prompt
  • I/A: In vim, upper-case commands can be divided into reverse or full-line commands represented by lower-case letters, it is the function and the whole row, that is, insert at the beginning/end of the row
  • o/O: Insert under/upstream of the current row
  • v/V/<C-v>Is a command to enter the visual mode. uppercase V indicates the choice of rows and blocks.
2. Repeat the command
1
. [N]

These two are very important commands.

  • .Is to repeat the last action,[N]Represents a number, which can be used together with moving, editing, and selecting commands to perform the preceding operations more quickly.
3. Move commands
1
h j k l 0 ^ $ e E b B w W f F t ; , gg G H L M ENTER [[ ]] % :[N]

These are the moving commands in Vim, that is, to locate the cursor position in normal mode.

  • j/ENTER: Move to the next row. If you want to move to the next n rows, refer to the following[N]This section.
  • k: Previous line
  • h/l: Shifts one character left/right
    PS: actuallyENTERThe command is basically not used. It is used to move up, down, left, and righthjklThat's it. Many people just started using it.
    Vim is scared by these "awkward" key operations and feels very uncomfortable. After you get used to it, you will feel how good this design is, to help beginners discard the arrow keys (that is, draw arrows)
    In the following configuration file, I disabled the direction keys for a simple configuration, which can speed up the basic keyboard operations for beginners familiar with vim, however, the configuration file is not mentioned here.
    When it comes, I will explain it.

  • 0 ^The two commands move the cursor to the beginning of the line,^Is the first visible character to be moved to the row, while0Is the first row to be moved to the whole row.
  • $: Locate to the end of the row
  • e/bMove one word forward/backward,eAfter the command is moved to the next word, the last character of the word is located.
  • wMove forward to the first character of the next word.
  • f/FIs fast moving, f I understand the meaning of find here, for example, inthis is a text line.In such a line of text, if the cursor is located at a, then Ifn, It will be located at the n characters of the word line. Similarly,FThis is what reverse lookup means.
  • tCommands andfSimilar, but in the previous example, if you usetnThen, it will be located at the I letter of the word line, that is, the first character to be searched.
  • ;Use abovef/tAfter the command, if you need to find the next character, use;.
  • gg/G: Jump to the Start/end of the file.
  • H/M/L: Jump to the top, middle, and bottom of the current screen.
  • [[/]]: Move to the upper/lower section. In C Programs, it is generally the upper/next function.
  • %: Used to jump to matching brackets/quotation marks, or even jump between the corresponding condition compilation # If, # endif.
  • :[N]: Jump to the row with the specified row number n. For example:67It will jump to the 67th rows.
    Jump commands should also be mentioned in mobile commands.m 'In fact, it is also a mobile command
    These two commands are used together. The jump command, also known as the marks command, is to mark some locations of the file and then return to this location for further editing, for example, you can usemaAnd then in another location of the file'aNext, jump to the next step.maLocation of the record.
    However, the most commonly used jump command is:

  • ‘[Jump to the first character of the last modified text
  • '.Jump to the last place where the text was modified
  • ''Jump back to the last hop
  • '^Jump to the last end of the insert mode
    Many of the mobile commands mentioned here can be repeated with those mentioned above.[N]For example5jIndicates moving 5 rows down,5wIndicates moving five words forward.
4. Edit commands
1
x X d y p P "ayy "ap r R c o D C s S Y u C-r .  << >>
  • x/XIs to delete a character before/under the current cursor.
  • d/c/yIt is the delete/modify/Copy command. The reason why these three commands are put in a group is that these three commands can be used together with the previous commands such as mobile jump, suchdd/yyDeletes/copies a row,dw/cw/ywIs to delete/modify/copy a word,d$/c$/y$Deletes, modifies, and copies data to the end of a row,d^/y^It indicates to delete/copy to the beginning of a line. Of course, these commands can also be combined with repeated commands, such5ddDelete 5 rows,d/c/yfxDelete/modify/copy to character X, and so on. The commands that can be used are also described below.vCommand.
    The preceding commands can be flexibly combined to facilitate code writing, for example:
  • df), yf), cf), vf)Delete from the current character (copy, change, select) until after =
  • di), yi), ci), vi)Delete (copy, modify, and select) the content in the brackets
  • dt”, yt”, ct”, vt”Delete (copy, change, and select) from the current character until "before"
  • diw, yiw, ciw, viwDelete (copy, change, select) the word where the cursor is located
  • da”, ya”, ca”, va”Delete (copy, change, select) all text in ", including quotation marks
    In fact, the DELETE Command refers to the cut command in vim.
  • p/PThe two commands are used to copy or cut the content before or after the current character.
    Of course, we will encounter this problem in the process of using the content, delete the content in two places, and then paste the content separately. In this case, we need to use"ayy "apThis command,"ayyIs to copy the current row to the Register named,"apPaste the content in register a here.
5. Search/replace

From here, we will introduce another mode in VIM: Command mode, input in Normal Mode:You can enter the command mode,EscReturns the normal mode.

  • /Forward lookup
  • ?Backward Search
  • nLast redo // or?
  • NReverse redo last time // or?
  • \cCase Insensitive when searching
  • \CSearch for uppercase and lowercase letters
  • *Search for words under the current cursor
  • #Searches for words under the current cursor in the opposite direction.
    Here, search and replace can be used with regular expressions.
6. Record
1
q @

The record I understand is what macros mean. The Q Command needs to be used twice during usage: indicates the start record for the first time. After the Q command is pressed for the first time, you need to enter a character to indicate where the macro of the record is to be stored, the second time indicates the end of the record. Q needs to be used. @ Is to read the operation records in the specified register and re-execute these operations in sequence. Here is an example to describe the number from 1 to 100:

12345678
i1<ESC>qayyp<C-a>q100@a
7. Multiple tags

When editing multiple files, you can use multiple tags to conveniently switch between different files.

  • :tabnewCreate a tab
  • :tabcloseClose Tab
  • :tabedit {file}Create a new tab and open file in the new tab.

  • gtNext Tab
  • gTPrevious Tab
8. Multiple windows

If the screen is large enough, it is more comfortable to use multiple windows to edit files than to use multiple labels. For example, I can write in. the C file is displayed in a window next to it. the content of the H file.

  • :sp {file}Horizontal cut window, and open file in new window
  • :vsp {file}Vertical Cut window, and open file in new window
  • C-w wCommands are switched between different windows. In the subsequent configuration file, I will mention that you can easily switch between multiple windows by defining the configuration file.
Ii. configuration files

This part is customized through the configuration file to make it more in line with your editing habits. Here, I will describe the complete configuration file here.

Backup file configuration
1234
set backup " Enable backupset backupdir=~/.vim/backup " Set backup directoryset directory=~/.vim/swap,/tmp " Set swap file directoryautocmd BufWritePre * let &backupext = strftime(".%m-%d-%H-%M") " Keep more backups for one file
Some configurations in the Search Mode
12345
set magic " Enable magic matchingset showmatch " Show matching bracetsset hlsearch " Highlight search thingsset smartcase " Ignore case when searchingset ignorecase
Useful keyboard binding

Key Binding for multiple tags and Multi-Window editing

12345678910
nnoremap tp :tabprevious<CR>nnoremap tn :tabnext<CR>nnoremap to :tabnew<CR>nnoremap tc :tabclose<CR>nnoremap gf <C-W>gfnmap <silent> <C-k> <C-W><C-k>nmap <silent> <C-j> <C-W><C-j>nmap <silent> <C-h> <C-W><C-h>nmap <silent> <C-l> <C-W><C-l>

F [N] Key Binding:

1234567891011
nnoremap <silent> <F2> :TlistToggle<CR>:TlistUpdate<CR>nnoremap <F3> :Rgrep<CR>nmap <F4> :noh<cr><ESC>inoremap <F5> <C-R>=strftime("%Y-%m-%d %T %Z")<CR>nnoremap <F5> :w<CR>:make!<CR>nnoremap <F6> :w<CR>:make! %< CC=gcc CFLAGS="-Wall -g -O2"<CR>:!./%<<CR>inoremap <F6> <ESC>:w<CR>:make! %< CC=gcc CFLAGS="-Wall -g -O2"<CR>:!./%<<CR>nnoremap <silent> <F7> :botright copen<CR>nnoremap <silent> <F8> :cclose<CR>nnoremap <silent> <F9> :NERDTreeToggle<CR>nnoremap <silent> <F10> :set number!<CR>

Direction keys disabled:

12345678
map <UP> <NOP>map <DOWN> <NOP>map <LEFT> <NOP>map <RIGHT> <NOP>inoremap <UP> <NOP>inoremap <DOWN> <NOP>inoremap <LEFT> <NOP>inoremap <RIGHt> <NOP>
Autocmd

Autocmd is some commands that Vim automatically executes based on the judgment file format. For more information about autocmd configuration, see my complete configuration file. In the configuration file, all the configurations are commented out. With the preceding commands and configurations, you can understand them and modify them as needed.

Iii. Introduction to plug-ins

With vim, you can use some plug-ins to enhance functions. I use Vim to edit files such as C, makefile, and markdown in Linux, therefore, the list of plug-ins I use is these. Here we will introduce some of the plug-ins.

1. vundle

The vundle plug-in must be introduced first. Before using this plug-in, managing Vim plug-ins is a headache, especially when I installed a plug-in but felt it was not easy to use after the trial and deleted the plug-in, it was always very troublesome. After searching, I finally found vundle (Google again ), of course, you must have a git environment in your system using the vundle plug-in. I plan to write a special note about git to record some of the knowledge I have summarized when learning git for version control. In fact, for git, there are a lot of good information on the network.

Installation & Configuration:

1
git clone http://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

Add the following content to vimrc:

12345
set rtp+=~/.vim/bundle/vundle/call vundle#rc()Bundle 'gmarik/vundle'Bundle 'vim-plugin-foo'Bundle 'vim-plugin-bar'

Usage:
First invimrcAdd plug-ins to be installed, such:Bundle 'a.vim', And then:BundleInstallYou can install the plug-in.
InvimrcRemove the added plug-in, and then:BundleCleanYou can delete the corresponding plug-ins. The plug-ins described below are installed in this way.

2. snipmate & supertab-continued

This plug-in is used to automatically complete some content, which is very convenient to use when writing code.
Use: enter some text (defined in the snipmate plug-in) and presstabFor example, main, if, Inc, Inc, for and so on can be completed in the C language code file.

3. doxygentoolkit & the-nerd-commenter

Add comments to the code file.
Configuration:

12345678910
let g:DoxygenToolkit_authorName="Vortex - txc DOT yang AT gmail DOT com"let g:DoxygenToolkit_briefTag_funcName="yes"let s:licenseTag = "Copyleft(C)\<enter>"let s:licenseTag = s:licenseTag . "For free\<enter>"let g:DoxygenToolkit_licenseTag = s:licenseTaglet g:doxygen_enhanced_color=1map <leader>da :DoxAuthor<CR>map <leader>df :Dox<CR>map <leader>db :DoxLic<CR>map <leader>dc a /*  */<LEFT><LEFT><LEFT>

Usage:
Above the function name,dfAdd function header comments for Function Modification
,daYou can add a file header and modify the information in the preceding configuration file.
,ccComment the current row
,csComment on the code area in a more sexy way
,cuUncomment
,cASwitch between different annotation styles

4. Repeat. Vim & surround. Vim

This group of plug-ins is used to repeat some operations..Command enhanced version. For specific use, you can view the respective plug-in documentation, there are very detailed examples.

5. vim-Powerline

You can install the customized plug-in for the status bar.

6. Transpose-Words

Emacs-likeM-tPlug-ins exchange words in that way.
Usage: for examplea = bSuch textM-tYou can change itb = a.

The above configurations and plug-ins are a bit of knowledge I have summarized during the use of VIM. It is recorded here for reference when it is easy to forget.

References:
Vim entry-level and advanced

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.