Vim Multi-window, common command set

Source: Internet
Author: User
Tags function definition function prototype save file

1. Open multiple Windows

Commands to open multiple windows: Horizontal cutting window: new+ window name (after saving is file name)  :split+ window name, or abbreviated as: sp+ window name portrait cutting window Name: vsplit+ window name, also can be abbreviated as: vsp+ window name       vim-o file1 file2 ...   #水平打开多窗口,     vim-o file1 file2 ...  # Open multiple windows vertically,     vim-d file1 file2 ...   #垂直打开多窗口, and compare     vimdiff file1 File2.   #等同于上一句  2, close multiple windows can be used: q!, can also be used: Close, the last window cannot be closed with close. Using close only temporarily closes the window, its contents are still in the cache, and only use q!, w!, or X to actually exit.   Close window      q   or close    #关闭当前窗口      only           #保留当前窗口, close all other Windows      qall (QA)           #退出所有窗口      wall            #保存所有窗口3, window switch: ctrl+w+j/k, through the j/k can be switched up or down, or: ctrl+w plus up and down keys, you can also quickly double-click CTRL + W Switch the window in turn. 4, Window resizing vertical adjustment: Ctrl+w + vertical expansion (increase in number of lines): ctrl+w-vertical reduction (number of lines reduced): res (ize) Num   For example:: Res 5, display row number adjusted to 5 rows: res (ize) +nuM increase the current window height num line: res (ize)-num reduce the current window height by num row lateral adjustment: Vertical res (ize) num Specifies that the current window is num column: Vertical res (ize) +num adds the NUM column to the current window: Vertical res (ize)-num reduce the current Window num column 5, rename the window: F File6, vi open multi-file vi a B c:n jump to the next file, you can also directly specify the file to jump, such as: N C, you can jump directly to the C file: e# back to the file you just edited 7, File browsing: Ex Open the Directory browser, you can browse all the files in the current directory, and you can choose: Sex horizontally split the current window and open the Directory browser in a window: LS shows the current buffer condition 8, VI and Shell switch: Shell You can switch to the shell command line without shutting down VI: Exit returns to Vi9 from the shell. Multi-label directly at the time of editing input: Vim-p to edit the file name, such as Vim-p * is to edit the current directory of all the files to switch between multiple tags to the right to switch the GT, to the left to switch with the GT in the edit when you want to add a label: Tabnew FILENAME:TABC        Close the current Tab:tabo        close all other tab:tabs         View all open TAB:TABP       previous: Tabn        After a

1. Vim Basic usage: mode, cursor move, find replace, copy paste Delete

1.1 Help

: Help

: Help command

1.2 Mode switching
Command mode ESC, CTRL-C, with cursor movement available ALT-H,ALT-J,ALT-K,ALT-L
Edit mode I edit in the current position, a inserted after the current position, I inserted at the beginning of the line, a at the end of the insertion, o add a new row
Visual Mode V-block mode, V-block mode, ctrl-v column-block mode

The following operations and shortcut keys are basically in command mode, the edit Mode button can only enter text.

1.3 Input Mode
Edit directly on the keyboard as I
: Command: Set nu
Execute shell command:!command:!pwd


1.4 Undo, Redo
Undo last edit Operation U, u
Undo all unsaved edits and reload the file: e!

Redo the last undone edit Operation Ctrl-r

1.5 Save, exit

Save File: W

Close file: Q

Save and Close file: Wq

Do not save forcibly closed files: q!

Close all Files exit: QA


1.6 Moving the screen

Next Page Ctrl-f

Page UP Ctrl-b

Ctrl-e the next turn.

Ctrl-y up the line

1.7 Cursor Positioning shortcut keys
To the specified line: line number, line number g
To file header 1G
To end of file G
Lower left upper right h,j,k,l
Next word W, previous word b
To the beginning ^
To end of line $
End of line Insert A, add empty line O

1.8 Positioning the cursor in a function
[[Go to Previous ' {' in first column]
]] to go to the next "{" In the first column
{Go to previous empty row
} go to the next empty line

1.9 Finding the current file

N,n after finding the key, n jumps to the next position, N jumps to the previous position

