Linux Text editor Vim/vi detailed introduction

Source: Internet
Author: User
Tags linux text editor


Two: Text editor Vim / Vi (get up early and call Vi later upgraded version Vim)
vim / vi is a powerful full-screen text editor, the most commonly used text editor on linux / unix,
Its role is to create, edit, and display text files.
vim / vi has no menu, only commands ---- Official: www.vim.org
Hell
Three modes:
vi filename command mode
Press i, a, o to insert mode
: set nu edit mode (set line number, this is the edit mode, the beginning of the :)
Ranch
Common commands:
One group: Insert command
Command effect
a Append text after cursor
A Append text at the end of this line
i Insert text before the cursor
I insert text at the beginning of this line
o Insert new line under cursor
O insert new line on cursor
Two groups: positioning command
Command effect
h, left arrow
j 、 Down arrow key Move down one line
k 、 Up arrow key Move down one line
l, right arrow, move one character to the right
$ Move to end of line
0 Move to the beginning of the line (letter zero)
H Move to the top of the screen
M Move to the center of the screen
L Move to the bottom of the screen
Ranch
: set nu set line number
: set nonu cancel line number
gg to the first line
G to the last line
nG to nth line
: n to line n
Three groups: delete command
Command effect
x delete the character at the cursor
nx delete n characters after the cursor
dd deletes the cursor line, ndd deletes n lines
dG delete the contents of the cursor to the end
D delete from cursor to end of line
: n1, n2d delete the specified range of lines such as: 66,67d-delete 66-67 lines
Four groups: copy and cut commands
Command effect
yy, Y copy the current line
nyy, nY Copy the current n lines --- 5yy (copy 5 lines including the current line)
dd cut the current line
ndd cut n lines below the current line
p, P Paste under the current cursor line (p) or above the line (P)
Five groups: replace and cancel commands
Command effect
r replaces the character at the cursor
R Replace characters from the cursor position, press Esc to end
u Cancel the previous operation

Six: search and replace commands
Command effect
/ string Search forward for the specified string. The search is case-insensitive: set ic (execute the ceil before it is case-insensitive) --set noic close
--- For example: / leyangjun command mode, press n to search leyangjun in turn
n Search for the next occurrence of the specified character string
Ranch
:% s / old / new / g Full text replacement specified string (% s means full text replacement)
For example:% s / ftp / leyangjun / g replace all ftp with leyangjun in the full text, g means no confirmation is required when replacing
: n1, n2s / old / new / g Replace the specified string within a certain range
For example: 4,10 / leyangjun / ftp / g Replace all leyangjun in lines 4-10 with ftp
4,10 / leyangjun / ftp / c Confirmation required
Ranch
Save and exit: ----: wq is equivalent to shift + ZZ
Force save and exit: wq! (Only the owner / root of the file can operate)
Save as: w /root/service.bak Save as root
Ranch
Application example:
Import file contents: r file name (when you are editing a file, you can import the contents of b file into a file)
For example:: r / etc / leyangjun Import the leyangjun file into the contents of the currently edited file.
Ranch
Execute the command in vi :! command
For example: I am editing a file but now I want to see the contents of a file
:! ls / root-> prompt you to press enter and go back
Ranch
For example: combined with the above import, import the result of a command execution into a file
: r! date ---- Import dates into the file I am editing now
Ranch
Define shortcuts: map shortcuts trigger commands
Example:: map ^ P I # <ESC> ^ means a shortcut key Note that you must use ctrl + v + p to get ^ p
I # means insert a #, (I means enter insert mode)
: map ^ E [email protected] (press ctrl + v + E, there is a letter i) Shortcut for inserting mailbox
Cancellation: unmap ^ P (ctrl + v + p)
Ranch
Consecutive line comments: n1, n2s / ^ / # / g (/ ^ means the beginning of the line)
Example: 4,8s / ^ / # / g (4-8 lines plus # to comment out)
: n1, n2s / ^ / # // g
Example: 4,8s / ^ / # // g (remove the # of lines 4-8) The meaning of substitution is empty
: n1, n2s / ^ / \ / \ // g
Example: 4,8s / ^ / \ / \ // g (remove the comment symbol from line 4-8 //)
Hell
Replace (ab is recommended, more convenient than shortcut keys)
: ab mymail [email protected] (ab command can be understood as replacing a with b)
When you are finished, you can go back to your current edit text: you enter myemail-> Enter or space, you will find it replaced with [email protected]
unab myemail (undefine ab)
Hell

Hell
vi configuration key:
Change the configuration file of vi (after the modification, the next time you open the file, follow the configuration file):
~ home director into the tree home directory (eg / home / leyangjun)
~ / .vimrc Each user has their own vim configuration file, which is empty by default  Actual operation:
1: vi /root/.vimrc or vi ~ / .vimrc
Ranch
2: Edit
set nu (the line number will be next time you open each file)
ab lyj leyangjun 


Linux Text editor Vim/vi detailed introduction


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.