5.5-5.7 use and expansion of vim

Source: Internet
Author: User
Tags decrypt

5.5-5.7 use and expansion of vim

Settings for VIM

The difference between ": Wq" and ": X"

    • ": Wq" Mandatory write file and exit (save and exit write and quite). Force write even if the file is not modified, and update the file's modification time.

    • ": X" writes the file and exits. Writes only when the file is modified and updates the file modification time, otherwise the file modification time is not updated.

    • Difference:

The two are generally not the same, but in terms of programming, editing the source file can have a significant impact. Because the file is not modified, ": Wq" forces the update of the file's modification time so that make compiles the entire project with the file being modified, and then the recompile link is delivered to the executable file. This can have misleading consequences and, of course, unnecessary system resource costs. But like version control software generally preferred or compare file content, modification time is generally ignored.

Several shortcut keys commonly used in VIM

    • Note All lines: Ctrl + V j/k+ shift +i

    • View cursor the current man manual shift+k

    • d+f+ characters

    • Check area v+t+ characters

    • Remove all characters after the cursor in VIM shift+d

    • Delete the word on the current cursor d+i+w

    • Select the word on the current cursor v+i+w

Some of the commands in vim

Replacement mode

In general mode, press R and R on the keyboard to enter replacement mode. If you press small r then you enter the replacement mode, and the next character you enter will replace the character where you currently have the cursor, and then automatically exit the replacement mode. If you press the big R then all the characters you enter below will replace the following characters in turn until you press exit to replace mode.

Visual mode

Press V and V on the keyboard to enter the visual mode in normal mode. If you press small v then it is time to enter the view mode, when you move the cursor will be at the cursor at the end of all the characters selected, you can copy, delete and so on. If the large V is also entered in the view mode, this time moving the cursor will be the line to the end of the cursor line of all the contents of the selection, you can also copy, delete and so on.

Note: Use the "Ctrl + V" key combination in general mode to enter the block operation mode, which is similar to the visual mode in this mode, but the selected content is different.

Remove the character from the end of the line from the cursor

Enter uppercase "D" in general mode or enter "d$"

Delete characters from the cursor to the beginning of the line

Enter uppercase "d^" in general mode

Delete the line from the cursor line to the end of the file

Enter the uppercase "DG" in general mode

Delete all rows in the specified range

Eg: delete all contents from 10 to 15 rows

Enter ": 10,15d" in general mode

Save the file you are editing as a new file

Eg: Save the file being edited as "/root/" and save it as 1.txt

Enter ": W/root/1.txt" in general mode

Eg: Save the 10th line of the file you are editing to line 15th as 1.txt and save it to the root directory enter ": 10,15 w/root/1.txt" in general mode

Import the contents of other files into the file where you are editing the cursor

Eg: Import the contents of the "/root/1.txt" file into the next 10th line of the edited file

First in general mode, press "10G" to position the cursor to line 10th, and then press "O" lowercase, the current line below another line, and enter the insertion mode, finally press RETURN to the general mode on the keyboard, and then enter ": R/root/1.txt" return

When editing a file, you can still run the Linux command without exiting the file

Eg: I'm editing a file, but at this point I want to see the contents of the "/root/1.txt" file, but I don't want to quit the file I'm editing, so we can enter "in edit mode":! Cat/root/1.txt "

Imports the execution results of the command into the file where the cursor is being edited

This problem we can combine the above two questions, in the general mode of input ": R!" Cat/root/1.txt "

Find replacements for features using

Eg: add "#" at the beginning of line 10 to 15

Enter ": 10,15s/^/#/" in general mode

Eg: Remove "#" at the beginning of line 10 to 15

Enter ": 10,15s/^#//" in general mode

Eg: added "//" at the beginning of line 10 to 15

Enter ": 10,15s/^/\/\//" or ": 10,[email protected]^@//@" or ": 10,15s#^#//#" in general mode

Note: on the last side of all the commands can be used together with G or C, g means that all occurrences of the specified word nonalphanumeric substitution, but if the addition of G so that the location of the definition of the character, and vice versa before the position of the definition of characters, then you can not appear after G. Add C in the following can interact with the user, in the search to find the characters matching the command prompt the user whether to replace, require user confirmation, or do not need to confirm

Replaces the specified character of the input with the specified character

Eg: when editing a document, I would like to enter a continuous string such as "ABCDEFGHIJKLMNOPQR", when I want to enter only one or a string of specified characters can be replaced with just the character, such as I specify the input "aming" system will automatically put "aming" Replace with "ABCDEFGHIJKLMNOPQR"

Enter ": AB aming ABCDEFGHIJKLMNOPQR" in the general mode and then enter the edit mode, and when you enter "aming" you will find that the "ABCDEFGHIJKLMNOPQR" is automatically replaced.

Definition of shortcut keys

Eg: I want to press the Ctrl+b shortcut on the keyboard in general mode, and automatically insert the "#" sign at the beginning of the line where the cursor is located, and then automatically exit to normal mode.

In general mode, enter ": Map Ctrl + V ctrl+b I #" and then enter, this time in the general mode by pressing the ctrl+b on the keyboard will be at the beginning of the cursor inserted "#" number