*, # The word where the cursor is currently key as a keyword, exact match lookup, equivalent to/\<key\>

g*, g# the word key, which is the current cursor, ignores case lookup, which is equivalent to/key

/key find key backwards from the current cursor position

? Key looks forward from the current cursor position to key

/\<key\>,?\<key\> Exact Match find key

1.10 Batch Replacement:%s/the string to be replaced/new string/g

1.11 Copy-paste Delete
Copy line yy copy n rows nyy
Paste Line P
Delete Row dd Delete n row ndd
Delete Word DW copy word yw

Visual Block Selection replication:
Enter visualization mode v,v,ctrl-v
In visual mode, the direction key selection block
Press Y to copy the selected block

2. Configuration: Display and edit styles, profiles

2.1 Displaying and editing styles
The status line shows the file name set Statusline+=%f,set laststatus=2
Display line number: Set Nu hidden line number: Set Nonu
Auto indent: Set autoindent
C-style indentation: Set cindent
Display special symbols such as break characters: set list

2.2 Configuration Files
Vim comes with a configuration file/ETC/VIMRC
User-defined profile ~/.VIMRC, create it yourself. Write your own configuration here.
Syntax highlighting syntax on

3. Vim usage: Multi-line comment, multi-file, multi-pane edit, save session


3.1 Multi-line comments
Multi-line Comment key operation:
Note: Ctrl-v enters column edit mode, moves the cursor down or up, marks the beginning of the line that needs comment, then presses the uppercase I, then inserts the comment symbol such as "#", and then presses ESC, it is all commented.
Delete: Ctrl-v enters column edit mode, moves the cursor down or up, selects the Notes section, and press D to delete the annotation symbol.

Multi-line annotations use the Replace command:
:%s/^/\/\//g to add//number comments at the beginning of the entire content
: 2,50s/^/\/\//g Add//Number comment at the beginning of 2~50
In turn, substitution is both a delete operation.

3.2 Editing Multiple files
Open multiple Files $ vim filename1 filename2
Switch file:p rev,:next,:p rev!,:next!,first,last, just switch it over, it's in the buffer.
Show list of open files: args

Open a new buffer to see other files: E filename
Toggle the last two files (buffers): e# or ctrl-^
List of files showing buffers: buffers
Close one of the (current) files: BD,:BD 1,:BD 2, serial number by: buffers displayed
Switch to a file: B 1,:b 2, serial number by: buffers display
Switch to previous file: Bp,:bprev
Switch to the next file: Bn,:bnext

3.3 Multi-window editing

Split window
Split horizontally
: SP//The currently edited file splits a window and opens
: Spfilename//split a window out, open file filename
: Split
: New

Vertical split
: Vsplit
: vnew
Toggle Window
Ctrl-w up or down key
Ctrl-w H,j,k,l//h left, J down, K, L Right//ctrl Press, press W release, CTRL release, press H

Move window
Ctrl-w h,j,k,l//h left, J, K, L right//Lock CapsLock can be capitalized

Change window size
Maximizes the current window ctrl-w ctrl--//This is special: CTRL press, press W to release, then press-release, and finally release CTRL
Equal window Ctrl-w =
Resize window ctrl-w + Increase window size one row, ctrl-w-Reduce window size one row

Show Command History window
Q: Or q/, exit: Q

Make the current file occupy the entire window
Ctrl-w o

Close one of the (current) Windows
Ctrl-w c//Actually the file is still in the buffers

Let the file with the buffer come to the split window
Ctrl-w ctrl-^//Keep Ctrl pressed


3.4 Save the environment and state of the directory in which the session

If you have opened a lot of windows in vim, want to keep this environment, and so on the next edit browse and then load.

: Help Mksession View Mksession

: mksession! Saves the current VIM state, which produces a session file Session.vim in the current directory.

$ vim-s VIM will automatically load the session file of the current directory Session.vim before: mksession! The state of keeping is back again.

: mksession! Work.vim Save current VIM status to session file Work.vim

$ vim-s Work.vim Vim download session file Work.vim, before: mksession! The state of Work.vim is back again.

