Vim Usage Summary

Source: Internet
Author: User
Tags eol prototype definition tag name

Vim use command

Auto-complete operation

    • Ctrl + N/ctrl + P Auto complement string
    • CTRL +x CTRL +F Auto-complete file name

"Multi-file operation"
To open multiple files:

    • Vim file1 file2 ... filen open all the files you want to open in the same window
    • Vim-o file1 file2 ... filen open an edit session, split the window horizontally, a file occupies a window
    • Vim-o file1 file2 ... filen open an edit session, split the window vertically, a file occupies a window
    • Vim-o5 file1 file2 will be allocated horizontally 5 identical Windows, 3 of which are idle

File switching:

    • : E file Loads the display file in the current window
    • : LS Displays a list of all files opened, numbered
    • : b1~n Switch to nth file
    • : Bn Next file
    • : BP previous file
    • Switching between ctrl+6 two files

"Multi-Window operation"
Cutting window Operation:

    • : SP (lit) Horizontal cutting window
    • : VSP (list) Vertical cutting window
    • : QA (LL) Closes all windows

The default newly created window displays the same files, and you can display different files by using the following command:
: [n] Split (vsplit) [++opt] [+cmd] [file]

Multi-Window Switching:

Press CTRL + W, and then H, J, K, L, respectively, to move the window to the left, bottom, top and right, or use the up, down, left, and right buttons

    • Ctrl + W + H: Move the window to the left
    • Ctrl + W + j: Move the Window down
    • Ctrl + W + j: Move the Window up
    • Ctrl + W + L: Move the window to the right

    • Ctrl + W + w: This command loops through all the Windows

    • Ctrl + W + t: Move to the top left corner of the window
    • Ctrl + W + B: Move to the bottom right corner of the window
    • Ctrl + W + P: Move to the previous Access window
Tips for use

"New file Add header automatically"

"New Python file automatically adds header information let G:python_author = ' cold ' # name let G:python_email = ' [email protected] ' # mailbox Functio n Headerpython () normal 1GPagerSetline (".","#!/usr/bin/env python") Normal OPagerSetline (".","#-*-coding:utf-8-*-") Normal OPagerSetline (".","#") Normal OPagerSetline (".","# Author:". g:python_author) Normal OPagerSetline (".","# e-mail:". g:python_email) Normal OPagerSetline (".","# Create Date:". Strftime ("%y-%m-%d%h:%m:%s")) Normal OPagerSetline (".","# File Name:". Expand ("%")) Normal OPagerSetline (".","# Desc:") Let Lnum = line (".") Normal OPagerSetline (".","#") Normal OPager cursor(Lnum, Len (Getline (lnum))) Normal A"Normal G"Normal O"Normal oendfautocmd bufnewfile *.py call Headerpython ()

Multi-row multi-column operation

Press V to enter visual state, select multiple lines, indent or indent with > or <

Automatic indentation is usually used according to the language characteristics: Use = = in the command state for the current line (double-click = two times), or n== (n is natural number) for multiple lines to automatically indent the following n rows from the current line. You can try to indent the code arbitrarily and then use the n== layout, which is equivalent to the normal IDE's code format. Use Gg=g to compose the entire code.

: 20,30 s/^/#/g 20th to 30th line with # comment out.
: 20,30 s/^#//g Uncomment

: 4,10 s/^[^i]+//Remove blank characters from beginning of line

: Co 12 "Copy the current line to line 12.
You can see that the VIM command is for the current line, with a range above it for multiple lines.
Use. Represents the current row,% represents the full text, and $ represents the end

:., s/^/#/g

Vim configuration file Usage

vim --versionView VIM-supported features and configuration file directories

