Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Vim in linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Vim in linux". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use Vim in linux".

1. Vim mode

Normal mode (press Esc or Ctrl+ [enter) to display the file name in the lower left corner or empty

Insert mode (press I to enter) display in the lower left corner-- INSERT--

Visual mode (press v to enter) display in the lower left corner-VISUAL--

2. Open the file

# Open a single file

Vim file

# Open multiple files at the same time

Vim file1 file2..

# Open a new file in the vim window

: open [file]

[give an example]

# currently open 1.txt, made some edits but did not save

: open! Discard these changes and reopen the unmodified file

# currently open 1.txt, make some edits and save

Open 2.txt directly exits the editing of 1.txt and opens 2.txt editing directly, saving the step of quitting: wq and then re-vim 2.txt.

# Open a remote file, such as ftp or share folder

: e ftp://192.168.10.76/abc.txt

: e\ qadrive\ test\ 1.txt

# Open the file as read-only, but you can still use: wq! Write

Vim-R file

# the modification function is forcibly disabled and cannot be used: wq! Write

Vim-M file

3. Insert command

I insert before death in the current position

I insert at the beginning of the current line

An insert after the current location

An insert at the end of the current line

O insert a row after the current row

O insert a row before the current row

4. Find command

The simplest search

/ text look for text, press n to find the next one, and press N key to find the previous one.

? text to find text, reverse search, press n health check to find the next, press N key to find the previous one.

There are some special characters in vim that need to be escaped when looking up. * [] ^% /? ~ $

Set ignorecase ignores case lookups

Set noignorecase does not ignore case lookups

Quick search, you don't need to type characters by hand.

* look backward (down) for the word where the cursor is located

# look forward (above) for the word where the cursor is located

For the above two kinds of search, the continue search command of nQuery N is still applicable.

Precise search: matching word search

If there are these three words in the text

Hellohelloworldhellopython

Then I'll use / hello, and all three words will match.

Is there any way to achieve accurate search? You can use it.

/ hello\ >

Precise search: match the beginning and end of the line

# hello at the beginning of the line

/ ^ hello

# world is at the end of the line

/ world$

5. Replace command

~ reverse the case of cursor letters

R replace the current character with the written letter

R... Continuous substitution of letters

Cc replaces the entire line (that is, delete the current line and insert it on the next line)

Cw replaces a word (that is, delete a word and enters insert mode), as long as the cursor is in the first letter of the word (b positioning is available)

C (uppercase C) replace to the end of the line (unlike D, D deletes (cuts) to the end of the line, C deletes to the line bit and enters insert mode)

S/old/new/ replaces new with old, replacing the first match in the current line

S/old/new/g replaces new with old, replacing all matches on the current line

:% s/old/new/ replaces new with old, replacing the first match of all lines

:% s/old/new/g replaces new with old, replacing all matches of the entire file

: 10pr 20s / ^ / g add four spaces before each line from lines 10 to 20 for indentation.

Ddp swaps the line where the cursor is located and the line immediately below it.

6. Undo and redo

U undo (Undo)

U undo the operation on the whole row

Ctrl + r redo (Redo), that is, the undo of undo.

7. Delete command

It is important to note that vim does not simply delete the command, which you understand as more accurate.

Delete by character

X Delete the current character

3x delete the current character 3 times

X deletes the previous character of the current character.

3x deletes the current cursor three characters forward

Dl deletes the current character, dl=x

Dh deletes the previous character, X=dh

D deletes the current character to the end of the line. Dumped $

D $removes the current character to the end of the line

D ^ to the beginning of the line before deleting the current character

Delete by word

Dw deletes the current character to the end of the word

Daw deletes the word of the current character

Delete in behavior unit

Dd deletes the current line

Dj deletes the next line

Dk deletes the previous line

Dgg deletes the current line to the beginning of the document

D1G deletes the current line to the beginning of the document

DG deletes the current line to the end of the document

Kdgg deletes all rows before the current line (excluding the current line)

All rows after jdG deletes the current line (excluding the current line)

10d deletes the 10 lines at the beginning of the current line.

110d delete 1-10 lines

: 11 lines deleted 11 lines and all subsequent lines

: 1 ~ # delete all lines

J deletes the blank line between the two lines, which is actually merging the two lines.

8. Copy and paste

Use y replication in normal mode

The entire line where the yy copy cursor is located (3yy means copy 3 lines)

Copy y ^ to the beginning of the line, or y0. Does not contain the character where the cursor is located.

Copy y$ to the end of the line. Contains the character where the cursor is located.

Yw copies a word.

Y2W copy two words.

YG is copied to the end of the text.

Y1G is copied to the beginning of the text.

Use p paste in normal mode

P (lowercase): represents pasting behind the cursor (below, right)

P (uppercase): represents pasting in front of the cursor (above, left)

9. Cut and paste

Dd is actually a cut command to cut the current line.

Ddp cuts and pastes the current line to switch positions between the current line and the next line

Press v (verbatim) or V (line by line) to enter visual mode in normal mode

Then use the jklh command to move to select certain lines or characters, then press d to cut

Ndd cuts the n lines after the current line. You can paste the cut using the p command.

1. Cut 1-10 lines in 10 days. The cut content can be pasted with the p command.

: 1, 10 m 20 move lines 1-10 after line 20.

10. Exit Save

Wq saves and exits

ZZ save and exit

: q! Force exit and ignore all changes

: e! Discard all changes and open the original file.

ZZ save and exit

Sav (eas) new.txt is saved as a new file, exits editing of the original file and will not be saved

F (ile) new.txt opens a new file and does not save it, exits editing of the original file and will not save it

11. Move command

Move in characters

H move one character to the left

L move one character to the right

K move up one character

J move down one character

# [positioning characters] f and F

The first x character after fx finds the cursor

The third character with d after 3fd finds the cursor

F is the same as f, reverse search.

Move in units of behavior

# 10 refers to all numbers and can be specified at will

Move 10 characters to the left in 10 hours

10l move 10 characters to the right

10k move up 10 lines

10j move down 10 lines

Move $to the end of the line

3$ move to the end of the next 3 lines

Move in terms of words

W move forward one word (cursor stops at the beginning of the word)

B move back one word

E, same as w, but the cursor stops at the end of the word

Ge is the same as b, and the cursor stops at the end of the word.

Move by sentence

(move to the beginning of the sentence

) move to the end of the sentence

Jump to the beginning and end of the file

Gg moves to the header of the file. = [= = ``

G moves to the end of the file. =]]

