[Vim] practical command line

Source: Internet
Author: User
Tags perl script
Highlight keywords in VIM
  • syntax enableHighlighted keywords: usually followed bysyntax onEnable syntax highlighting
  • syntax clearClear keyword highlighting

When securecrt is not in xterminal mode, try it and it does not work. If it is in xterminal mode, it will work.

Search in VIM
  • /Followed by the string to be searched: search from the current cursor position up
  • ?Followed by the string to be searched: searches down from the current cursor position
  • nFind Next
  • NFind the previous
Clear and redraw Screen
  • [Ctrl+l]Key combination
Vim shortcut key Customization
  • For keyboard symbols, you can use: Help key-notation to view help
Custom editing Environment

The SET command is used to customize the editing environment.

  • set showmodeWhen VI is in input mode, a prompt is displayed.
  • set smInstant display of paired) or}
  • set ts=nSet the tab to n (the default value is 8)
  • set aiThe indention position of the previous line in the next line begins.
  • set numberDisplay row numbers in the editor
  • set nonuUndisplay the row number in the editor
  • set hlsearchHighlight the searched string
  • set incsearchThe matching point is displayed immediately after the input string is entered.
  • set rulerRuler, used to display the row number and column number at the cursor position, separated by commas. Each window has its own ruler. If the window has status lines, the ruler is displayed there. Otherwise, it is displayed on the last line of the screen.
  • set cindent: Cindent controls the number of indentations through the value of the shiftwidth option, instead of the value of tabstops. The default value of shiftwidth is 8.
  • set shiftwidth: Change the shiftwidth value.
  • set helplang=cn: If the Vim Help document cannot display Chinese characters, add this sentence.
  • set encoding=utf-8:
  • colo murphy: Sometimes when you use Vim to open a file, the color displayed is not suitable for reading. You can use this command to change the color to the black background.

I used the above method in gvim to set it, but it seems that it does not work. I don't know if it is correct or why

Move cursor
  • hMove cursor left
  • L (or space key)Move cursor right
  • K (or [Ctrl + P])Move cursor up
  • J (or [Ctrl + N])Move cursor down
  • [Ctrl+f]Flip forward
  • [Ctrl+b]Flip back
  • [Ctrl+d]Half page forward
  • [Ctrl+u]Flip back half page
  • 1GJump to the beginning of the file
  • 40GJump to row 40th
  • GJump to the end of the file
  • [Ctrl+g]Display the current row number and percentage of Files
  • Ctrl +EScroll down
  • Ctrl +YScroll up
Move the cursor within a row

Commands B, E, and W serve the same purpose as commands B, E, and W, but the latter ignores punctuation marks.

  • bMove backward to the beginning of a word
  • eMove forward to the end of a word
  • wMove forward to the beginning of a word
  • 0Or | move to the beginning of the line
  • 30|Move to column 30th
  • ^First character of the first word to be moved to the row
  • $Move to end of line
  • : numJump to the specified row number
Insert text
  • iInsert characters on the left of the cursor
  • 20i-[Esc]Insert 20 characters
  • IInsert characters at the beginning of a row
  • aAdd text to the right of the cursor
  • AAdd text at the end of a row
  • oInsert an empty row under the current row
  • OInsert an empty row to the current row.
Select text
  • ve: Select the entire word
  • yw: Copy the word where the cursor is located
  • 5yl: Copy 5 Characters of input
Delete and move text
  • xDelete the character of the cursor
  • 6xDelete the characters at the cursor position and the five characters on the right
  • XDelete the previous character
  • ddDelete current row
  • 4ddDelete four rows
  • dwDelete a word
  • d0Delete to the beginning of a row
  • d$Delete to end of line
  • dDelete a region (only for VIM)
  • pPaste the cut text below (whole line) or right side (partial line)
  • PPaste the cut text to the top (whole line) or left (partial line)
  • addPaste the current row into buffer
  • apPaste the content in buffer A to the current position
  • JMerge the current row into the next row
  • xpChange the location of two characters (in fact, it is to delete one character and then paste another character)
Some shortcut keys that are particularly useful to programmers
  • [[= Move to the beginning of the previous line{It is equivalent?^{: Actually, it is to find the previous function.
  • ]]= Move to the beginning of the next row{It is equivalent/^{: Actually, it is to find the previous function.
  • []= Move to the beginning of the previous line}It is equivalent?^}: It is not very convenient to use
  • ][= Move to the beginning of the next row}It is equivalent?^}: It is not very convenient to use
  • f= To the previous empty line
  • g= To the next blank line
  • gd= To the definition of the current local variable (the current meaning is the word on which the cursor stays).: after use, all words matching the word where the current cursor is located are highlighted.
  • gD: Jump to the variable definition
  • *= To the next word that is the same as the current word
  • #= To the previous word that is the same as the current word
  • ‘‘= Line to the last cursor stop
  • %: Allows the cursor to move to the matching brackets: This is very useful.
  • >Increase indentation,x>The indentation of the following X rows is increased.
  • <Reduce indentation,x<Reduce the indentation of the following X rows
  • syntax enable+syntax onEnable syntax highlighting
