search
HomeOperation and MaintenanceLinux Operation and MaintenanceDetailed explanation of linux vi command
Detailed explanation of linux vi commandOct 28, 2019 pm 02:27 PM
linuxviDetailed explanation

Detailed explanation of linux vi command

Detailed explanation of linux vi command

Recommended: [Linux video tutorial]

vi editor It is the standard editor under all Unix and Linux systems. Its power is not inferior to any latest text editor. Here is an introduction to its usage and some instructions.

Since the vi editor is exactly the same for any version of Unix and Linux systems, you can learn more about it in any other place where vi is introduced. Vi is also the most basic text editor in Linux. After learning it, you will have no problem in the Linux world.

Detailed explanation of linux vi command

1. The basic concept of vi

Basically vi can be divided into three states, namely command mode (command mode), Insert mode(Insert mode) and bottom line mode(last line mode), the functions of each mode are distinguished as follows:

1 ) Command line mode command mode)

Control the movement of the screen cursor, the deletion of characters, words or lines, move and copy a section and enter Insert mode, or go to last line mode.

2) Insert mode

Only in Insert mode, text input can be done. Press the "ESC" key to return to the command line mode.

3) Bottom line mode (last line mode)

Save the file or exit vi, you can also set the editing environment, such as searching for strings, listing line numbers, etc.

However, generally when we use vi, we simplify vi into two modes, that is, the last line mode (last line mode) is also included in the command line mode (command mode).

2. Basic operations of vi

a) Enter vi

After entering vi and file name at the system prompt, enter vi full screen Editing screen:

$ vi myfile

But one thing to pay special attention to is that after you enter vi, you are in "command mode (command mode)", and you need to switch to "Insert mode (Insert mode)" before you can enter Word. People who use vi for the first time will want to use the up, down, left and right keys to move the cursor first. As a result, the computer keeps beeping, which makes them mad. So after entering vi, don't move around and switch to "Insert mode". !

b) Switch to Insert mode to edit the file

Click the letter "i" in "command mode" to enter "Insert mode", then you can start entering text.

c) Insert switching

You are currently in "Insert mode", you can only keep inputting text, if you find that you have entered the wrong word! If you want to use the cursor keys to move back and delete the word, you must first press the "ESC" key to go to "command mode (command mode)" and then delete the word.

d) Exit vi and save the file

In "command mode (command mode)", click the ":" colon key to enter "Last line mode", for example:

: w filename #(输入 「w filename」将文章以指定的文件名filename保存)
: wq #(输入「wq」,存盘并退出vi)
: q! #(输入q!, 不存盘强制退出vi)

3. Command mode function key

1). Press "

i

" to switch to insert mode Insert mode "insert mode", after pressing "i" to enter insert mode, the input file starts from the current position of the cursor;

After pressing "a" to enter insert mode, the input file starts from the current position of the cursor. Start inputting text at the next position;

After pressing "o" to enter insert mode, a new line is inserted, and text is entered starting from the beginning of the line.

2). Switch from insert mode to command line mode

Press the "ESC" key.

3). To move the cursor

vi can directly use the cursor on the keyboard to move up, down, left, and right, but regular vi uses lowercase English letters "h", "j", and "k" , "l", respectively controls the cursor to move one space to the left, down, up, and right.

  按「ctrl」+「b」:屏幕往"后"移动一页。
  按「ctrl」+「f」:屏幕往"前"移动一页。
  按「ctrl」+「u」:屏幕往"后"移动半页。
  按「ctrl」+「d」:屏幕往"前"移动半页。
  按数字「0」:移到文章的开头。
  按「G」:移动到文章的最后。
  按「$」:移动到光标所在行的"行尾"。
  按「^」:移动到光标所在行的"行首"
  按「w」:光标跳到下个字的开头
  按「e」:光标跳到下个字的字尾
  按「b」:光标回到上个字的开头
  按「#l」:光标移到该行的第#个位置,如:5l,56l。

4). Delete the text

  「x」:每按一次,删除光标所在位置的"后面"一个字符。
  「#x」:例如,「6x」表示删除光标所在位置的"后面"6个字符。
  「X」:大写的X,每按一次,删除光标所在位置的"前面"一个字符。
  「#X」:例如,「20X」表示删除光标所在位置的"前面"20个字符。
  「dd」:删除光标所在行。
  「#dd」:从光标所在行开始删除#行

