Real high-level usage of vim (visual mode, last line mode, edit mode)

Real high-level usage of vim (visual mode, last line mode, edit mode)

As a powerful, reliable and efficient text editor, vim is deeply loved by users who use Linux system. It is no exaggeration to say that they will not use vim text editor. Even if sed, awk and grep swordsmen use it again 6, they are helpless in the face of massive text content.

This article aims to reveal the mysterious veil of vim. Common usage can not improve the efficiency of text editing. If you want to use it, use high-level skills, right. Therefore, this paper does not discuss the history of vim and its basic usage, but only discusses high-order techniques. Therefore, students without VIM use foundation can quit (mercilessly persuade them to quit!!!).

Firstly, there are six modes of vim:

1, Normal mode

That is, the mode entered when opening a text with vim. This mode can also be called the default mode, the basic mode, and any other mode can return to the normal mode through esc key.

In this mode, the keys that can be used on the keyboard include: v, p, x, cc, d, dd, i, o, R, R, etc. the specific usage is the basic knowledge. Please Baidu yourself.

2, Command mode

Enter "/" in the normal mode to enter the command line mode, where you can search, replace, etc. This mode is mainly aimed at text content.

3, Last line mode

Enter ":" in the normal mode. This mode is called the last line mode. You can exit VIM, set VIM, and enter the internal interactive commands of vim.

4, Insert mode

This mode is relatively normal. The text document enters the editable state through six letters a, i, o (three pairs of uppercase and lowercase),

5, Visual mode (also called view mode)

This mode is aimed at block editing, that is, selecting a block area to edit text continuously, quickly and efficiently.

6, Replacement mode

Quickly replace some contents of text documents. The replacement method is controllable and customizable.

 

The high-order usage of vim mainly focuses on the above-mentioned two, three, five and six philosophies. Therefore, it is divided according to the mode and explained in turn. Explain the use case and use Anaconda KS, the most common file in the root directory of centos system CFG to explain.

The contents of the document are as follows:

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$3Y0z0FD29vhUy/Ed$PRmmeqp284stUb5548cvv9V.1JTgtwSsx4wSi5wicLri1tBIgw15wyhXdLCnoB7tv/7a38OpN2ybou/VNeTHP/
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=2048
part pv.198 --fstype="lvmpv" --ondisk=sda --size=100351
volgroup centos --pesize=4096 pv.198
logvol /home  --fstype="xfs" --size=4092 --name=home --vgname=centos
logvol /  --fstype="xfs" --size=61440 --name=root --vgname=centos
logvol /var  --fstype="xfs" --size=30720 --name=var --vgname=centos
logvol swap  --fstype="swap" --size=4096 --name=swap --vgname=centos

%packages
@^minimal
@core

%end

%addon com_redhat_kdump --disable --reserve-mb='128M'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

First, after vim opens the file, it enters the normal mode, and ctrl +g can see the basic information of the document

As shown above, my cursor stops at the first letter of line 49, and the file name opened by vim is Anaconda KS CFG, 53 lines in total, and the cursor stays at 92% of the text document. The file has not been Modified. If it has been Modified but has not been saved and exited, ctrl+g will display like this (with [Modified]).

(1) Normal mode - > visual mode (view mode)

The switching mode is V or V or ctrl+v. here, V, V and ctrl+v are the three states of the visual mode, V is the basic mode of the visual mode (the effect of row or block mode can be obtained, which is more flexible in use), and V is the row visual mode of the visual mode (in rows), ctrl+v is the block visual mode of the visual mode (the block is the unit, and the area needs to be defined). After entering the visual mode, copy, paste, delete and cursor positioning can still be used. Specifically, y, p, d, h, j, k and l can be used. Therefore, we can achieve many desired results with these keys, not only these keys, We also have shortcut keys to use!!!!! That is, ve, vw, vb.

(1)v

Of course, the first mock exam is to be a gangster if we break away from other modes. For example, we need to copy the version in the first line, excluding the # number in front of it. Paste the word into the next line. Our key sequence should be ggwveyo esc p. Here, we use the shortcut key of ve combination.

ve means continuous ew from the cursor to the end of the word, that is, continuous selection, taking the word as the unit. Note that spaces are not included. vw is the same as ve, except that it includes spaces, which means that spaces are also a unit.

vb is backward selection, and multiple b is multiple backward selection.

vc and vs delete the letter of the cursor and enter the editing mode. If the line is empty, delete the empty line and insert edit at the beginning of the next line. Here we need to mention cc. In the normal mode, this combination means to delete the line where the cursor is located and enter the edit mode, which is simply line rewriting,

Equal to dd + o

(2) Big v

Still taking the example file as an example, what should I do if I want to copy these two lines in the file?

network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
       network  --hostname=localhost.localdomain

The first idea is how to quickly locate these two lines, then select these two lines, copy and paste them in the appropriate place.

Location method: enter network after / and press enter. At this point, you should have located these two lines.

Vjyy this combined command can copy these two lines. At this point, move the cursor to the end of the file and p you can see the copied two lines.

The function of big v is to select lines by express delivery. That's all. Note that it is selected by behavior unit. At this time, even if hl is used to adjust the range, it is still selected by row.

(3)Ctrl +v

There's nothing to say about this. For block selection, you can use webhjkl these buttons to quickly select.

 

Summary: visual mode -- > insert mode, please use vc, vs. if you need to quickly copy the whole text, the command should be: ggvG+end+yy,

ve, vw and vb are selected according to words (usually w, e and b are required. For example, a combination like wve is to select one word, wvee, select two words, and calculate one word with special symbols). Follow up processing and think by yourself.

Unfinished to be continued!!!

 

 

 

 

 

 

 

 

Keywords: Linux shell vim IDE

Added by nonso on Sat, 12 Feb 2022 01:58:38 +0200