A quick start to the Vim editor on Linux

Source: Internet
Author: User



1. Introduction



The VIM editor is a powerful cross-platform text file Editing tool that inherits from the VI Editor of UNIX systems, supports the LINUX/MAC OS x/windows system, and uses it to create and modify text files.



Open the file in the following way:



# vim [options] [file ..]
Options file
+#: after opening the file, directly place the cursor at the beginning of the line;
+/Pattern: after opening the file, directly place the cursor at the beginning of the first line matched by pattern;
FOR EXAMPLE:
[[email protected] ~] (VIM + 10 / TMP / my.cnf) open my.cnf file and the cursor is at line 10
[[email protected] ~] (VIM + / basedir / TMP / my.cnf) open my.cnf file and the cursor is at the beginning of the first line matched by basedir;


You can also open it directly using the following method:



#vim [File]


Where [file] is the path name of the file to be edited. If the file does not exist, it will create a new file. Vim has three modes of operation, respectively called:



edit mode , input mode , and last line mode , when Vim is running, it first enters edit mode.



Edit mode (default format after opening file): Move cursor, cut/paste text
Input mode: Modify Edit Text
Last-line mode: Save, exit, built-in command operation, etc.


Conversion between three modes requires the input key:



Edit mode--Input mode:



I:insert, enter at the cursor position;



A:append, enter at the rear of the cursor;



o: Opens a new line below the cursor location;



I: Enter at the beginning of the line where the cursor is located;



A: Enter at the end of the line where the cursor is located;



O: Opens a new line at the top of the cursor position;



Input mode--edit mode



Esc



Edit Mode--last-line mode



:



Last-line mode--edit mode



Esc



650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/87/DF/wKiom1fjisHw_P2PAACvW0TRfYc000.png-wh_500x0-wm_3 -wmp_4-s_3351107520.png "title=" Vim.png "alt=" Wkiom1fjishw_p2paacvw0trfyc000.png-wh_50 "/>






2. Edit mode



Vim edit mode can move the position of the cursor, and the body can be copied, pasted, deleted, inserted and other operations. After all the editing work, you need to save the editing results, exit the editing interface, you can issue the ZZ command, two times the capitalization of the Z key.



2.1 Jump of the cursor



You can use the top, bottom, left and right keys to move the cursor, to achieve the jump between characters. You can also use the following keys to complete the same character movement function:



H: Left J: Lower K: Upper L: Right




Jump between words



w: The beginning of the next word;
e: The ending of the current or next word;
b: The beginning of the current or previous word;


Beginning line End Jump



^ : jumps to the first non-white space character at the beginning of the line;
0: jump to the first line;
$: jump to the end of the line;


Inline jump



#G: jumps to the line specified by #;
1G, gg: first row;
G: the last line


2.2 Flip Screen:



Ctrl+f: flip the screen to the end of the file
Ctrl+b: flip to the top of the file
Ctrl+d: flip half the screen to the end of the file
Ctrl+u: flip half the screen to the top of the file
Enter: Enter


2.3 Search Matches



Search is an essential feature of the file editor, and Vim can use the search function directly in edit mode.



The search is used in the following ways:



/PATTERN: from where the current cursor is, look to the end of the file for all strings that can be matched to by the current PATTERN
? PATTERN: from where the current cursor is, find all strings to the top of the file that can be matched by the current PATTERN
N: next, in the same direction as the command
N: last one, opposite to the command


2.4 Editing commands



Character editing:
X: delete the character where the cursor is;
#x: delete the # character at the beginning of the cursor;
Xp: exchanges the position of the character at the cursor and the character after it.

Replace command:
R: replace the character where the cursor is by pressing the r key on the character where the cursor is, and then enter the character to be replaced
Delete command:
: delete command, can be combined with the cursor jump character, to achieve range deletion;
D $: deletes the character from the cursor position to the end of the line;
D ^ : deletes the character from the cursor position to the beginning of the line;
Dw: delete a word after the cursor;
Db: delete the word before the cursor;
Dd: deletes the line where the cursor is;
Dd: deletes all lines beginning with the cursor;
Special note: delete operation is also a cut operation, the last delete will be copied to the clipboard
Paste command (p, put, paste) :
P: if the contents of the buffer are full lines, paste them under the line where the current cursor is; Otherwise, paste it behind the current cursor.
P: if the contents in the buffer are full lines, paste them above the line where the current cursor is; Otherwise, paste to the front of the current cursor;
Copy command (yank, y) :
Y: copy, working behavior similar to d command;
Y $: copies the character from the cursor position to the end of the line
Y ^ : copies the character from the cursor position to the beginning of the line
Yy: copy a whole line
#yy: copy line #
Change (c) :
Edit mode -> input mode, delete operation;
C $
C ^
Cc: deletes the line where the cursor is located and converts it to output mode;
#cc: removes # lines and converts to output mode

Undo operation:
U: undo the previous operation;
#u: undo previous # operations;
Revocation of previous revocation:
Ctrl + r
Close file:
ZZ: save and exit;
Type. The last command will be executed


3. Input mode



