Vim advanced command set ex

Source: Internet
Author: User

Ex is Vim's support for row editor ed. ed is a line-oriented editor. in ancient times, in the age of black and white terminals, there was a popular row-oriented editor Ed, that is, reading files one row at a time, displaying only one row at a time, and then editing this line. therefore, the ex commands in Vim are all supported by the Ed editor. the commands in the stream editor sed used in modern times are extensions of the Ed command. so I have mastered the ex edit command in Vim, and even sed has done it together with a little flexibility.

Vim is a full-screen editor, so it does not show only one row as Ed. For Vim, the Ed command is usually edited by the current row, which is equivalent to the row shown in ed, it is actually the current row of the file.


Command knowledge

All the ex editing commands in VIM start with: (colon ).
The startup mode is to enter the colon in command mode, and then enter the specific command
In the vim status line (usually the bottom line of the screen), the command you entered is displayed, and the command output result is also displayed.
Because it is a line-oriented Editing Command, the basic unit is the line. For example, the copy and cut operations are in the unit of behavior (that is, at least one line ).

Command Format
:[range] cmd args
Miscellaneous commands

These commands are not related to specific text editing, but are also very common.

  • : =-- Print the total number of lines of a file
  • :/Pattern/=-- Print the line number matching pattern
  • : W-- Write the current buffer to a file, that is, save the file.
  • : W!-- Force save. If it is a read-only file, it can be saved.
  • : Q-- Exit the current file, provided that it has been saved. If Vim is not saved, you will be reminded.
  • : Q!-- Force exit, that is, exit whether it has been saved or not, the modifications may be lost.
  • : WQ-- Save and exit.
  • : Wa-- Write all the buffer into the file, that is, save all the files (if you open multiple files ).
  • -- Save and exit the command that programmers should use most. the difference between it and WQ is that if the file is not modified, X will not modify the timestamp of the file, so it does not need to be re-compiled. however, the WQ command will modify the timestamp even if the file is not modified, which will trigger re-compilation.
  • : XA-- Save and exit all files. If multiple files are opened.
  • : W A-New-File-- Save as a-New-File
  • : [Range] WA-New-File-- Save the content of the range specified by [range] as a-New-File
  • : [Range] W> Another-File-- Append the content of the range specified by [range] to another-file.
  • : R FileOr: Read
    File-- Read the content in the file and put it under the current row. The details are to open the file, copy the content, and paste it under the current row.
  • : N r File-- Copy the file to the nth row
  • :/Pattern/R File-- Copy the content in the file to the first line that matches pattern.
  • : EAnother-File-- Read another-file into memory and start editing
  • : E!-- Discard all unsaved changes and return to the State of the last file.
How to specify the range [range]

Many Commands require specifying the range and line number. How can this problem be solved?

0. If no row number is specified, it usually indicates the current row.
1. The specific absolute line number. The line number starts from 1 to Line X, and X is the total line number of the file.

  • : N, m-- Line N of the file to line m, including N and m

For example, 2 and 10 are between 2nd and 10th rows.
If n = 0 or N =-1, n is automatically converted to 1. If M is greater than X, n is converted to X.
Special row number

  • .(Period) -- current row
  • $-- Last line

For example:., $Indicates all rows in the file.

  • %-- All rows are equivalent to: 1, x, and:., $

2. relative to the current row and relative row number

  • +/-N-- + Indicates the nth row from the current direction;-indicates that the current row minus n rows forward
  • + /--- The first line or the last line

For example:-, + D-- Delete the current row. There are three rows in total: the last row and the next row;:-2, + 2 DDelete the current row. The first two rows and the last two rows have five rows in total.
Use search matching to specify rows

  • /Pattern/-- Specifies the next line that matches pattern, that is, the first line that matches pattern after the current row. This can only specify one row
  • /Pattern/,/pattern2/-- The first line containing pattern to the first line containing pattern2.

Note: If the row number after the specified range is smaller than the previous one, VIM will prompt you for the [range] boundary indication. Do you want to swap it.
Specify the current row again

  • N; m-- Regards row N as the current row. The cursor is in the current row by default. This command can quickly specify the current row, saving a lot of effort

For example:

  • : 40; + 50 d-- Delete rows 40th to 50th. This is equivalent to: 40 to 40, and then execute + 50 d.
  • :/Old/; + 10 d-- Delete 10 rows from the row containing the old to the following. This is equivalent to searching the row containing the old:/old/, and then executing: + 10 d
Edit Command Parsing

  • : [Range] d-- Delete. For example: 1, 20 d Delete rows 1st to 20th
  • : [Range] MV target-- Add the moving destination to the end. Cut the part specified by [range] and paste it to the position specified by the target.
  • : [Range] Co/T target-- Copy the specified part of [range] to the target position.
  • : [Range] Y/ya-- Copy [range] To the clipboard
  • : [Range] y Name-- Copy [range] To the clipboard and name it a, so that you can operate multiple copies at the same time.
  • : N Pu-- Paste the content in the clipboard under line N
  • : N Pu Name-- Copy and paste the clipboard named name under line N
Command Line startup command

Is to perform some operations after opening

$ VIM + N file -- open the file and jump to line N $ VIM + file -- open the file and jump to the last line $ VIM +/pattern file -- open the file and jump to the file containing pattern. that line

For example,$ VIM +/"Your Life" File-- Open and jump to the line of "your life"