Note: Command Ctrl + V and CTRL+B are keyboard combinations, not input characters, is required to press the combination of keys, where the first CTRL + V is pressed, the second ctrl+b is to define the shortcut key, according to the settings you need to press. Then "I" means "I" in the normal mode to enter the insertion mode and move the cursor to the beginning of the line, and then enter the "#", followed by "" means to exit edit mode.

Simultaneous editing of two files

Eg: I am now editing the 1.txt file, and then I want to open the root directory of the 2.txt simultaneous editing, and the window up and down horizontally separated, display together.

Enter ": New/root/2.txt" in general mode

Eg: I am now editing the 1.txt file, then I want to open the root directory of 2.txt simultaneous editing, and the window left and right vertically separated, together display.

Enter ": Vsplit/root/2.txt" in general mode

Note: in general mode Press "CTRL+W" key combination, and then press the left or right, or up and down the direction of the key, you can switch between different windows if you enter ": only" in the general mode, only the window that is currently being edited is kept, and all others are closed.

Case insensitive when vim looks up keywords

Enter ": Set IC" in general mode

If you want to cancel, enter ": Set Noic"

Set the file as read-only file

Enter ": Set readonly" in general mode

Restore the file to its open state

Enter ": e!" in general mode

Use of configuration files

Above so many operations, such as setting ignore case, set shortcut keys, set automatic replacement, and so on some operations, when the computer restarts, there is no. At this point we can write these commands to the configuration file, so that after the restart of the computer can still be used, we have two ways:

    • All users are unified

Modify the "/ETC/VIMRC" file, add the command you want to set at the end, which is the part I marked in red

    • Only for the current user

Modify the ". VIMRC" file in the user's home directory, note that there is a point, which is hidden files, the general user home under No, need to manually create their own

Extended content:

Vim Paste Code Auto indent causes the whole mess:

Use VIM to open a blank document, and then paste the copied code into it, found that it has automatic indentation function, resulting in the pasted text line is more than one line on the right, looks mess. The quicker solution is to enter set noai in command-line mode before pasting the document Nosi

Then press ' I ' to enter edit mode, and then paste the copied code content so that it does not automatically indent.

Sometimes, such a method is not good to use, you can try this:

: Set Paste

Client putty, Xshell connection to the Linux vim keypad problem:

When using VI on the putty, when opening numlock, press the numeric key on the keypad and not enter a number, but a letter and then newline (in fact, the command mode corresponds to the upper and lower keys). Workaround:

Option Terminal->features, find disable application keypad mode, select on it.

The Xshell in Xmanager 4 also has a small keyboard problem resolution:

S

Modify session Properties--terminal->VT mode--Initial numeric keypad mode

Select Set as normal.

Linux uses Vim/vi to encrypt and decrypt files:

First, using VIM/VI encryption:

Advantages: After encryption, if you do not know the password, you can not see the plaintext, including the root user can not see;

Cons: Obviously let others know encryption, it is easy for others to destroy the encrypted files, including content destruction and deletion;

VI Editor believe that everyone is familiar with it, VI has a command is to encrypt the file, for example:

1) First create an experimental file under the root master directory/root/Text.txt:

[Email protected] ~]# Vim/vi text.txt

2) Enter the edit mode, press ESC after entering the content, then enter: X (note is the uppercase X), enter;

3) Then the system prompts you to enter the password, 2 times, as follows:

Enter Password: *******

Please enter again: *******

4) After saving the exit, now this file has been encrypted;

5) Use cat or more to view the contents of the file, displayed as garbled; re-edit this file with Vim/vi, will prompt for the password, if the password entered is incorrect, the same will be displayed as garbled!

Note: Do not forget the password after the file is encrypted!

Second, decrypt the files encrypted with VI (provided that you know the encrypted password):

1) Open the file with Vim/vi, such as Text.txt, to enter the correct password, and then at the time of editing, set the password to empty, by entering the following command:

: Set key=

Then directly enter, after saving the file, the file has been decrypted.

2) or This is OK:

After opening the file correctly with ": X" instruction, and then give a blank password can also. Save with "wq!" Save.

The two methods actually have the same effect.

Vim quickly deletes a specified segment of characters:

Because not a line, so with DD not, but with X to remove, and too slow. Today a special quick removal method is found online.

That is, using DA, how to use it, see the example below.

For example, my 1.txt content is as follows:

11111111111111111111111111

222222222222222222222222222222b3nzac1yc2eaaaabiwaaaqeav5ojvuidaavusdoa2fbfnl0k2gbtc05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnukv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola /1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/ rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e3333333333333333333333

44444444444444444444444444444

I now want to remove the characters between 22222222222 and 33333333333333333 only to do this:

1. Change the contents of the 1.txt to:

11111111111111111111111111

222222222222222222222222222222{b3nzac1yc2eaaaabiwaaaqeav5ojvuidaavusdoa2fbfnl0k2gbtc05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnukv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola /1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/ rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e}3333333333333333333333

44444444444444444444444444444

Note that there is no line break between 2222 and {.

2. Move the cursor to the first {, that is, the last 2, and then enter da{to delete all the characters in the {}.

In addition, in addition to the use of {, you can also use ",", (such as the special symbol of the pair.)

In fact, Vim has a more commonly used that is V, with V and D to delete is also very convenient:

After you open a text, press v directly, and then move the cursor to select the text, and when you are finished with the text you want, press D directly, and then delete it.


5.5-5.7 use and extension of vim

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.