SetNocompatible"Turn off VI compatibility Mode"ColorScheme Morning"Set color scheme/usr/share/vim/vim74/colorsset number"Show line NumbersSetCursorline"Highlight current row set ruler"Open the status bar rulerSetShiftwidth=4 "Set << and >> command width 4set softtabstop=4 when moving"Makes it possible to delete the bar when you press BACKSPACE4A spaceSettabstop=4 "Set tab length to 4set expandtab"Replace the TAB key with a space": Set ts=4":SetNoexpandtab":%retab!":SetThe list displays tabs and displays the following settings"Set listchars=tab:>-,trail:-" SetListchars=tab:\|\, trail:.,extends:>,precedes:<,eol:$SetNobackup"Do not back up set Autochdir when overwriting files"Automatically switches the current directory to the directory where the current file is located syntax on "Automatic syntax highlighting filetype plugin indent on"Turn on pluginsSetBackupcopy=yes"The behavior when setting up a backup is to overwrite set ignorecase smartcase"Ignores case when searching, but remains case sensitive when one or more uppercase letters are availableSetNowrapscan"Prohibit re-searching of set incsearch on both ends of a file search"Search results are displayed when you enter search contentSetHlsearch"Highlight found text set noerrorbells when searching"Turn off error message ringSetNovisualbell"Turn off using visual bells instead of call set t_vb="Terminal code for blank error ringtone"Set Showmatch"When inserting parentheses, briefly jump to matching corresponding parentheses"Set matchtime=2"Briefly jump to the time of matching parenthesesSetMagic"Set magic set hidden"Allows the buffer to be toggled when there are unsaved modifications, which are saved by vimSetGuioptions-=t"Hide Toolbar set guioptions-=m"Hide the menu barSetSmartindent"Use Smart Auto indent set Backspace=indent,eol,start when new row is turned on"Not set in insert state cannot be used backspace andDeleteKey to remove the carriage return characterSetcmdheight=1 "Set the line number of the command line to 1set laststatus=2"Displays the status bar (the default value is1, the status bar cannot be displayed)SetStatusline=\%<%f[%1*%m%*%n%r%h]%=\%y\%0(%{&fileformat}\%{&encoding}\%c:%l/%l%) \"Set the information displayed in the status line set Nofoldenable"Start foldingSetFoldmethod=syntax"Set syntax collapse set foldcolumn=0"Sets the width of the collapsed area setlocal foldlevel=1 "Set the number of collapsed layers to" SetFoldclose= All "set to auto close collapse"Nnoremap <Space> @= (foldclosed (Line ('. ')) <0) ?' Zc ':' zo ') <CR>"Use the SPACEBAR to switch folding
Use of Vim plugin Ctags

Installation

    • Installation:yum install -y ctags

"Generate tags File"

    • Ctags-r Generate tags files recursively for all code files in the current directory and subdirectories
    • Ctags . C . h Generate tags files for some source code

In order to make the field completion valid, in the generation of tags need some additional parameters, the recommended C + + parameters are mainly:
Ctags-r–c++-kinds=+px–fields=+ias–extra=+q
which
Option C++-kinds is used to specify the tags record type for the C + + language, –c-kinds is used to specify the C language, and the common format is –{language}-kinds
Option Fileds to specify the extended field fields for each marker
The extra option is used to add additional entries: F indicates that an entry is added for each file, and Q adds an entry for each class

"How to use"

When the vim opens the source code, specifies the tags file, is normal use, usually manually specified, at the VIM command line input:
:set tags=./tags(tags file under current path)
To refer to multiple tags files in different directories, you can separate them with commas

Or, set ~/.VIMRC, add a row, you do not have to manually set the tags path:
Set Tags=~/path/tags

To add a system function or tag tag for a global variable, you need to do:
Ctags-i __throw–file-scope=yes–langmap=c:+.h–languages=c,c++–links=yes–c-kinds=+p–fields=+s-r-F ~/.vim/systags/u Sr/include/usr/local/include
and added in ~/.VIMRC (also by the manual addition described above):
Set Tags+=~/.vim/systags
In this way, you can enjoy the system library function name completion, prototype preview and other functions.

If you frequently check the code in different projects, you can add it in ~/.VIMRC:
Set tags=tags;
Set Autochdir

Set the tags file, the most commonly used shortcut keys when locating variable/function definitions are:
Ctrl +]
Jump to the definition of a variable or function, or use the command
: Ta Name
and using shortcut key combinations
Ctrl + o/t
Returns to the position before the jump.

In addition, Ctags does not generate indexes for local variables, but it is also fairly fast to use the GD key combination (Quick Find and locate the word where the cursor is located).

$ vim-t Myadd
When you open a file with Vim, the add parameter-T funcname automatically opens the file that defines the function and navigates to the first line of the definition, above which is to find the Myadd defined file open and place the cursor at the first line of the definition.

: Tags
The Find/Jump process is listed (the list of tags passed)

Also, attach the other useful shortcuts in the VIM environment:
* Navigate to the next occurrence of the word that the current light cursor refers to
# Navigates to the last occurrence of the word indicated by the current light cursor
n position to jump to the next occurrence of a word that has been marked
Shift+n to jump to the last occurrence of a word that has been marked

For more detailed ctags usage, use in vim
: Help Tags

TagList

function
A source code browser (supports/C + +, Java, Perl, Python, TCL, sql, PHP, etc): Provides a structured browse function of the source code, can be defined in the source code classes, functions, variables, such as tree structure display, hierarchical relationship can be a And to quickly locate and view it.

"Download and install"
The TagList plugin is in the form of a vim script, so just download it and put it in the appropriate directory. TagList is based on ctags, so make sure you have the ctags installed before using TagList.

