One article completely solves vi/vim editor operation

Table of contents

1. Basic operation

1.1 Three modes of vi

1.2 Insert and move cursor commands

1.3 Delete command

1.4 Revocation order

1.5 Repeat command

1.6 Copy Cut Paste

1.7 Region Replacement Search

2. Advanced Operations

2.1 Copy n lines from one file to another

2.2 Annotations and explanations

2.3 Quickly switch between viewing compiled warnings/errors and editing

3. Modify the configuration


References and more content: One article completely solves vi/vim editor operation

1. Basic operation

1.1 Three modes of vi

1. Command mode

2. Edit mode

3. Last line mode (in command mode, press ":" to enter last line mode)

By default, the VI editor is in the command mode, and you need to enter the editing mode when you need to write something in it

command mode to edit mode: insert command i, append command a, open command o, modify command c, replace command r, replace command s

Edit mode to command mode: Esc

Exit process:
1. Enter the command mode
2. Enter the last line mode
3. Enter the following content in the last line mode, corresponding to the corresponding operation

【:w】 保存文件
【:w!】 若文件为只读,强制保存文件
【:q】 离开vi
【:q!】 不保存强制离开vi
【:wq】 保存后离开
【:wq!】 强制保存后离开
【:! command】 暂时离开vi到命令行下执行一个命令后的显示结果
【:set nu】 显示行号
【:set nonu】 取消显示行号
【:w newfile】 另存为

1.2 Insert and move cursor commands

i:插入光标前一个字符
I:插入行首
a:插入光标后一个字符
A:插入行末
o:向下新开一行,插入行首
O:向上新开一行,插入行首
移动光标
h:左移     
j:下移        
k:上移      
l:右移      
M:光标移动中间行      
L:光标移动到屏幕最后一行行首      
G:移动到指定行,行号 -G 在命令行中输入 vi +26 samp.txt 命令直接打开文件到达 26 行,在 vi 编辑器中也可以输入 :26 跳转到 26 行       
{:按段移动,上移      
}:按段移动,下移     
Ctr-d:向下翻半屏     
Ctr-u:向上翻半屏     
gg:光标移动文件开头      
G:光标移动文件末尾     

1.3 Delete command

常用:
x    删除光标后一个字符,相当于del
X    删除光标前一个字符,相当于Backspace
dd   删除光标所在行
ndd  删除指定的行数
:n1,n2d  删除n1到n2行

不常用:
D  删除光标后本行所有的内容,包括光标所在字符
do 删至行首
d$ 删至行尾

1.4 Revocation order

u:一步一步撤销
ctr-r:反撤销

1.5 Repeat command

. 这是个点,重复上一次操作的命令
文本行移动
>>:文本行右移
<<:文本行左移

1.6 Copy Cut Paste

可视模式(选择文本)
v 按字符移动,选中文本
V 按行移动
ctrl+v 按列选择

y 选择了某一块后,复制到缓冲区待用
yy 复制当前行
nyy 复制n行
yG 复制到最后一行

p:在光标所在位置向下新开辟一行,粘贴,大写表示在光标之上

所有的y换成d,就是把复制变成了剪切

不常用:
y^ 复制光标位置到行头内容
y$ 复制光标位置到行尾内容

1.7 Region Replacement Search

/string  向光标之下寻找一个名称为string字符串

?string   向光标之上寻找一个名称为string字符串

n  正向查找,搜索出的string,可以理解成next

N  反向查找,搜索出的string,可以理解成Not next

n是从上到下,N是从下到上

:n1,n2s/string1/string2/g  这里的n1是查找的开始行数,n2是查找结束的行数。【:2,7s/ddd/fff/g】在第2行,第7行之间,将ddd替换成fff

:1,$s/string1/string2/g   从第一行到最后一行寻找 string1 字符串,并将该字符串替换为 string2 !(常用)

:1,$s/string1/string2/gc  从第一行到最后一行寻找 string1 字符串,并将该字符串替换为 string2 !且在替换前显示提示字符给用户确认 (confirm) 是否需要替换!(常用)

:s/vivian/sky/     替换当前行第一个 vivian 为 sky
:s/vivian/sky/g     替换当前行所有 vivian 为 sky

