Tip: Vim's portrait editing mode

Source: Internet
Author: User

Before https://www.ibm.com/developerworks/cn/linux/l-cn-vimcolumn/starts

The human brain's processing of words is flat, so we browse articles, find materials, or refactor code, either horizontally or vertically, or use search to make text as a whole. While editing text, writing code is not horizontal or vertical. Regular text editors are horizontal edits, and portrait editing is often used as a feature. For example, Vim, EditPlus, UltraEdit These editors have portrait editing mode, or column mode. Like everyday word processing tools MicroSoft Word, OpenOffice Writer can also select text vertically by pressing the function key ALT, and then manipulate the text. Vertical editing is not only a function of the editor, but also a way of thinking about the problem, bullets, paragraph numbering, are the embodiment of vertical editing.

Vim's vertical editing mode is easy to start, flexible to use, and can be used in conjunction with the plug-in to achieve very useful advanced features.

Back to top of page

Start method

In Vim command mode, the move cursor is positioned in a location, and the ctrl-v word "VISUAL BLOCK" appears on the status bar after typing, which is entered in portrait editing mode. Move the cursor to select the area you want to edit on demand. Note: In the Windows version of VIM, key combinations ctrl-v are usually mapped to text pasting, so the Window version of Vim's portrait editing mode is initiated by the ctrl-q . Of course, flexible Vim can also be defined by the user's own key combination.

Back to top of page

Example 1: Portrait editing application demo in bulk code modification

The number of columns:

10.1.5.214  10.1.5.212  10.1.5.210

Edit into sequence:

Ping-c 4 10.5.5.214 >> result0  ping-c 4 10.5.5.212 >> result0  ping-c 4 10.5.5.210 >> result0

This is a process of modifying the IP sequence to an executable ping command sequence.

First step: Modify

Change the second paragraph in the IP sequence to "5" for all the Numbers "1":

Position the cursor "1" in the second paragraph of the first line IP address

ctrl-v Go to Portrait editing mode

G Move the cursor to the last row, and the visible block overrides the column you want to modify

r Enter Modify mode

5 Enter the number "5"

ESC Exit portrait editing mode, and all selected numbers are changed to "5" and back to command mode

The results are as follows:

10.5.5.214  10.5.5.212  10.5.5.210
Step two: Before adding

Add "Ping–c 4" before all lines:

Position the cursor to the first column in the first row

ctrl-v Go to Portrait editing mode

G Moves the cursor to the first column in the last row, and the visible block overrides the first column

I Enter the beginning of the insertion mode

ping -c 4 Enter the required character "Ping–c 4"

ESC Exit portrait editing mode with "Ping–c 4" added before all selected characters, back to command mode

The results are as follows:

Ping-c 4 10.5.5.214  ping-c 4 10.5.5.212  ping-c 4 10.5.5.210
Step three: Add after

Add ">> result0" after all lines:

Position the cursor to the last column in the first row

ctrl-vGo to Portrait editing mode

GMove the cursor to the last column in the last row, VISUAL BLOCK overwrites the last column

AEnter end of line insert mode

>> resultEnter the required characters ">> result0"

ESCExit portrait editing mode with all selected characters added ">> result0" back to command mode

The results are as follows:

Ping-c 4 10.5.5.214 >> result0  ping-c 4 10.5.5.212 >> result0  ping-c 4 10.5.5.210 >> result0

The above three steps have a common feature, that is, both portrait orientation for editing. The above example of three lines of code can also be applied to more rows.

Back to top of page

VISINCR Installation and operation

The vertical editing of the code shown in the example above can also be achieved through a general code refactoring function or a lookup substitution combined with regular expressions. However, the vertical editing mode of Vim can also be extended by the plugin visincr to achieve richer functions, such as the vertical generation sequence.

Search for the installation file of the VISINCR plugin from Vim's official website and download it to the VIM working directory. You can install the plugin by executing the following command:

Vim visincr.vba.gz  : So%  : Q

Example of how to act in Example 1:

Use ctrl-v and move the cursor to select a column that ends with the number "0"

:Type a colon to trigger Vim into command line mode

:IStarting with the first digit, the increment sequence is generated vertically, with a magnitude of 1

ENTERType the drive key to execute the command

The results are as follows:

Ping-c 4 10.5.5.214 >> result0  ping-c 4 10.5.5.212 >> result1  ping-c 4 10.5.5.210 >> result2

Back to top of page

Example 2:VISINCR application demo for generating test data

It is often necessary to generate a simple and regular test data during application development. For example, to a table:

TestTable ("ipaddr", "filename", "owner")

Generate the following SQL statement:

INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.212", "Result1", "Testb");  INSERT into Test values ("10.5.5.210", "result2", "TESTC");

This vertical, regular sequence of statements can be constructed using the VININCR application of the portrait editing function.

First step: Prepare

Open a new Vim editing page, enter a "3" in command mode, and type "I" into edit mode. The number "3" means that the input will be repeated 3 times.

In VIM, enter the first line first:

INSERT into Test values ("10.5.5.214", "result0", "Testa");

End the line entry with a carriage return and open a new line, pressing ESC to exit edit mode.

If this is the case:

INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.214", "result0", "Testa");
Step two: Construct the descending sequence

For ipaddr columns, use ctrl-v and move the cursor to select the last segment of the IP address that is decreasing in magnitude 2:

:Type a colon to trigger Vim into command line mode

:I -2 Starting with the first digit, the descending sequence is generated vertically, with a magnitude of 2

ENTERType the drive key to execute the command

The results are as follows:

INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.212", "result0", "Testa");  INSERT into Test values ("10.5.5.210", "result0", "Testa");
Step three: Construct an increment sequence

For the filename column, using the method described in the previous section, the results are as follows:

INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.212", "Result1", "Testa");  INSERT into Test values ("10.5.5.210", "result2", "Testa");
Fourth step: Construct the letter increment sequence

For the owner column, use ctrl-v and move the cursor to select the portion of the English letter that will increment.

:Type a colon to trigger Vim into command line mode

:IA Create a sequence of alphabetically sorted by the first letter as a starting point

ENTER Type enter key to execute the command

The results are as follows:

INSERT into Test values ("10.5.5.214", "result0", "Testa");  INSERT into Test values ("10.5.5.212", "Result1", "Testb");  INSERT into Test values ("10.5.5.210", "result2", "TESTC");

The above steps demonstrate the use of the Vim plugin visincr to edit the code vertically, generating a sequence of logical rules in the vertical.

Back to top of page

Summarize

Vim is not only an editing tool or a set of thinking methods, Vim has a lot of guidance in the direction of thinking skills, so that vim in the various editors in the endless time of the new. This paper, from the perspective of vertical editing, combines visincr to demonstrate the examples that can be used in the development and testing process.

Tip: Vim's portrait editing mode

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.