JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Summary of vim common commands

Author:JIYIK Last Updated:2025/04/08 Views:

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can remember these commands unconsciously.

I just started learning to use vim, and I tend to forget many commands after a while. I will paste the commonly used commands here, which can be used as a help manual. These commands are all taken from "The Basics of Linux Private Recipes by Bird Brother" and I hope it will be helpful to you.

Part 1: Description of buttons available in general mode, such as cursor movement, copy and paste, search and replace, etc.

h or the left arrow key (←) Move the cursor one character to the left
j or the down arrow key (↓) Move the cursor down one character
k or the up arrow key (↑) Move the cursor up one character
l or the right arrow key (→) Move the cursor one character to the right
If you put your right hand on the keyboard, you will find that hjkl is arranged together, so you can use these four buttons to move the cursor. If you want to move multiple times, for example, move down 30 lines, you can use the combination key "30j" or "30↓", that is, add the number of times you want to move (number), and then press the action!
[Ctrl] + [f] The screen moves down one page, equivalent to the [Page Down] button (commonly used)
[Ctrl] + [b] The screen moves up one page, equivalent to the [Page Up] button (commonly used)
[Ctrl] + [d] The screen moves half a page downward
[Ctrl] + [u] The screen moves half a page up
+ Move the cursor to the next non-space character
- Move the cursor to the previous column of non-space characters
n<space> The n stands for "number", for example, 20. If you press the space key after pressing a number, the cursor will move n characters to the right of the line. For example, 20<space> will move the cursor 20 characters backward.
0 or function key [Home] This is the number '0': move to the first character of this line (commonly used)
$ or function key [End] Move to the last character of this line (commonly used)
H Move the cursor to the first character of the top line of the screen
M Move the cursor to the first character of the line in the center of the screen
L Move the cursor to the first character of the bottom line of this screen
G Move to the last line of this file (commonly used)
nG n is a number. Move to the nth line of this file. For example, 20G will move to the 20th line of this file (can be used with :set nu)
gg Move to the first line of this file, which is equivalent to 1G! (Commonly used)
n<Enter> n is a number. The cursor moves down n lines (commonly used)
Search and Replace
/word Search for a string named word under the cursor. For example, to search for the string vbird in the file, just enter /vbird! (Common)
?word Search for a string named word above the cursor.
n The n is an English key. It means " repeat the previous search action ". For example, if we just executed /vbird to search for the string vbird, after pressing n, we will continue to search for the next string named vbird. If we execute ?vbird, then pressing n will continue to search for the string named vbird!
N This N is an English key. It is the opposite of n, and it performs the previous search action in the "reverse direction". For example, after /vbird, pressing N means searching for vbird in the "upward direction".
Using /word with n and N is very helpful! It allows you to find some of the keywords you searched for repeatedly!
:n1,n2s/word1/word2/g n1 and n2 are numbers. Search for the string word1 between lines n1 and n2, and replace it with word2! For example, search for vbird between lines 100 and 200 and replace it with VBIRD:
":100,200s/vbird/VBIRD/g". (Common)
:1,$s/word1/word2/g Search word1 string from the first line to the last line, and replace the string with word2! (Commonly used)
:1,$s/word1/word2/gc Search word1 string from the first line to the last line, and replace the string with word2! And display a prompt character before replacing to let the user confirm whether to replace! (Commonly used)
Delete, Copy and Paste
x, X In a line of text, x deletes one character backward (equivalent to the [del] key), and X deletes one character forward (equivalent to the [backspace] key) (commonly used)
nx n is a number, and it will delete n characters in a row. For example, I want to delete 10 characters in a row, "10x".
dd Delete the entire column where the cursor is located (commonly used)
ndd n is a number. Delete n columns below the cursor, for example, 20dd will delete 20 columns (commonly used)
d1G Delete all data from the cursor to the first row
dG Delete all data from the cursor to the last line
d$ Delete the cursor position to the last character of the line
d0 That is the number 0, delete the cursor position, to the first character of the line
yy Copy the row where the cursor is located (commonly used)
nyy n is a number. Copy the n columns below the cursor, for example, 20yy will copy 20 columns (commonly used)
y1G Copy all data from the cursor column to the first column
y Copy all data from the cursor column to the last column
y0 Copy all data from the character where the cursor is to the beginning of the line
y$ Copy all data from the character where the cursor is to the end of the line
p, P p 为将已复制的数据在光标下一行贴上,P 则为贴在游标上一行! 举例来说,我目前光标在第 20 行,且已经复制了 10 行数据。则按下 p 后, 那 10 行数据会贴在原本的 20 行之后,亦即由 21 行开始贴。但如果是按下 P 呢? 那么原本的第 20 行会被推到变成 30 行。 (常用)
J 将光标所在列与下一列的数据结合成同一列
c 重复删除多个数据,例如向下删除 10 行,[ 10cj ]
u 复原前一个动作。(常用)
[Ctrl]+r 重做上一个动作。(常用)
这个 u 与 [Ctrl]+r 是很常用的指令!一个是复原,另一个则是重做一次~ 利用这两个功能按键,你的编辑,嘿嘿!很快乐的啦!
. 不要怀疑!这就是小数点!意思是重复前一个动作的意思。 如果你想要重复删除、重复贴上等等动作,按下小数点『.』就好了! (常用)

