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

The use of VIM Editing in Linux

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the "use of VIM editors in Linux", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the use of VIM editors in Linux"!

Intimate contact with vim

Task 1: enter the current user's home directory, view the current work path, create a me.txt file with vim in the current directory, enter 3 lines of text, save and exit the vim editor. Finally, use cat to view the contents of the me.txt file.

[root@sky] # cd ~

[root@sky ~] # pwd

[root@sky ~] # vim me.txt

Click here.

Click here.

Press the I key to enter input mode, and then enter the following content welcome to vim hello vim goodbye press the Esc key in the upper left corner of the keyboard to return to the command mode of vim input: wq Save and exit vim Editor

View the contents of the file:

[root@sky ~] # cat me.txt

Task 2: empty all the files in the / tmp directory, use vi to create a file a.txt in the / tmp directory, enter 3 lines, then save and exit, and then use the cat command to view the contents of the file.

[root@sky ~] # rm-rf / tmp/*

[root@sky ~] # vim / tmp/a.txt

Press the I key to enter input mode

Welcome to vim editer

Thank you

Goodbye

Press the ESC key in the upper left corner of the keyboard to return to command mode

Enter: wq save exit. This is the last line mode of vi (also known as trailing mode).

[root@sky ~] # cat / tmp/a.txt

Advanced usage of vim

Opening of a file

1. Open it directly

Cd / tmp

Vim a.txt

two。 Jump directly to the end of the line after startup (+)

Vim + a.txt

3. Jump to the line containing keywords according to pattern matching (+ / pattern) at startup

Vim + / to a.txt

4.vim pattern classification? How to switch?

Command mode: press Esc to enter this mode to execute some commands that come with vim, such as I insert mode, yy copy, p paste, dd delete a line.

Edit mode: enter this mode with I, a, o in the command mode of vim to edit the body content of the file.

Trailing mode: use: or / enter this mode in vim command mode to execute display line number: set nu, save: W, save and exit: wq, do not save exit: Q and other tail mode commands.

Esc is used to switch from edit mode or trailing mode to command mode.

Write the first nine lines in the passwd file to the / tmp/pa.txt file, then open it with vim and perform the following command shortcut action.

Head-9 / etc/passwd > / tmp/pa.txt

Vim / tmp/pa.txt

5. Command mode shortcut cursor positioning

-- understand--

Words

Move w to the right of the cursor by pressing the word to the right of the cursor to the beginning of the next word and e to the end of the word

B: skip to the beginning of the word to the left of the current word

Character

Move the h cursor to the left ←

J cursor move Down ↓ k cursor move up ↑ l cursor move right →

Flip the screen (command mode)

Ctrl+f forward page, f is front

B turn the page after, b is back

DG deletes the line where the cursor is located to the end (the last line of the file)

Dgg deletes the line of the cursor to the first line (line 1 of the file)

U undo operation

-the beginning and the end of the line

G cursor moves to the trailing line of the text

Move the gg cursor to the beginning of the text line

0 or ^ or home cursor moves to the beginning of the line

Move the $or end cursor to the end of the line

How do I navigate to the specified line?

Trailing mode: 5 cursor to line 5

Command mode (copy, paste, delete)

Dd deletes (cuts) the line where the cursor is located

Ndd deletes (cuts) the line of the cursor and the following n lines, such as 3dd

Yy copy the line of the cursor

Nyy copies the line in which the cursor is located and the following n lines, such as 3yy

P paste the next line of the cursor

R replace the character where the cursor is located (once replaced)

R replacement mode, ESC exit replacement mode (continuous replacement)

D $delete the position of the cursor to the end of the line including the cursor is equivalent to D

Delete the content in front of the cursor until the beginning of the line does not include the cursor

Dw deletes the word where the cursor is located

3dw deletes the last three words from the beginning of the cursor

Delete delete characters (deleted from left to right in characters) are equivalent to x

2x Delete the last 2 characters of the cursor (inclusive)

2x delete the first 2 characters of the cursor

U undo previous operation

Ctrl+r anti-revocation

ZZ (command mode) save, equivalent to: wq

Find (command mode)

/ keyword such as / sbin, then use n to continue to find the next one, and N to continue to find the previous one

-Edit mode (input mode)

An insert after the cursor position

I insert before the location of the cursor

O insert another line on the next line of the cursor.

An insert at the end of the line where the cursor is located

I insert at the beginning of the line where the cursor is located

O insert one line above the line of the cursor

Trailing mode (last line mode)

Save exit (w/q/x)

6 the cursor is positioned on line 6

: Q exit normally

: q! Forced exit

W save

Wq saves and exits

: wq! Force save and exit

: X save and exit

: 10pr 13d delete 10-13 lines

., $d means delete from the location of the cursor to the end (including the line where the cursor is located)

