Learn a little shell every day: Use vi and vim commands

Common operations of vi

Enter vi

[root@hadoop-master test]# vi vi_test.txt 

Use the above command to enter the content page of vi_test.txt

#!/usr/bin/env bash
echo "服务器开始部署服务"
projectname="demo"
#打开文件所属的目录,不然远程执行会找不到当前目录
cd /usr/local/test
#新的jar包会当成参数传过来
newJar=$1
echo "新的jar为:$newJar"
#如果新的jar包为空则退出
if [ -z "$newJar" ]; then
 echo "新的jar不能为空"
 exit 0
fi

Command mode and Insert mode switch

After opening the vi command, enter the command mode by default, press the letter [i] or [Insert] key to enter the edit mode.
Edit mode
There will be a INSERTmark under the edit mode, you can modify the file content in the edit mode, and then press [Esc] to enter Command mode.

Save and exit

In command mode, press [:] colon to enter [Last line mode] to
save: :w(After entering the command, press [Enter] to execute the command)
Exit: :q
save and exit: :wq
save and force exit::wq!

Command mode (emphasis)

Enter Insert mode

Press [i] to enter insert mode, the cursor starts from the current position
Insert mode 1
Press [a] to enter insert mode, the cursor starts from the next character at the current position

Insert mode 2
Press [o] to enter the insert mode, the cursor is to re-insert a line below the current position, and the cursor starts from the beginning of the line
Insert mode 3

Move the cursor

Left: ⬅, [h]
Right: ➡, [l]
Down: ⬇, [j]
Up: ⬆, [k]

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

Delete text

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

copy

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

Find

/pattern  从光标开始处向文件尾搜索pattern
?pattern  从光标开始处向文件首搜索pattern

Character replacement

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

String replacement

:1,$s/oldstr/newstr/g        在全文范围用newstr替换oldstr
:n,ms/oldstr/newstr/g        在第n行到第m行内用newstr替换oldstr

Show line number

[:set number]   或者 [:set nu]       显示行号
[:set nonumber] 或者 [:set noun]     不显示行号

How to quickly empty file content

true  "" > fileName

reference:

Detailed explanation of vi commands: https://www.cnblogs.com/ovliverlin/articles/1162430.html
Summary of vi commands: https://www.cnblogs.com/fwl8888/p/9416375.html

Guess you like

Origin blog.csdn.net/u011047968/article/details/108121363