Linux VIM Usage

Source: Internet
Author: User

Vim

General mode, edit mode, and command line mode, respectively

General mode:

Open a file with VI directly into the general mode ( This is the default mode ).

You can use the up and down buttons to move the cursor, you can use the delete character or delete the entire line to process the file content, you can also use copy, paste to process your file data.

Edit mode:
in general mode can be deleted, copy, paste and other operations, but cannot edit the contents of the file, only when you press:I, I, O, O, A, a, R, R and so on any one letter will enter the editing mode. the INSERT or REPLACE Word appears at the bottom left of the screen and can be edited. If you want to go back to normal mode, simply press ESC to exit edit mode.

Command-line mode:
Enter :/? any of the three, you can move the cursor to the bottom row. In this mode, you can provide search, read, save, replace characters, leave VI, display line number and other actions are done in this mode!

Description of buttons available in general mode
Move Cursor
"H,J,K,l", respectively, control the cursor left, bottom, up and right one grid
Press "ctrl+b" screen to move one page to " back "
Press "ctrl+f" screen to move one page toward " front "

"n<space>" cursor moves n characters to the right
" Home"Move to the front of this line prompt

"0" number , ibid.
" End" moves to the last face of this line prompt

"$" character , ibid.

"w" cursor jumps to the beginning of the next word
"e" cursor jumps to the end of the next word

" h" The first character of the line that the cursor moves to the top of the screen
" M" cursor moves to the first character of the line in the middle of this screen
L "cursor moves to the first character of the line at the bottom of this screen

" G" move to the last line of this file
"NG" moves to the nth row of this file ( mates : Set Nu)
"gg" moves to the first line of this file, equivalent to 1G
"n<enter>" cursor moves down n rows

Find and replace
" /word " Look down to the cursor for a name called word? Word" Look up a string called word
" n " Represents the repetition of the previous search action?
" n" In contrast to the n

": n1,n2s/word1/word2/g" N1 and n2 are numbers, find n2 between the N1 and Word1 lines This string and replace the string with Word2

": 1, $s/word1/word2/g" finds the Word1 string from the first row to the last line and replaces the string with Word2
": 1, $s/word1/word2/gc" finds the Word1 string from the first line to the last line and replaces the string with Word2 , and prompts the user to confirm the replacement before replacing

Delete, copy, and paste

"x" removes one character (equivalent to "del" key ) for backward
" X" to remove one character ( equivalent to"backspace"key )
"NX" continuously deletes n characters backwards

"dd" deletes the line where the cursor is located
"ndd" removes the downward n-line of the cursor
"d1g" removes all data from the line to the first row of the cursor
"dG" removes all data from the last row of the cursor

"d0" deletes the first character of the line at the cursor location

"d$" deletes the cursor where the last character of the line

"yy" the line where the cursor is copied
"nyy" copy the down N column where the cursor is located

"y1g" copies all data from the row to the first row of the cursor
"yG" copies all data from the row to the last row of the cursor

"y0" copies all data from the same character as the cursor to the beginning of the line
"y$" copies all data at the end of the line with the same character as the cursor

"P" Pastes the copied data on the next line of the cursor
" P" is affixed to the previous line of the cursor

"u" restores the previous operation

". " is to repeat the previous action

: W" save edited content
": W! "Forces writing to the file, but with your permissions on the file
" : Q " Span style= "font-family: Arial" > left VI
": Q! " do not want to save changes forced to leave
" : Wq" Save and leave
: x< Span style= "font-family: Arial" > " Save and leave
" zz " If the file has not changed, then do not save leave, if the file has been modified, then save left

": W filename" save edited data to another file (similar to save)
": R Filename " in the edited data, read into the data of another file. Upcoming " Filename" The contents of this file are appended to the line where the cursor is located.

: n1,n2 w filename " will N1 N2 The content of filename
":! Command" temporarily away VI to the command-line mode to perform the command display results! For example :! Ls/home vi /home under the ls output file information!

": Set nu" displays line numbers
": Set Nonu" in contrast to set Nu , to cancel a row