5). Copy

 「yw」:将光标所在之处到字尾的字符复制到缓冲区中。
  「#yw」:复制#个字到缓冲区
  「yy」:复制光标所在行到缓冲区。
  「#yy」:例如,「6yy」表示拷贝从光标所在的该行"往下数"6行文字。
  「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与"y"有关的复制命令都必须与"p"配合才能完成复制与粘贴功能。

6). Replace

  「r」:替换光标所在处的字符。
  「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

7). Restore the last time Operation

「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次"u"可以执行多次回复。

8). Change

   「cw」:更改光标所在处的字到字尾处
  「c#w」:例如,「c3w」表示更改3个字

9). Jump to the specified line

  「ctrl」+「g」列出光标所在行的行号。
  「#G」:例如,「15G」,表示移动光标至文章的第15行行首。

4. Introduction to commands in Last line mode

Before using "last line mode", please remember to press the "ESC" key to confirm that you are in "command mode", and then press ":" Colon to enter "last line mode".

A) List line numbers

「set nu」:输入「set nu」后,会在文件中的每一行前面列出行号。

B) Jump to a certain line in the file

「#」:「#」号表示一个数字,在冒号后输入一个数字,再按回车键就会跳到该行了,如输入数字15,再回车,就会跳到文章的第15行。

C) Find characters

 「/关键字」:先按「/」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往后寻找到您要的关键字为止。
 「?关键字」:先按「?」键,再输入您想寻找的字符,如果第一次找的关键字不是您想要的,可以一直按「n」会往前寻找到您要的关键字为止。

D) Save the file

 「w」:在冒号输入字母「w」就可以将文件保存起来。

E) Leave vi

 「q」:按「q」就是退出,如果无法离开vi,可以在「q」后跟一个「!」强制离开vi。
 「qw」:一般建议离开时,搭配「w」一起使用,这样在退出的时候还可以保存文件。

5. vi command list
1. The following table lists the functions of some keys in command mode:

h  左移光标一个字符
l  右移光标一个字符
k  光标上移一行
j  光标下移一行
^  光标移动至行首
0  数字"0",光标移至文章的开头
G  光标移至文章的最后
$ 光标移动至行尾
Ctrl+f 向前翻屏
Ctrl+b 向后翻屏
Ctrl+d 向前翻半屏
Ctrl+u 向后翻半屏
i 在光标位置前插入字符
a 在光标所在位置的后一个字符开始增加
o 插入新的一行,从行首开始输入
ESC 从输入状态退至命令状态
x 删除光标后面的字符
#x 删除光标后的#个字符
X (大写X),删除光标前面的字符
#X 删除光标前面的#个字符
dd 删除光标所在的行
#dd 删除从光标所在行数的#行
yw 复制光标所在位置的一个字
#yw 复制光标所在位置的#个字
yy 复制光标所在位置的一行
#yy 复制从光标所在行数的#行
p 粘贴
u 取消操作
cw 更改光标所在位置的一个字
#cw 更改光标所在位置的#个字

2、下表列出行命令模式下的一些指令 

w filename 储存正在编辑的文件为filename
wq filename 储存正在编辑的文件为filename,并退出vi
q! 放弃所有修改,退出vi
set nu 显示行号
/或? 查找,在/后输入要查找的内容
n 与/或?一起使用,如果查找的内容不是想要找的关键字,按n或向后(与/联用)或向前(与?联用)继续查找,直到找到为止。

对于第一次用vi,有几点注意要提醒一下: 

1、 用vi打开文件后,是处于「命令行模式(command mode)」,您要切换到「插入模式(Insert mode)」才能够输入文字。切换方法:在「命令行模式(command mode)」下按一下字母「i」就可以进入「插入模式(Insert mode)」,这时候你就可以开始输入文字了。 

2、编辑好后,需从插入模式切换为命令行模式才能对文件进行保存,切换方法:按「ESC」键。 

3、保存并退出文件:在命令模式下输入:wq即可!(别忘了wq前面的:)

The above is the detailed content of Detailed explanation of linux vi command. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),