Convenient Development Editing Tools - Vim

xiaoxiao2021-03-06  41

Convenient Development Editing Tools - Vim

Mounton @ www.ihere.org (mounT0n@yahoo.com)

June 2003

0. Preface

The programmer spends the longest development tool may be an editor, a very convenient, efficient editor is very effective for developers. Under UNIX / Linux, even Windows, VIM can be said to be a very good editor. Although many friends are using Vim, but usually only use VIM very limited functions, many of the functions that are very conforming to developers are not exposed to VIM. Below, it will be introduced from the development process to the function of the development process. After reading this article, you will find that Vim is originally such a powerful integrated development environment.

Vim is a GPL-based open source project, which is the upgrade version of VI, and VI is the most universal editor in the UNIX environment. However, due to Vim's powerful features, many of the latest released Linux versions have been used as the default VI editor. If you do not install VIM in your system, you can download the appropriate version from the Vim.SourceForge.net. Gossip less, the following is introduced from the basic editing command, the general editing command, the development common command, and the like. This article is dominated by the introduction, and it will be understood in the details. The following discussion is based on VIM 6.2.

1. Basic editing command

This section simply classified basic VIM editing commands, only gives a simple functional description, as a command to use Vim, please check the online help of Vim for a detailed description. Enter the Vim: h . The following command is in normal mode (there is a state where the ESC button is reached in any state, in order to avoid conceptual blur, here is not described in these states) input character sequence.

command

meaning

command

meaning

Mobile hjkl's left upper right (and direction keys) HML in the same page (screen) first line, middle row, last line Ctrl f Ctrl B Next Previous Ctrl D Ctrl U low pages The first page of BWE, the first word of the first word, one word, first, one word, one word, 0 ^ $, the first line of non-empty character line

Enter a A current character after entering the row input I i Current Character Front Front input O o Current row inserting a new row input Current line Insert a new line input: r [filename] Currently inserting the file [filename]

Change the text within the range of D [Range], such as: DW (delete to the word) D $ (deleted to the end) C [Range] change, remove the designated state after delete the specified range ~ [Range] Change the current character or range Internal cases, jinks the current row and the next row R [char] replace the character, use the subsequently entered character to replace the current character R into the replacement mode, the input character replaces the text Y [Ran] Copying the text P pasted in the copy range When the current character is removed, the current character is deleted. Repeat the last command to change the text content

Find f [char] f [char] Later in the line to find characters [char] T [char] t [char] t [char] Log finding characters in the line [char] Downtown Find Character [char] Pre-[String]? [String] Find [String] Find [String] After searching, use n to continue searching, using n to continue looking for * # down the opposite direction to find the cursor Current word to find the cursor Current word N N repeat The last time /? Command is repeated the last time /? Command

Others: e filename Close the current file, editing the file: w filename Write Ctrl g: g Display File Information: Help [cmd] View Vim command cmd help information: u Undo A change: Red Redo changes U Undo All changing Note:

[Range] is the range defining the previous action, and the Vim is called Motion. The above changing class commands belong to the motion command, meaning that the cursor position is moved.

Command meaning command meaning 0 to row first character ^ to the first non-empty character $ to the last character

[N] K [n] j goes down a few lines [N] w Delete N words

The above classification is that individuals are classified for ease of memory, without specific criteria or existing rules.

2. General Command

2.1. Search

Search should be said to be a very common command during the development process. The most common search commands are / and the words or regular expressions that are needed later (simple introduction to the following summary). Then use N or N to find the next or last search result.

The most likely situation in the development process is to see a word hopes to find it, this time * (search for crop words down) and # (search the current word upward) command is very convenient. If you want to search for words that contain the current words, you can use the g * or g # command.

Find the definition of variables, you can use GD (local variable definitions of current words) and GD (global variable definition of current words), which are not necessarily very accurate, but sometimes it is more useful.