"v" character selection, will be the place where the cursor will be reversed white selection
" V" line selection, the line that the cursor passes through is reversed to the white selection
" Ctrl + V" block selection, you can select data in a rectangular way (obtained vertical column)
"y" will copy the anti-white place
"d" will remove the anti-white place

Multi-file editing

"vim file1 file2"

": N" Edits the next file
": N" Edit previous file
": Files" lists all the files currently in this vim editor

Multi-Window function
There are two files that need to be viewed against.
": SP filename" opens a new window, if there is a filename, the new window opens a new file, otherwise two Windows is the same file content ( synchronized display ) .

"ctrl+w+j"
"ctrl+w+↓" Press the key is: First press "Ctrl" not put, then press W to release all the keys, and then press J ( or DOWN ARROW key ), the cursor can be moved to the window below.

"ctrl+w+k"
"ctrl+w+↑" Ditto, but the cursor moves to the window above.

Vim Environment setting parameters, want to know the current setpoint, you can enter in the general mode " : Set all" to check,

:set All " Show all current environment parameter SetPoint
: Set Hlsearch" high brightness anti-white ( high brightness search )
: Set Nohlsearch " remove high brightness anti-white ( high brightness search )
: Set backspace=2" when editing can be at any time with the backspace key to delete the (0, 1, only for the characters just entered valid)
: Set Autoindent " auto indent
: Set noautoindent" Cancel Auto Indent
: Set ruler "
: Set Showmode"
: Set nu " display line number
: Set Nonu" cancel line number
: Set Bg=dark " show different base xxx
: Syntax on" grammar test, Color display
: Syntax off " Close grammar check

Now that we know what we have done, we can write our ownVimoperating environment.
OverallVimis usually set to a value of/etc/vimrcinside this file, it is not recommended that you modify him. We set up a family in our own home directory.. VIMRCfile, which can be achieved by writing your own content here.
[Email protected] ~]# vim ~/.VIMRC
The contents are as follows
Set Hlsearch "High Brightness anti-white
Set backspace=2 "can be deleted at any time with backspace key
Set Autoindent "Auto Indent
Set Ruler "the status of the last row can be displayed
Set Showmode "The state of the line in the lower left corner
Set Nu "line numbers can be displayed at the front of each line
Set Bg=dark "show different bottom xxx tune
Syntax on "Perform a grammar check, color display
double quotes for this file(")represents a comment

After you save the vim, you will have your own VIM operating environment The next time you use vim .
As a reminder, each line in this file is preceded by a ":" Effect.



Vim cache file, restore and open warning message

when we use vim to Edit,vim will be in the directory with the edited file, and then create a file named . filename.swp.

if the system is disconnected for some reason, The file that caused the edits has not yet been saved, this time . FILENAM.SWP will be able to send a rescue function.

For example:

Vim Man.config
CTRL + Z put in the background to execute
we stop process of VI
a method
PS aux |grep VI
kill-9 N


A different approach
Jobs
Kill-9%1

we use the ls-l command to check the contents of the directory, we will find a. man.config.swp file , this file is a cached file

Let's edit it again.
Vim Man.config
There will be some information
problem One: There may be other people or programs editing this file at the same time:
problem Two: In the previous vim environment, vim interruption (crashed) may be caused by some unknown cause :

There are six command entries in the lower right corner, which can be described as follows:
(O) pen read-only: Opens this file as a read-only document, can be used when you just want to view the contents of the file and do not want to edit the behavior. In general, in class, if you are logged in to a classmate's computer to see his configuration file, The result found that the classmate himself in the editing, you can use this mode;
(E) dit anyway: or open the file you want to edit in a normal way, the contents of the scratch disk are not loaded. If two people are editing this file, it is easy to change each other's files and so on.
(R) Ecover: Load the scratch disk with the unsaved work you want to save. But when you get back and save it,Vim, you should manually delete the temporary archive yourself.
(D) elete it: You are sure that the temporary archive is useless! The scratch disk will be deleted before you open the file .
(Q) uit: Press theQjust leave .Vim, no action is made to return to the command prompt character.
(A) bort: Ignore this edit behavior, feel on withQuitvery similar!


Linux Vim use

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.