:n,$s/vivian/sky/   替换第 n 行开始到最后一行中每一行的第一个 vivian 为 sky
:n,$s/vivian/sky/g   替换第 n 行开始到最后一行中每一行所有 vivian 为 sky
n 为数字,若 n 为 .,表示从当前行开始到最后一行

:%s/vivian/sky/(等同于 :g/vivian/s//sky/) 替换每一行的第一个 vivian 为 sky
:%s/vivian/sky/g(等同于 :g/vivian/s//sky/g) 替换每一行中所有 vivian 为 sky

:s#vivian/#sky/# 替换当前行第一个 vivian/ 为 sky/
:%s+/oradata/apras/+/user01/apras1+ (使用+ 来 替换 / ): /oradata/apras/替换成/user01/apras1/

不加 g,表示只对搜索字符串的首次出现进行替换;

 g放在命令末尾,表示对搜索字符串的每次出现进行替换;

g 放在命令开头,表示对正文中所有包含搜索字符串的行进行替换操作

2. Advanced Operations

2.1 Copy n lines from one file to another

1) Open the source file, position the cursor on the first line to be copied (the line where the cursor is located is the first line to be copied), and input "anyy

Among them, " is the quotation mark, a is the variable name, other letters or numbers can be used, but there can only be one character, n is the number of lines to be copied, and yy is the copy command.

2) Open the target file, position the cursor at the paste position (insert from the next line of the cursor), input "ap

Among them, " is the quotation mark, a is the variable name, which is consistent with the variable name used when copying, and p is the zh paste command.

2.2 Annotations and explanations

Note: use batch replacement

:5,10s/^/\/\//g 在5到10行首插入//

Comment

1) Batch replacement

:5,10s/^\/\///g

2) ctrl+v select by column and press dd

2.3 Quickly switch between viewing compiled warnings/errors and editing

It's not difficult, it's just a trick

For example, after editing a program in vim test.c, exit and compile with the gcc -o test.o test.c command. If there are warnings or errors, you need to use the vim test.c command to modify it. It is not very troublesome, but it is not elegant enough .

In fact, after you edit the code, you only need to press esc to enter the command mode and output: !gcc -o test.o test.c to realize compilation. Compilation warnings and errors will be displayed. At this time, vim will not exit, and press enter to return to vim.

3. Modify the configuration

Modify the current user configuration: vim ~/.vimrc only takes effect for the current user

Modify the global configuration: sudo vim /etc/vim/vimrc or sudo /etc/vimrc, effective for all users

set nu                      " 显示行号
set tabstop=4               " 设置软制表符宽度为4
set softtabstop=4           " 设置软制表符宽度为4
set shiftwidth=4            " 设置缩进的空格数为4
set autoindent              " 设置自动缩进:即每行的缩进值与上一行相等
set cindent                 " 使用 C/C++ 语言的自动缩进方式
set cursorline              " 突出显示当前行
set expandtab               " 空格代替制表符
set showmatch               " 光标遇到圆括号、方括号、大括号时,自动高亮对应的另一个圆括号、方括号和大括号
set ruler                   " 在状态栏显示光标的当前位置(位于哪一行哪一列)
 
set guifont=Consolas:h15    " 设置字体和字体大小
colorscheme molokai         " 设置主题为molokai
 
set nobackup                " 取消备份文件
                            " 默认情况下,文件保存时,会额外创建一个备份文件,它的文件名是在原文件名的末尾,再添加一个波浪号~
setlocal noswapfile         " 不创建交换文件。交换文件主要用于系统崩溃时恢复文件,文件名的开头是.、结尾是.swp
set noundofile              " 取消生成un文件
 
set hlsearch                " 设置高亮显示搜索字符串
set showmode                " 在底部显示,当前处于命令模式还是插入模式
set showcmd                 " 命令模式下,在底部显示,当前键入的指令。比如输入快捷键将在底部显示具体命令
set t_Co=256                " 启用256色
set noerrorbells            " 出错时不要发出响声
" 高亮显示
syntax on
syntax enable
 
 

Guess you like

Origin blog.csdn.net/freestep96/article/details/127301088