Other methods of movement

Move ^ to the first non-white space character on the line.

Move 0 to the first character of the line (can be a space)

The use of named tags to jump, personally feel that this is very useful, because it can cross files.

Using ma, you can mark this as an and use'a to jump

Use: marks to view all tags

Use: delm! All tags can be deleted

When viewing the error log, the normal step is to vim open the file and then use shift+g to jump to the last line. Here is an easier operation to jump to the last line as soon as you open the file. Just add a + between the vim and the file.

Vim + you.log

For example, you can do this when you want to open a file and jump to a specified line immediately.

# Open the file and jump to line 20

Vim you.log + 20

When you use / search location jump or use: line number to make a precise jump, sometimes we want to return to the last location, how to achieve?

Simply use Ctrl+o to return to the previous location.

12. Typesetting function

Indent

: set shiftwidth? View indent valu

Set shiftwidth=4 sets the indent value to 4

# indentation is best written to the configuration file ~ / .vimrc

: set tabstop=4

: set softtabstop=4

: set shiftwidth=4

: set expandtab

Indent to the right

-the command sets the .vimrc file and makes sure that you do have tab in your file. If expendtab is turned on, tab will be expanded to spaces.

Syntax lists the grammar items that have been defined

Syntax clear clears the defined syntax rules

Syntax case match is case-sensitive, and int and Int will be regarded as different syntax elements

Syntax case ignore is case-independent, int and Int will be treated as the same syntax elements and use the same color scheme

The above is all the contents of the article "how to use Vim in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 261

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report