Modify text
  • rchReplace the character at the cursor position with CH
  • RReplace text on the right of the cursor
  • sReplace the character of the cursor with any character
  • SReplace the entire line of content
  • cwModify a word
  • cModify text in a region (Vim)
  • ~Change the case sensitivity of letters in the scanned or area.
  • !tr‘[a-z]‘‘[A-Z]‘Convert letters in the region to uppercase (Vim)
  • !tr‘[A-Z]‘‘[a-z]‘Convert the letters in the region to lowercase (Vim)
Vim tab Problems

Vim has added the multi-tag switching function since vim7, which is equivalent to multiple windows. Although the previous version also has multiple file editing functions, it is not as convenient as it is.

  • :tabnew[++ Opt option] [+ cmd] File: Create a new tab for the specified file
  • :tabeOpen the specified file in a new tab
  • :tabcClose current tab
  • :taboClose all other tabs
  • :tabsView All open tabs
  • :tabpPrevious
  • :tabnNext

In standard mode:

  • gt,gTYou can switch between tabs directly.
  • :help table: For detailed usage, see the help documentation
Vim compares two files
  • vim -d file1 file2Directly open two files for comparison
  • vim file1Open the first file, and then usevert diffsplit file2Open the second file for comparison;vertVertical Split Window. The default value is horizontal split window.
  • Directly compare the differences between the two files: UsediffthisInput in two windows respectively;
  • [c,]cLocate differences: Jump to the first and last differences;
  • ctrl-w w: Window jump: jump to the next window
  • ctrl-w h/l: Jump to the left (right) window
  • ctrl-w j/kJump to the upper (lower) window
  • dpMerge documents: merge the current document content of the difference to another document (diff put)
  • doCopy the content of another document from the difference point to the current document.
  • zoTemporarily expand the same collapsed content
  • zcFold again
Copy text
  • yyCopy current row
  • 6yyCopy six rows
  • ywCopy a word
  • yCopy region (Vim)
  • pPaste the copied text to the right or below
  • PPaste the copied text to the left or above
  • "ayyCopy the current row to buffer.
  • "apRestore content in buffer
Start Editor
  • vi +100 fooOpen File Foo and move the cursor to line 3
  • vi +/pat fooOpen File Foo and place the cursor in the row that Pat appears for the first time.
  • vi +fooOpen File Foo and place the cursor at the end of the file
  • vi foo1 foo2... Open the file foo1, use N to switch to foo2, etc.
  • vi -r fooRestore contents in the swap file to the buffer zone
  • vi -R fooOpen the foo file in read-only mode
Save and exit
  • :wSave the file and continue editing
  • :w barSame as "Save as..." in Microsoft Windows ......"
  • :w! barSame as above, but overwrite the existing file bar
  • :n1,n2w fooSave the line from N1 to N2 to the file foo
  • :n1,n2w>>fooAdd the line between N1 and N2 to the end of the foo File
  • :.w fooSave the current row to the file foo
  • :$w fooSave the last row to the file foo
  • :xSave and exit the editor
  • :wqSame as above
  • :qExit the editor if the file content does not change.
  • :q!Discard modification and exit Editor
Directory browsing in VIM
  • :E: Open the folder in the default Vim path.
  • : Go to the parent directory
  • D: Delete an object (uppercase)
  • R: Change the file name (uppercase)
  • s: Sort files in lower case)
  • x: Execution File
Multi-file editing

If the current row is not saved (and autowrite is not set), The: E,: N, and: rew commands cannot be used. Command! Makes the security feature invalid.

  • :e fooStop editing the current file and start editing the foo file.
  • :e! fooSame as above, but the modification to the current file is canceled
  • :e!Load the latest saved content of the current file
  • [Ctrl+^]Return to the recently edited File
  • :nEdit the next file (when you use VI to edit multiple files at the same time)
  • :set autowrite(aw)Automatically save the current file before switching to the next file with: N
  • :rewThe first file returned to the file list (when multiple files are edited simultaneously using VI)
  • :r fooInsert the content of the foo file below the current row
Multi-Window (Vim)
  • :sqSplit the current window into two
  • :newOpen a new blank window
  • :[Ctrl+w][Ctrl+w]Switch Ctrl + W + r between windows to move the window to the right Ctrl + W + L to move the window to the left
  • :onChange the current window to a unique window.
  • :qExit current window
  • :qaExit all windows
  • :xaSave and exit all windows
  • [Ctrl]+Increase window size
  • [Ctrl]-Reduce window size
Search and duplicate