You can only enter the input mode from edit mode, enter the following mode:



After positioning the cursor from edit mode, press the following key to enter the input mode in different ways:
I: insert, type at the cursor;
A: append, type after the cursor;
O: open a new line below the cursor;
I: type at the beginning of the line where the cursor is;
A: enter at the end of the line where the cursor is;
O: open a new line above the cursor;


Return to edit mode by pressing the ESC key.



4. Last-line mode



The command mode is the interface of Vim's built-in command line and can be used to edit files using complex commands. In edit mode, type:, the cursor jumps to the last line of the screen, where the colon is displayed, and the last line mode is entered. Command mode is also called command mode, the user input content is displayed on the last line of the screen, press ENTER, execute the command.



4.1 Address delimitation



In the last line: After entering a specific character to achieve the address bound


# : a specific line #, such as 5 is line 5;
. : current line;
$: last line;
#,# : specifies the range of lines. The left side is the starting line and the right side is the ending line.
#,+# : specifies the line range, the left side is the absolute number of the beginning line, and the right side is the offset relative to the left line number;
For example: 3,+7 represents rows 3 to 10;
.,$-1: current line to penultimate line
1,$: first row to last row
The full text % :
/pattern/ : the line from the beginning of the cursor to the end of the file that is first matched by the pattern;
/pat1/,/pat2/ : all the lines between the first line matched by pat1 and the end of the first line matched by pat2, starting from where the cursor is;
Can be used together with the editing command to achieve editing operations:
28d: delete 28 lines
3y: copy the third line
W /PATH/TO/SOMEFILE: saves the text in the scope TO the specified file;
R /PATH/FROM/SOMEFILE: reads the text in the specified file and inserts it into the specified location.


4.2 Find



Find in last-row mode is equivalent to/Find in edit mode



/PATTERN: find all strings that can be matched to by the current PATTERN from the position of the current cursor to the end of the file;
? PATTERN: find all strings that can be matched to by the current PATTERN from the position of the current cursor to the beginning of the file;
N: next, in the same direction as the command;
N: the previous one, opposite to the command;


4.3 Find and replace



S: command of the last-line mode;



Use format:



s/what to look for/replace with content/modifiers



What to look for: You can use regular expressions;



Replace with: cannot use the next expression, but can be referenced;



if the "What to find" section uses grouping symbols in a pattern: in Replace with content back to reference;



Direct reference to find the pattern to match all the text, to use the & symbol;



Modifier:



I: ignoring case;



G: Global substitution means that if a row is matched to multiple times, it is replaced;



You can replace the delimiter with other non-characters commonly used characters:



[Email protected]@@



s###



Example:



%[email protected]\<t\ ([[: alpha:]]\+\) \>@t\[email Protected]%[email protected]\<t[[:alpha:]]\+\>@ &[email protected]


Use the exercise:



1) copy /etc/grub2.cfg file into /tmp directory and delete the whitespace character at the beginning of the line in /tmp/grub2.cfg file with the find and replace command;
% ^ have protected [email] [[: space:]] \ [email protected] @
2) copy of the/etc/rc. D/init. D/functions provides the file to the/TMP directory, replace with find command for each of/TMP/functions provides file with blank characters at the beginning of the beginning of a line with with #;
[email protected] % ^ \ [[: space:]] + [^ [: space:]] @ # & @ g
3) start the last four lines of the/TMP /grub2.cfg file with a #;
At $3, [email protected] * @ # & @
4) replace all enabled=0 in /etc/yum.repos.d/ centos-base.repo file with enabled=1, and all gpgcheck=0 with gpgcheck=1;
% \ [email protected] (enabled \ | gpgcheck \)/email protected \ [email protected]


5. Vim's multi-file function:



Multiple File Open:



Vim FILE1 FILE2 ...


To switch between files:



: Next Next: Prev Previous: first one: Last


Exit All Files:



: Wqall Save all files and exit;: Wall:qall





Multiple windows:



-O: Horizontal Split Window-o: Vertical Split window


To switch between windows:


Ctrl+w, arrow Note: Individual files can also be split into multiple windows for viewing: Ctrl+w, S: Split horizontal window ctrl+w, V: Vertical Split window

 




6. Customize Vim's working characteristics:



Note: The settings in the last line mode are only valid for the current VIM process;



Permanently valid:



Global:/ETC/VIMRC User personal: ~/.VIMRC





Line number



Display: Set number, abbreviated set NU Suppress display: Set Nomber, set Nonu


Bracket matching highlighting



Match: Set Showmatch, set SM Cancel: Set NOSM


Auto Indent



Enable: Set AI disable: Set Noai


Highlight Search



Enabled: Set Hlsearch disabled: Set Nohlsearch


Syntax highlighting



Enable: Syntax on disable: syntax off


Ignore character case



Enabled: Set IC disabled: Set Noic


7. Save and exit



Exit does not save



: Q Exit without modification;: q! Discard all changes and exit the editing program;


Save exit



: W


Save to another file



W/path/to/somefile: Saves the text in the range to the specified file;





This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1855557



A quick start to the Vim editor on Linux


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.