You can also manually download the session in VIM:

: Source Session.vim Loads the state that Session.vim holds.

: Source Work.vim Loads the state that Work.vim holds.

Configure auto Save and download session

After this configuration, the first time in the directory to automatically keep the session into vim, with the command: mksession! Project.vim. Later use directly in the current directory $vim can.

If you want to automatically save and download the session when Vim is turned on and off, you can add it to the profile ~/.VIMRC:

"Auto Save and load session
Let G:
Let G:ORIGPWD=GETCWD ()
If Filereadable (g:autosessionfile)
If ARGC () ==0
Au vimenter * call Enterhandler ()
Au Vimleave * call Leavehandler ()
endif
endif
function! Leavehandler ()
Exec "mks!". G:origpwd. " /". G:autosessionfile
Endfunction
function! Enterhandler ()
EXE "source". G:autosessionfile
Endfunction
Refer to the session auto-save and load in vim


4. Browse Code: Ctags,taglist,nerd_tree,cscope


4.1 Popular VIM Scripts
Http://vim.sourceforge.net under Scripts->browse all

4.2ctags jump To Definition

Ctags is a plugin for generating code index files that supports many languages.

You can use Ctrl] to jump to the definition of a function, or you can use commands to search for the definition of a function, which is very useful.

Installing Ctags
If the Ctags,fedora is not installed under the shell can be installed with # yum-y install Ctags.

Create a tags file
Shell, in the need to create tags files under the directory with $ ctags-r, then the entire directory of functions, macros, variables, classes, structures, etc. are added tags.

Using Ctags
Press to jump Ctrl-],ctrl-t,ctrl-o
In the directory where the tags file is created, the cursor is moved to a called function name in a file that is opened with VIM, and CTRL +] automatically jumps to the definition of the function, CTRL-T returns.

Command Jump: Tags functionname
In a file opened with vim, use the command: Tags functionname. : Ta functionname

Search and jump when open
Vim-t functionname

Multiple matching tags when jumping

: TNEXT,:TPREV,:TN,:TP

4.3Taglist function and Variable list
TagList is the source code browsing plugin for VIM, which can view function list, variable list, etc. in window mode.
The implementation of TagList relies on ctags.

Installing TagList
1. Download the latest version of the TagList package in http://www.vim-taglist.sourceforge.net/screenshots.html.
2. Create the. Vim directory in the personal user directory and extract the contents of the Taglist_**.zip into the directory.
3. Enter vim and enter the command "helptags ~/.vim/doc" in command mode to complete the TagList installation.
4. Enter the command ": Tlist" to check if the installation was successful.


Modifying a configuration file

Let tlist_file_fold_auto_close=1 other files in addition to the current window display files.
Let Tlist_exit_onlywindow=1 if Tlist is the last window, automatically exits vim.

Use: Tlist
After you open Vim, use the command: Tlist to activate the TagList window; press Ctrl-w W to jump back and forth in two windows.

: Tlist

: Tlisttoggle

Tlist shortcut keys
In the Tlist List window, take the function as an example
See Function prototype spaces
See function definition P
Jump to function definition move to letter name at enter
Collapse/expand-, +
Collapse All/Expand All =,*
Change how the list is sorted s
Update list U
Zoom to zoom out window X
Exit Window Q
Up/Next file [[,]]

Configuration: Tlist shortcut keys

The. VIMRC writes:
Nnoremap:tlisttoggle

Tlist Help

Press F1 in the Tlist window

: Help Tlist View Tlist command assistance

: Help ' tlist* ' view Tlist options assistance

: E ~/.vim/taglist.txt directly open the document to see Help

4.4 Nerd_tree Directory tree List

Installing Nerd_tree

1. Download the Nerd_tree package in Vim.sourceforge.net.

2. Extract the Nerd_tree package to. Vim, and you'll see. Vim has more file Doc/nerd_tree.txt,plugin/nerd_tree.vim.

3. Enter Vim, enter: Nerdtree Check if the installation is successful.

Using Nerdtree

After you open vim, use the command: Nerdtree to activate the Nerdtree window; press Ctrl-w W to jump back and forth in two windows.