The search mode pat can be a string or a regular expression. VI uses a set of dedicated character sequences to perform searches within a row.

  • /patForward search pat
  • ?patSearch for Pat
  • /printfSearch for printf
  • ?[0-0]$Search for numbers at the end of a row
  • /^#Search for the beginning of a row #
  • nRepeat the search in the last search direction
  • NRepeat the search in the opposite direction of the previous search
  • :set wrapscan(ws)Continue searching until the end of the file is reached
  • :set ignorecase(ic)Case Insensitive during search
  • :set magicMeaning of retaining regular expression characters
  • fcSearch for the forward character C in the current row
  • FcSearch for character C in the current row
  • ;Repeat the previous search in the current row
  • ,Repeat the previous reverse search in the current row
  • *: Look down at the word where the cursor is located, similar/
  • #: Look up the word where the cursor is located, similar?
Replace

The source mode S1 can also be a regular expression. When the Champions League address is used, "." indicates the current row, "$" indicates the last row, and "1, $" indicates the entire file.

  • :.s/s1/s2Replace S1 that appears for the first time in the current row with S2.
  • :1,$s/s1/s2Replace S1 in the entire file with S2.
  • :1,$s/echo/printf/gReplace all the echo with printf.
  • :1,$s/s1/s2/gcReplace all S1 WITH S2 in interactive mode
  • :1,$s/#//gDelete all "#" in the file
  • :3,10s/^/#/gInsert "#" at the beginning of Line 3 to 10
  • :$s/$/;/Insert ";" at the end of the last line
  • :1,$s/s1/s2/gReplace all S1 WITH S2
  • :1,$s/HEAD/<&>/gReplace all headers
Logos and bookmarks
  • maSet a tag
  • ‘aMove to tag
  • "Switch between the current flag and the previous flag position
Redo and restore
  • .Repeat the previous command
  • uCancel the last edit command (cancel all previous commands in VIM)
  • [Ctrl+r]Redo the last canceled Operation (Vim only)
  • UCancel all operations on the current row
  • "4pRestore 4th most recent delete operations from the buffer zone
Abbreviation
  • :ab name stgAbbreviated name as STG
  • :unab stgDelete STG abbreviations
  • :abList all abbreviations
Ing key
  • :map key commandsMap key to commands
  • :map! key commandsMap keys to commands in input mode
  • :unmap keyCancel key ing in command mode
  • :unmap! keyCancel key ing in input mode
  • :mapDisplay All mappings in command mode
  • :map!Show all mappings in input mode
Interface commands with Unix
  • :!cmdRun the Unix cmd command
  • :!%Execute the current file as a shell or Perl script
  • :r !cmdInsert the result of the CMD command below the current line
  • :r !head -n 3 fooInsert the first three lines of the foo file to the current row.
  • shTemporarily exit the Unix shell Environment
  • [Ctrl+z]Pause editing (use the FG command to return to the VI editor)
  • :!cc%Compile the C program being edited
  • :!javac %Compile the Java program being edited
Others
  • To change the indentation of some text in the file: In command mode, press V to select multiple lines, and then press SHIFT +> (<) to change the indentation.
  • You can also press SHIFT +> (<) to change the indent (3 times)
Plugin installation
  • Common plug-ins*.vimCopy ~ /. Vim/plugin.
  • *.vmbPlug-in: Use Vim to open the plug-in, and then use the command:so %Install
  • *.vbaPlug-in: first use Vim to open the plug-in, and then useUseVimBall ~/.vimInstall
Vim folding
  • Set the foldmethod: You can use the 'foldmethod' option to set the foldmethod: Set OFDM =*
  • There are 6 Ways to select the fold:
    • Manual manual definition folding
    • More indent indicates more fold
    • Expr uses expressions to define collapse
    • Syntax uses syntax highlighting to define collapse
    • Diff folds Unchanged text
    • Marker folds the mark in the text

Note that each folding method is incompatible. If expr is not used and marker is used, the indent and marker modes are used in turn.
When using this function, use the: Set OFDM = marker command to set it to the marker foldmethod ).
To make the folding take effect every time Vim is opened, add the settings in the. vimrc file, such as adding: Set OFDM = syntax, just like adding other initialization settings.

  • Fold command
    After the folding method is selected, we can implement the folding we need for some code. Because I use more indent and marker, their usage is used as an example:
    If the indent method is used, VIM automatically folds the middle part of the braces, and we can directly use these ready-made folding results.
    In a foldable place (in the middle of braces ):
    ZC folding
    ZC folds all nested folding points in the range
    Zo unfold and fold
    Zo expands all nested folding points in the range
    [Z to the start of the current fold.
    ] Z to the end of the currently opened fold.
    ZJ moves down. To the start of the next fold. The closed folding is also included.
    ZK moves up to the end of the previous fold. The closed folding is also included.
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.