Edit multiple files

Commands, modes, and buffer will be shared before multiple files and multiple windows. Therefore, if you edit multiple files at the same time, the efficiency will be improved.

1. Run the command line to start multiple files.

$vim a.txt b.txt c.txt

2. When editing a file, you can run the: e command to edit another file.

  • : EAnother-File

3. ARGs -- display the list of all files. The current file is marked as [filename ].

$vim a.txt b.txt c.txt:args  ----->>>>[a.txt] b.txt c.txt:n     ----->>>>a.txt [b.txt] c.txt

4. Switch between files

  • : P [rev]-- Previous file
  • : N [ext]-- Next file
  • : Last-- Last file
Dual-file Mode

1. Startup Mode: When editing a file, use the e command to read and edit another file and the file enters the dual-file mode.
2. Two file stenographer, equivalent to a string macro. The content is the edited two file names.

  • %-- Current file name
  • #-- Previous file

3. Example

$ Vim a.txt: w -- save and modify: E B .txt -- start and edit another file B .txt: E # -- start and edit the previous file, and upload the file a.txt: E # -- start and edit the previous file. This is B .txt: w %. bak -- when the pre-file is B .txt.bak(when the pre-file is B .txt): R # -- when the pre-file is B .txt, open a.txt in the new window: Split #. txt: E! # -- Modify the prefix and edit a.txt

Combined Command

You can use | to combine several commands, and use; To combine shell commands is the same.

For example

  • : 1, 3 d | S/their/They-- Delete rows 1 to 3 and replace their with they in the current row.
Get help

Any command can be used

  • : Help CMD-- To get help documentation
Execute shell commands

  • :! CMD-- Execute the shell command and jump to terminal to display the command result.

Most shell commands can be executed internally.

  • : Cmd-- Execute the command and the operation result is displayed in the status line.

For example: CD-- Switch the current directory;: LsTraverse directory content;: Pwd-- View the current working directory
Temporary Access to Shell

Sometimes you want to perform some operations on the shell terminal and then return to edit the file. You can do this:

-- Save and exit to the terminal; do all stuff you want $ Vim file -- open and edit the file again

This is a very common scenario, so there is a more convenient and convenient method, you can use shell or sh commands to do the same thing

: Sh -- return to terminal $ do you stuff $ exit -- when the terminal is finished, use the exit command to exit the terminal and return to the editor.

The sh command is very practical. For example, after compiling and running the debugging routine, you can use the sh command to exit shell for testing and debugging, and then exit to edit.
Quickfix does not require compilation by editing and compiling. How can this problem be solved?

The simplest method is to directly run the GCC command:

  • :! Gcc-O list. C-STD = c99-wall-LM

This is absolutely feasible, but it is very troublesome.
You can use the make command in vim.

  • : Make list-- Compile list. c

However, this will only use the default hiding rule of make, which is quite weak. For example, if no additional options are available, it is not possible to specify a rule such as wall or STD.
Of course, you can write a makefile and then run make directly happily.

But what if no makefile exists, especially for some single files. the compilation targets and source code of each file are different. In this case, you can write a simple makefile without specifying the specific compilation targets and source code files. Instead, you can define some simple compilation options and rules:

#makefileLDLIBS= -lmCFLAGS= -g -std=c99 -Wall -Werror

In VIM:

  • : Make list-- Use the rules defined in makefile to compile list. c
  • : Make hello-- Use the rules defined in makefile to compile hello. c

You can also use the makeprg command to specify the compilation targets and rules, but this command is not easy to use, and every time you change a new file, it is not as convenient as makefile.
Use quickfix to quickly modify compilation errors

After the make command is run, if there is a compilation error, VIM will list the compilation errors in the form of a list, and the equickfix tool will help you quickly locate the wrong line

  • : CC-- Displays details of compilation errors in the status line.
  • : CN-- Next compilation Error
  • : CP-- Previous compilation Error
  • : CW-- Open the quickfix window. This will open a new window under the screen, which contains a compilation Error List
  • : CL-- List all compilation errors

Quickfix is actually a compilation Error List. Each quickfix list can display 10 errors, so I want to see other errors (if there are more than 10)

  • : Col-- Previous list
  • : Cnew-- Next list
Example: Edit, compile, run, and debug a single file:

$ Vim hello. c
Edit
: Make hello-- Compile hello
: CC-- Display compilation error information
: CN-- Next
: CN-- Next
: Make hello-- Compile... okay
: Sh-- Enter Shell
$./Hello-- Run and test the program, with bugs
$ GDB hello-- Debugging... found the problem
$ Exit
Fix running problems
: Make hello
: Sh
$./Hello
$ Exit-- Return to the editor

Use the powerful search artifact grep in VIM

You can also use the search matching artifact grep in vim.

  • : Gr/grep [opt]Pattern files-- Search for matching pattern in files, which is the same as grep in shell command.

The result is given in the form of a quickfix list, so the quickfix command is also applicable here:

  • : CN-- Next match
  • : CP-- Match the previous one
  • : CW-- Open the quickfix list, which contains hyperlinks. After clicking, you can directly enter the match.
  • : CL-- View all matches

These commands will jump from a match to the next or previous match. If this file is exceeded, it will enter another file. Therefore, it is very powerful to jump between different files.
For more information, see help.: Help grep
Global Search and replacement

This is also the use of the ex command, because you can write a separate article, please read this article

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.