: Nerdtree

: Nerdtreetoggle

Nerdtree shortcut keys

O, Enter expand/Collapse Directory

P Previous level Directory

/Find Files

Q exit

Help

: E ~/.vim/doc/nerd_tree.txt

Ignore specific file types

In the configuration file, write:

Let Nerdtreeignore = ['. *\.o$ ', '. *\.ko$ ', '. *\.gz$ ') ignores files with the suffix. o,.ko,.gz.

Configuration: Nerdtree shortcut keys

The. VIMRC writes:

Nnoremap:nerdtreetoggle

4.5 Bufexplorer Buffer File list

Install bufexplorer similar to Nerd_tree.

Using Bufexplorer

: Bufexplorer

Configuration: Bufexplorer shortcut keys

The. VIMRC writes:

Nnoremap:bufexplorer

4.6cscope find function called location in entire directory

Cscope can find function definitions throughout the directory, find functions where they are called, and so on.

Installing Cscope

Under Fedora, # yum-y install Cscope

Build the Cscope Database

In the desired directory

$ find. -name ' *.c '-or-name ' *.h '-or-name ' *. S ' | Cscope-rbq

The current directory will produce 3 files cscope.out,cscope.in.out,cscope.po.out.

Loading the Cscope database

Enter VIM in the current directory

: CS Add Cscope.out

Use Cscope to find an entire directory

: CS Help

: CS Find G Timer_interrupt find definition of Timer_interrupt

: CS Find C Timer_interrupt find the place to call Timer_interrupt

: CS Find e run_timer find keywords Run_timer

Find can be abbreviated to F

Multiple match between jumps

: CNEXT,CN

Modifying a configuration file

Set NOCST lets you jump to the defined command: TA and Ctrl] Call Ctags's tag, which helps: Help NOCST.

Set Csto=1 priority to use ctags tag

"Set cscopequickfix=s-,c-,d-,i-,t-,e-: CS Find C when the direct jump instead of the list, see a person likes to set this."
Nnoremap <c-\>:cs Find C =expand ("") cursor is placed on the function, and the shortcut key ctrl-\ jumps to the place where the function is called.

Nnoremap <c-_>:cs Find G =expand ("") cursor on function, shortcut key ctrl-_ jump to function definition or list multiple definitions

5. Compiling and modifying code errors

The Quickfix mode is built into VIM, where the error location information is recorded and used to locate the wrong location.


Compile

Call the makefile file in the current directory to compile: make

Change the command for the build call: Set makeprg=gcc\-g\ test.c\-o\ test, without writing makefile

Quick Locate Code Error

List errors: cl
Next Error: CN
Previous error: CP
Starting from scratch: cnew

Open Error List window: CW

Close Error List window: CCL

6. Call GdB to Debug:vimgdb

Install VIMGDB
      vimgdb need to be patched in the vim version, download vim source, corresponding version of the Vimgdb patch, Then press VIMGDB's documentation to install it.
 
Configure the key mapping
     configuration file   map :run Macros/gdb_mappings.vim
     Press F7, the key map can be switched in debug mode and normal mode.

Use vimgdb
    key map       or: Run Macros/gdb_mappings.vim
     Open gdb Command pane        spaces
    Open gdb Output pane         type gdb in the command pane, press ENTER
    load the executable file         Spaces Open the GDB command pane, file Test,test is the executable file generated after compilation
    set breakpoints     ctrl-b
     Start running     r
    continue     s,ctrl-n,c
     End        f
    exit         q


Variable Watch Window
Create a Variable Watch window: Bel 30vsplit gdb-variables
Select variable name to monitor window v command to select variable, ctrl-p add to Variable watch pane
Enter variable name to watch space Open gdb Command pane, enter Createvar variable name

Displayed in the GDB Output pane, view
Locals variable L,info locals
function Stack W,where
Breakpoint Position B,info Breakpoints
Register Info Registers

Reference: Vi/vim Use advanced: Use GDB debugging in Vim – use Vimgdb


7. Other

7.1 Chinese garbled problem
Install run Zhcon First

Vim Multi-window, common command set

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.