(Two can choose any one):
Official site http://vim-taglist.sourceforge.net/
VIM Online http://www.vim.org/scripts/script.php?script_id=273

Place the files in the /usr/share/vim/vim74 corresponding folder

Configure the TagList option in the Vim configuration file:

"If Ctags is not under path path, configure Ctags's path if mysys () = ="Windows"Let Tlist_ctags_cmd = ' Ctags ' ElseIf mysys () = ="Linux"Let Tlist_ctags_cmd = '/usr/bin/ctags ' endiflet tlist_show_one_file = 1"Not displaying multiple files at the same timeTagTo display only the current file's LetTlist_exit_onlywindow= 1      "If the TagList window is the last window, exit Vimlet Tlist_use_right_window = 1"Display the TagList window in the right window LetTlist_sort_type= "Name"       "TagList is sorted by tag name, and by default it is sorted by the order in which the tag appears in the file let Tlist_use_singleclick = 1"Settings clickTagJump to the definition and do double-click LetTlist_auto_open=1              "Start vim automatically open taglist window let tlist_close_on_select=1"In the selection ofTagAutomatically close TagList window after LetTlist_file_fold_auto_close=1   "When you display tags in multiple files at the same time, you can have taglist display only the current file tag, and the tags of the other files are folded up." Let Tlist_gainfocus_on_toggleopen=1 "When you open the TagList window using: tlisttoggle, the input focus is in the TagList window LetTlist_process_file_always=1    "taglist always resolves the tag in the file, regardless of whether the TagList window has not opened let Tlist_use_horiz_window = 1"Setting taglist window Landscape display"Tlist_winheight and Tlist_winwidth can set the height and width of the taglist window

Shortcut keys that you can use in the TagList window:

shortcut Keys function Description
CR Jump to the position defined by tag under the cursor, and double-click the tag function with the mouse
O Show cursor under tag in a newly opened window
Space Display the prototype definition of tag under cursor
U Update the tag in the TagList window
S Change the sort order, toggle between sorting by name and sorting by appearance
X TagList window zooms in and out for longer tag viewing
+ Open a folding, with Zo
- Fold the tag together with ZC
* Open all the folds, with the ZR
= Fold all the tags together with ZM
[[ Skip to previous file
]] Skip to the latter file
Q Close the TagList window
F1 Show Help
Cscope
    • Installation:yum install -y cscope
Profile syntax Introduction to shortcut key mappings

"Vim Mode"
There are many modes of vim, but it is generally mentioned in the following ways:

    • Normal Mode
      That is, the general Normal mode, the default into Vim, in this mode.

    • Visual Mode
      General translation visual mode, in which some characters, rows, and multiple columns are selected.
      In normal mode, you can press V to enter.

    • Insert Mode
      Insert mode, in fact, refers to the state of editing input. In normal mode, you can press I to enter.

    • Select Mode
      In the gvim under the commonly used mode, can be called as a choice mode bar. When you drag the area with the mouse, you enter the selection mode.
      And the visual mode is different, in this mode, after the selection of the highlighted area, tap any button to directly enter and replace the selected text.
      The same effect as the editor selected under Windows. In normal mode, you can enter by GH.

    • Command-line/ex Mode
      It's called command-line mode and ex-mode. The two are slightly different, normal mode by the colon (:) into command-line mode, you can enter a variety of commands,
      Use the various powerful features of VIM. In normal mode, pressing Q into the EX mode is actually a multi-line command-line mode.

"Map Basic Concept"

Combination of commands
Like other commands under Vim, the name of the command is often made up of several paragraphs. The prefix is used as the modifier of the command itself to fine-tune the effect of the command.
For map, there may be several prefixes

    • Nore
      Represents non-recursive, see the following introduction
    • N
      Indicates effective in normal mode
    • V
      Indicates effective in visual mode
    • I
      Indicates effective in insert mode
    • C
      Indicates effective in command-line mode

"Accelerator Map Instance"

Closing parenthesis Auto-complete:

    • inoremap ( ()<ESC>i
    • inoremap [ []<ESC>i
    • inoremap { {}<ESC>i<CR><ESC><S-o>
    • inoremap < <><ESC>i
    • inoremap " ""<ESC>i
    • inoremap ‘ ‘‘<ESC>i
Reference
    • Vim's complement mode accelerator, easy to play all 15 auto-completion modes
    • http://cscope.sourceforge.net/
    • http://ctags.sourceforge.net/
    • Installation and use of Vim plugin Ctags
    • Installation and use of Vim plugin TagList
    • Powerful vim configuration file to make programming more casual

Vim Usage Summary

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.