The macro definition of the current word can be used [_CTRL-D to find the first position of the current word appears after #define. And [D shows the current file and the first macro definition of the current word in the file, [D lists all macro definitions. The beginning of the command starts from the current location.

Searching in the current file and containing the header file, search macro definition can use the declarations of the above command, functions, variables, etc. [I lists all rows. The beginning of the command starts from the current location. The command of Vim is still more consistent. Use [_CTRL-I (starting from the first row), or] _ctrl_i (starting from the current location).

Multi-file search can be implemented by external commands: GREP, the default and use of the Grep command is very similar, find the list, you can use the QuickFix class command to loop access in the list, you can also be in multiple GREPs through such commands In the command list, this command is designed to be designed for compilation, and will be described in detail at Section 3.4.

The replacement command format is relatively long, but sometimes it is very useful, you must understand that every detail can protect flexible use. The command format is as follows:

: [RANGE] S [ubstitution] / {string} / [&] [c] [e] [g] [p] [r] [i] [i] [count]

The meaning of this command is in the range of Range, and lookup pattern definitions to String.

Range definitions can be referring to: Help Range, usually specify the number of rows, tags, and even specify a search, for example:

3,. 1 from 3 lines to the current line

'T, $ - 2 from the mark t to the article two lines after the end of the article

% All articles, equivalent., $

., / {pattern} / from the current line to the next line of matching Pattern

Pattern and search modes described above can be a string or a regular expression.

String can usually be a string, other special contents can be referred to: Help Sub-Replace-Special.

The final mark is as follows:

& Keep the tag of the last replacement command

c For each replacement

g Replace all matching modes per line, the default line is replaced once

i ignore the case

I don't ignore the case

2.2. Regular expression

Using regular expressions greatly enhanced search, replacement capabilities, but this also increases the difficulty of learning. Here, you can use the marks that may be used in the development process, and then take a few examples, you should be able to give an array of three, and you will know how to use it. Matching mode learning can be referred to: Help Pattern.

command

meaning

command

meaning

Ordinary Atom ^ / _ ^ Matching, must start anywhere in Pattern to match the first matching line, you must match the row at the end of Pattern. / _. Match any character, can not be anywhere at the end Matching characters /

MULTI ITEMS * 0 or more / 1 or more /? 0 or 1 / {n, m} n to m, try to match most of / {- n, m} N to M, try to match a few

Character Classes / S / S Blank Character Non-blank Character / D / D Digital Non-Digital / X / X Hexa Binary Digital Non-hexadecimal Digital / A / A Letter A-ZA-Z / W / w words include letters, numbers and underscores / h / h characters including letters and underscores

Other / E / T / R / B / N / c / c ignore case matches

In this way, the flag of the C language can use /

/

2.3. Multi-window

Sometimes you need to open multiple files or see a different part of a file, this situation is done in multiple windows.

Open a new window to use: split [filename] command, add the text name to the window to edit this file, otherwise edit the current file.

Close the current window to use: q.

Ctrl-w j Next window

Ctrl-w k on a window

Ctrl-w = all windows are high, wide

Ctrl-w increases the current window height

Ctrl-W - Reduce the current window height

Different files in memory are a buffer, one buffer can have 1 or more Windows, and even there is no Window. When jumping between multiple files (later), the buffer where the usual file is located is still in memory. You can use: buffers list all buffers in the current memory.

Use: BN (next buffer) and: bp (previous buffer) cycle between buffer.

2.4. Visual mode

There are too many graphical interfaces, and you may think that the use of commands is not intuitive, and the visual mode allows visual selection to select a piece of text, and then enter the relevant command to operate it. The use process is moving to block start characters, and the input command enters the visual mode; use various moving commands to move to the block; enter the relevant command to operate this block.

v Enter characters to select visual mode

V Enter line Select Visual Mode

Ctrl-V Enter Block Select Visual Mode

2.5. Mark (Mark)

Making a mark for a row is a very useful feature. It can be easily moved to this line in the future, and this location can also be referenced in Motion.

Setting tag Use M {A-ZA-Z} to set a tag, the name is the characters behind, one file can define a total of 26 tags in a file, and a total of 26 tags can be defined between the files. Move to the marker line, move to the [char] or `[char] command to move to the [CHAR] corresponding to the mark.

2.6. File recovery

Things to knock half a day cannot be lost due to power failure. Vim default input 200 characters or free 4 seconds to save in the .swp file.

The usual recovery step is to move to the file where the file is located; recover the filename file using the Vim -r [filename], if the edited file is not saved using Vim -R ""; save the recovered file as another file, use DIFF programs (you can use vimdiff) to see if it is restored correctly. If you don't remember the file name, all exchange files are listed using Vim -r.

3. Develop common orders

3.1. Tag

After tag, it is very convenient to jump in multiple source code files, this feature is very convenient to browse, read code.

The C / C source program can use ctags -r * .c * .cpp generate TAG files, and CTAG is already included in the latest VIM release. With a TAG file, you can easily jump.

Use Vim -t [Tagname] to boot VIM to open the appropriate file directly and position it to the location of Tagname. Using the command in VIM: tag [tagname] can jump to the definition of Tagname, tagname can use a regular expression to make blur matches.

Use ctrl-] can be jumped directly to the definition of the current word, if the current word is not tag, use the first Tagname on the right. This beating process makes up the tag stack. The command that jumps back along the stack is Ctrl-T, and the command moves up and down along the stack is: TN (old item) ,: TP (new item). List Tag Stack Use: tags.

3.2. Preview window

When calling a function, you usually need to view the parameters of the function, which can be easily implemented using the TAG technology and preview window.

Command: ptag [tagname] The preview window is displayed to the tagname definition. As a Tagname, the current word can enter Ctrl-W} directly, see the definition of the current word in the preview window.

Turn off the preview window Use: PCLOSE command.

Open a file in the preview window, use: PEDIT [FileName] command. Search a word in the current file and the included file and display it in the preview window, you can use: pSearch [name]. This command search function prototype is usually used.

3.2. Mobile

Vim defaults to support a variety of indentation methods. Adjusting indentation can use <(to left) or> (to right), you can add a range, or later add Motion.

Use the = {motion} command to automatically indent a piece of text.

Find the corresponding {}, [], (), sometimes more trouble, using the% command can be very convenient to find the corresponding symbol, except for the few sets of symbols above, can also be / * * / or #if #ifdef #else # Elif #endif, etc.

Code blocks are typically included by {}, which is very convenient between the respective code blocks. [{Moving to the cursor, the block,]} is moved to the tail of the position. Use the command [[{{to the outermost layer {,]] to jump to the outermost layer}, the outermost layer is usually the function boundary of the C / C program. The second layer {use [m command, second layer}] m command, usually the second layer of the Java program is the boundary of the function.

Jump in the file or between files, sometimes you need to return the location where the last cursor is located. You can use the CTRL-O command to jump to the previous location, and the ctrl-i command jumps to the new location. Use the command: jumps list all the jump lists. These jump commands include hopping to tag, searching, search, replacement,%, more than one line or a column of movement, etc. 3.3. Help

When writing a program, you often need to use Man [Section] FuncName to view the help of a system function, use the [section] k command in Vim to view the MANPAGE of the current word.

If you do need to execute the shell command, you can use:! to perform a shell command without leaving the VIM environment and the current terminal, you can get a shell, you can use: shell.

3.4. Compilation

Say that VIM can be an integrated development environment, which is to easily implement editing - compile - edit cycle. If your program has Makefile, use: make command to compile, and more convenient is that VIM can identify errors in compilation output and position it to the appropriate location. Command: CC [NUM] Displays the Nth Complete Error Message.

Move in the error list, you can use: CN (next error) and: CP (Previous Error). List the entire error list usage command: CLIST.

Vim saves multiple error lists, old error lists: CNEWER.

These commands are called QuickFix commands, and the grep commands described above also use these commands.

4. Reference

Vim HomePage http://vim.sourceforge.net/

2. VIM Documentation Home http://vimdoc.sourceforge.net/

3. VIM User Manual, is also the online help document of Vim, very comprehensive. Http://vimdoc.sourceforge.net/dion/vimum.html

4. Vim book, very detailed. http://www.truth.sk/vim/vimbook-opl.pdf

5. About the author

Mounton @ www.ihere.org is currently concerned about the development, research of network security products; software development processes. You can contact him via mount0n@yahoo.com.

转载请注明原文地址:https://www.9cbs.com/read-79018.html

New Post(0)