., + 5d means to delete 5 consecutive lines from the current line

Delete the line containing # for example, delete the line containing root: g/root/d

Replace substitute:s/This/THIS/ replace the This on the line of the cursor with THIS, replacing only the first one found

S/This/THIS/g replaces all This on the line of the cursor with THIS

:% s/This/THIS/g replaces the full text This with THIS

2s/This/THIS/g replaces the whole line of line 2 containing This with THIS

: 2Jar 3sThisqTHISUnip g replace the whole line of lines 2-3 containing This with THIS

:% s/This/THIS/gi replaces the full text This with THIS ignoring case

What vim must be proficient in:

1. Create a file with vim, enter the contents, and save the exit.

2. Yy copy, p paste, d delete, u undo, cursor positioning (arrow keys, 0, ^, $, G, gg)

3. / find, s replace

4. Display line number: set nu

Invoke external commands

:! date View system time

:! ls / home temporarily executes the specified command

R / etc/passd reads the contents of / etc/passwd into the current text

W / opt/a.txt saves the contents of the current text to / opt/a.txt

:., + 3w > > / opt/a.txt appends from the current line, the last three lines of the current line to / opt/a.txt

: 10pr 13w > > / opt/a.txt

Split screen (ctrl+w)

Sp/vsp

Undo u

Ctrl + r

Set nu sets line number

Set number sets line number

Set list displays special characters

Set nolist cancels display of special characters

Set ic (abbreviation for ignorecase) ignores case

Set noic (abbreviation for noignorecase)

Set all displays the contents of all current settings

Set autoindent (ai) automatic indentation

Set hlsearch setting to find highlight

Set nohlsearch cancels finding highlight

Syntax on setting syntax highlight

Syntax off turn off syntax highlighting

Visual mode

Visual block (ctrl+v)

Use the cursor to locate the column where you want to insert comments-- > ctrl+v-"- > I--" #-- "esc

Visual mode (v)

Visible line (V)

Multi-document editing capabilities of the vim editor:

Click here.

Click here.

Enter the / tmp directory to edit multiple files in a split horizon window

Cd / tmp vi-o (lowercase o) a.txt b.txt c.txt d.txt

After ctrl+w lets go, use the ↑↓← → key to switch in the file window.

Enter the line of ni hao in a.txt, then copy and paste

Move the cursor into the b.txt, c.txt, d.txt files, respectively, and paste

Save these files in turn with: wq

View the contents of a file with nl

Multi-document editing capabilities of the vim editor:

Click here.

Click here.

Enter the / tmp directory to split the window vertically to edit multiple files

Cd / tmp vi-O (capital O) a.txt b.txt c.txt d.txt

After ctrl+w lets go, use the ↑↓← → key to switch in the file window.

Enter the line of hello in a.txt, then copy and paste

Move the cursor into the b.txt, c.txt, d.txt files, respectively, and paste

Save these files in turn with: wq

View the contents of a file with nl

Click here.

Perform the following tasks

1. Write the first five lines in the passwd file to the / tmp/pk.txt file. Use the nl command to view the contents of the file.

2. Open the / tmp/pk.txt file with vim, display the line number with: set nu, then copy the first line with yy, move the cursor to the last line of the file with G, and paste it with p. Move the cursor to line 3 with: 3 and delete line 3 with dd.

3. Replace all sbin in the document with SBIN with:% s/sbin/SBIN/g. Delete the contents of the file that contain the root keyword with: g/root/d.

Custom definition

/ etc/vimrc global configuration file takes effect for all users

Set nu

Set ic

Syntax on

Set ai

Set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936 sets Vim to support character encoding. Utf-8 indicates that it supports Chinese.

~ / .local vimrc profile is valid for the current user

Vim / home/redhat/.vimrc

Garbled code problem

1. You can view the file encoding directly in Vim: set fileencoding

two。 If you just want to view files in other encoding formats or want to solve the problem of garbled viewing files with Vim, you can use the

Add the following to the vimrc file: set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936

Let vim automatically recognize file encodings (can automatically identify UTF-8 or GBK encoded files)

In fact, it is an attempt according to the list of codes provided by fileencodings, and if you can't find a suitable code, open it with latin-1 (ASCII) code.

File coding conversion mode

1. Convert file encoding directly in Vim, such as converting a file to utf-8 format: set fileencoding=utf-8

2. Iconv conversion. The command format of iconv is as follows:

Iconv-f encoding-t encoding inputfile

Iconv-f GBK-t UTF-8 abc-o / home/abc.txt converts GBK-encoded files to UTF-8

The default character set under windows is GBK linux. The default character set is UTF-8.

Thank you for your reading, the above is the content of "the use of VIM editors in Linux". After the study of this article, I believe you have a deeper understanding of the use of VIM editors in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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