第二部份:一般模式切换到编辑模式的可用的按钮说明

进入插入或取代的编辑模式
i, I 进入插入模式(Insert mode):
i 为『从目前光标所在处插入』, I 为『在目前所在行的第一个非空格符处开始插入』。 (常用)
a, A 进入插入模式(Insert mode):
a 为『从目前光标所在的下一个字符处开始插入』, A 为『从光标所在行的最后一个字符处开始插入』。(常用)
o, O 进入插入模式(Insert mode):
这是英文字母 o 的大小写。o 为『在目前光标所在的下一行处插入新的一行』; O 为在目前光标所在处的上一行插入新的一行!(常用)
r, R 进入取代模式(Replace mode):
r 只会取代光标所在的那一个字符一次;R会一直取代光标所在的文字,直到按下 ESC 为止;(常用)
上面这些按键中,在 vi 画面的左下角处会出现『--INSERT--』或『--REPLACE--』的字样。 由名称就知道该动作了吧!!特别注意的是,我们上面也提过了,你想要在档案里面输入字符时, 一定要在左下角处看到 INSERT 或 REPLACE 才能输入喔!
[Esc] 退出编辑模式,回到一般模式中(常用)

第三部份:一般模式切换到指令列模式的可用的按钮说明

指令列的储存、离开等指令
:w 将编辑的数据写入硬盘档案中(常用)
:w! 若文件属性为『只读』时,强制写入该档案。不过,到底能不能写入, 还是跟你对该档案的档案权限有关啊!
:q 离开 vi (常用)
:q! 若曾修改过档案,又不想储存,使用 ! 为强制离开不储存档案。
注意一下啊,那个惊叹号 (!) 在 vi 当中,常常具有『强制』的意思~
:wq 储存后离开,若为 :wq! 则为强制储存后离开 (常用)
ZZ 这是大写的 Z 喔!若档案没有更动,则不储存离开,若档案已经被更动过,则储存后离开!
:w [filename] 将编辑的数据储存成另一个档案(类似另存新档)
:r [filename] 在编辑的数据中,读入另一个档案的数据。亦即将 『filename』 这个档案内容加到游标所在行后面
:n1,n2 w [filename] 将 n1 到 n2 的内容储存成 filename 这个档案。
:! command 暂时离开 vi 到指令列模式下执行 command 的显示结果!例如
『:! ls /home』即可在 vi 当中察看 /home 底下以 ls 输出的档案信息!
vim 环境的变更
:set nu 显示行号,设定之后,会在每一行的前缀显示该行的行号
:set nonu 与 set nu 相反,为取消行号!

命令的总结摘自《鸟哥的linux私房菜基础篇》

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:https://www.jiyik.com/en/xwzj/opersys_10521.html

Related Articles

Connect to a remote MySQL database using the command line

Publish Date:2025/04/11 Views:183 Category:MySQL

Remote connectivity is required when we work in a distributed environment. This tutorial is a step-by-step guide on how to connect to a remote MySQL database using the command line. Connect to a remote MySQL database using the command line

Exit from the command line utility in PostgreSQL

Publish Date:2025/04/10 Views:128 Category:PostgreSQL

PSQL is a terminal-based front-end for the PostgreSQL database server. It uses either command-line arguments from an input file or from the command line, and is portable and easy to use. It also helps in writing scripts and letting other ta

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial