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 vi/vim, a tool commonly used in linux

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

Share

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

This article is about how to use vi/vim, a tool commonly used in linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Why do you want to learn vim

Linux has a large number of configuration files, so there are many text processing tools for linux, including

Less vi head tail cat grep sed awk.... . When editing some configuration files and viewing logs, the commonly used tools are vim and less, and their shortcut keys are very similar, so it is necessary to learn how to use vim.

Expected reader

Operation and maintenance, developers, and people who want to advance to linux. If you learn bash, it is more convenient to know vim. It is impossible to go to windows to edit every time.

Start learning

Three modes of vi and their switching

General mode

Edit mode

Instruction column mode

Graph LRA [general mode]-- I graph LRA Oamp An ESC-- > B [edit mode] A-- colon / question mark / forward slash-- > C [instruction line mode] C-- ESC-- > A

Differences between commands that switch from general mode to editing mode

I insert at the current cursor

O insert another line

Insert a character after a

R replace input. This is useful in changing a single character because it replaces only one character and changes back to normal mode.

Common buttons and functions

The following instructions are case sensitive

Method of moving cursors (general mode)

↑→↓← moves up and down right and left, 5 ↓ moves down 5 lines

Ctrl + f turn the page down, turn half the page command is ctrl + d

Ctrl + b turn the page up, turn half the page command is ctrl + u

0 to the beginning of a line, to the end of a line is $

Gg to the beginning, G to the end, 3G to line 3

The cursor moves down one line, and the 3 cursor moves down 3 lines.

Delete, copy, paste (normal mode)

X deletes one character backward, X deletes one character forward, and 3x deletes 3 characters backward.

Dd deletes one line, 3dd deletes 3 lines

D $is deleted from the current cursor position to the end, and dG is deleted from the current position to the last line.

Yy copies the current line, 3yy copies 3 lines

P pastes the copied things on the next line, P pastes on the previous line

U withdraw operation

. Repeat the last operation

Find and replace (general mode)

/ word looks down for characters,? word looks up for characters

N repeats the previous search action, and after pressing n, it will continue to find the next matching character.

1By 5s _ sanri _ sanriq9420g search for lines 1 to 5 of the sanri string, replace it with 9420

1Gramming sanriq9420 sanri g search for the first to last line of the sanri string, replace it with 9420

Select text (General Mode)

V then use the direction key, which is equivalent to holding down the mouse and dragging in windows

The line over which the V cursor passes will select

Ctrl + v Select column mode

Selected text copy

D selected text deletion

To sum up.

Judging from the above command, in vi, some letters have a special meaning, such as

$stands for the end of the line, 0 for the beginning of the line, G for the last line, y for copy, d for delete line, x for delete character,

Numbers generally mean to repeat several times, while uppercase and lowercase generally mean just the opposite.

The shortcut to turn the page happens to be an abbreviation in English.

Ctrl + f front page

Ctrl + b back page

Ctrl + d down page

Ctrl + u up page

Save file as (command mode)

W save the file,: W! Force to write to the file (permission required)

: Q exit,: Q! Exit without saving

: wq saves and exits,: wq! Force save and exit (permission required)

1 filename 5w [write the data from the first line to the fifth line to a new file

:! Command executes the command and can view some information when editing the file without having to exit the file

: set nu displays line number,: set nonu does not display line number

Practice.

Wget 'http://linux.vbird.org/linux_basic/0310vi/man.config'

Use vi to open the man.config file in the directory

Set the line number in vi

Move to line 58 and move 40 characters to the right. What directory do you see in double quotes?

Move to the first line and search down for the string 'bzip2'. Which line is he on?

Next, I'm going to change the 'lowercase man string' between 50 and 100 lines to 'uppercase MAN string', and whether a selection needs to be modified, and how to issue instructions? How many man changes will occur in the last line if you keep pressing 'yearly' during the selection process?

After the revision, suddenly backed out, to be fully restored, what are the methods?

7. I want to copy the contents of the nine lines 65 to 73 (including MANPATH_MAP) and post them after the last line

I don't want the annotation material that begins with the # symbol between lines 21 and 42. How can I delete it?

Save this file as a man.test.config file name

Go to line 27 and delete 15 characters. What is the first word that appears?

Add a new line to the first line, and enter'I am a student...''in the line.

Save and leave!

The answers are as follows:

Vi man.config: set nu 58G 40 → / dir/bin/foo gg / bzip2 118lines 50100s/man/MAN/gc 25 changes, 23 lines change u, or exit without saving: Q! Navigate to 65 lines 65G, copy 9 lines of 9yy, locate the last line G, paste p: 21 man.test.config 42s / ^ # / / g / / inside it is possible to use regular: W man.test.config 27G 15x you navigate to the first line gg, I then enter: wq

Multi-file editing

Vi can be followed by multiple files, like this

Vi man.config test.txt

N Edit the next file

N Edit the previous file

Files displays all files currently being edited

Multi-window editing

Multiple files can be opened in the same window, using the: sp [filename] command, like this

: sp / etc/hosts

Each window has its own separate mode and command

[ctrl] + w + ↓ switch to the next window

[ctrl] + w + ↑ switch to the previous window

Edit the configuration file of vim ~ / .vimrc (simple configuration)

One configuration per line is a comment

You can use the command: set all to view all configuration items. The common configuration items are as follows:

Set nu sets line number

Set ts=4 setting indents 4 spaces when tab is pressed

When set tabstop=4 is set to display, a tab occupies the width of 4 spaces.

Set autoindent is automatically indented, which is useful for writing code

The above command has been set up to display, backspace and press tab are used for tabs. If you need to use spaces to represent tabs, you need to add the following configurations

Set expandtab uses spaces to indicate indentation, not tabs

When set softtabstop=4 backspace, if you encounter four consecutive spaces, delete it one time instead of deleting one by one.

``

Thank you for reading! This is the end of this article on "how to use vi/vim, a tool commonly used in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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