Vi & Vim in Linux


Vi & Vim in Linux

basic description about vi & vim

vi is Linux built in text editor

vim is the enhanced version to vi, can do program

three patterns by vi & vim

  • normal pattern 正常模式
    • can not edit in this pattern
  • edit pattern 编辑模式
  • cmd line pattern 命令行模式

operating process

Step 1

vim Hello.java

image-20220522170231266

by this command, we go to normal pattern, from our command line

image-20220522170425702

Step 2

type i # 摁下i

now we are in edit pattern

image-20220522170558960

we can write our file here

Step 3

After editing

we type esc first  # 摁下esc

now we leave the edit pattern and go to normal pattern now

image-20220522171432159

then type

:wq # write(save) and quit

image-20220522172138024

image-20220522171815264

Step 4

if you want edit this file again

vim Hello.java

image-20220522172027683

how to change vi & vim

image-20220522165131419

shortcut key words 快捷键

under normal pattern : copy and paste

  • copy current line : yy
  • paste : p
    • copy 5 line below current line : 5yy 包括复制当前行
  • delete current line : dd
    • delete 5 line below current line : 5dd 包括删除当前行

these are under normal pattern, if you are in edit pattern, it will just type yy in the file

search a word in the file

/hello
type enter # 摁下回车开始查找
type n # 下一个,查到最后一个单词则回到开头

set number for line

: set nu # 设置行号

image-20220522173207087

image-20220522173412258

: set nonu # 取消行号

go to the last line and first line

under normal pattern

G # go to last line
gg # go to first line

undo

at the normal pattern

u # undo

if do some editions in edit pattern, can type esc go to normal pattern, then type u to undo all the editions you did just now

move the cursor

at the normal pattern

20 shift g # move to the 20th line

有可能键盘因为摁完shift后变成中文输入法,可以采取的措施是打开大写输入


Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC