Getting Started with Linux: VI/VIM

A small game to practice vim keys: http://vim-adventures.com/

 
 
1. Introduction to vi and vim
 
Text editors are divided into:
(1) Based on graphical interface, such as gedit, geany, etc.;
(2) Based on the command line, such as nano, vi, joe, etc.;
Each text editor of the command line interface has its own shortcut keys, such as nano's save as CTRL+W, vim's save as :w;
A command-line-based text editor is necessary, because Linux's tty1~tty6 are all command-line interfaces and cannot open a graphical interface text editor;
 
vi is a text editor installed by default in every Linux Distribution, and the default editor of many software is vi, so learning vi is necessary;
vim is an advanced version of vi, vi is a text editor, and vim should be said to be a program editor, because like a general IDE, it can load different syntax highlighting according to the file name, that is, color distinction, such as java files will follow java syntax highlighting;
Let's write a "hello world" java program to see the syntax highlighting of vim;
 
It can be seen that vim has a good syntax checking function, because the keywords are marked with different colors;
 
 
vi has three modes:
(1) Normal mode: enter the default mode of vi, and can perform operations such as copying, pasting, and deleting;
(2) Edit mode: press 'i' from the normal mode to enter;
(3) Command line mode: Press ":", '?', '/' from the general mode to enter;
 
Note: If vim is installed, the vim editor is also used after entering vi. After entering alias, we see "alias vi='vim'";
 
 
2. vi/vim button
 
 

Common keys in " Normal Mode "

h

left

J

down

K

Improvement

l

To the right

20h

20 characters to the left

20j

20 lines down

20k

20 lines of improvement

20l

20 characters to the right

Page Up

page up

Page Down

page down

Home

Move the cursor to the far left of the current line

End

Move the cursor to the far right end of the current flight

n [ space ]

Move the cursor n characters to the right

N[Enter]

Cursor down N lines

G

Move the cursor to the last line

nG

Move the cursor to the nth line

gg or 1G

Move the cursor to the first line

/word

look down word

?word

look up word

n

If it is used after /word , it means to continue searching downwards;

If it is used after ?word , it means to continue searching upwards;

N

如果是/word后使用,则表示向上继续查找;

如果是?word后使用,则表示向下继续查找;

:n,m/old/new/g

从第n行到第m行中,将old替换为new

:n,$s/old/new/g

从第n行到最后一行中,将old替换为new

u

类似windows中的CTRL+Z,前一步

x

向后删除一个字符

X

向前删除一个字符

3x

向后删除3个字符

3X

向前删除3个字符

dd

删除当前行

3dd

删除当前行开始的3

yy

复制当前行

3yy

复制当前行开始的3

p

黏贴到当前光标的下一行

P

黏贴到当前光标的上一行

CTRL+r

重做前一个操作

.

重复前一个操作

 

命令行模式的常用按键

:w

保存

:q

退出vim

:wq

保存并退出

:wq!

(在可以转换权限的情况下)强制保存并退出

:q!

直接退出不保存

:w filename

另存为filename

:n,m w filename

将第n行到第m行另存为filename

:set nu

显示行号

:set nonu

不显示行号

:! command

暂时离开vim,并执行command,执行完后再进入vim

:r filename

filename文件的数据读入当前文件

:set all

显示当前vim的环境配置

 
三、vim的恢复机制
 
 
vim正在编辑某个文件时都会同时存在 .file.swp,此文件用来暂存,帮助恢复文件内容,当vim正常关闭此文件时,.filename.swp文件会消失;
 
 
我们看到图中说明了出现此界面的两个原因:
(1)多人同时编辑此文件:因为Linux是多用户的操作系统,因此可能两个人同时登陆并编辑此文件,如果A进入系统开始编辑1.txt文件,则会出现.1.txt.swp,当A还没编辑完但B也想进去编辑时,因为此文件的目录中存在.1.txt.swp,则就会出现上图的界面;
(2)非正常关闭文件;
 
图中最后一行说明了我们此时可以采取的几个动作:
(1)o:以只读方式打开;
(2)R:恢复,即从swp文件中恢复,但是swp文件不会随着vim关闭而删除,需要手动删除;
(3)d:删除swp文件;
(4)q:退出vim;
 
四、vim记录及默认配置文件
 
 
/etc/vimrc文件为全局vim配置文件;
 
1.如果我们用xiazdong账户使用vim后,在/home/xiazdong就会出现 .viminfo文件,此文件用来作为vim的日志,记录用户使用vim打开了什么文件,做了什么操作;
2.如果xiazdong账户想要配置vim的默认环境,则可以在/home/xiazdong创建.vimrc,并且配置一些特征,比如显示行号,语法检查等;
一般使用如下配置:

set hlsearch
set backspace=2
set autoindent
set nu
set ruler
set showmode
set bg=dark
syntax on

 
 
五、Visual Block功能
 
可以用于块复制;
(1)CTRL+v:开始复制块;
(3)y:复制块;
(4)p:黏贴块;
(5)d:删除块;
 
 
 
六、同时编辑多个文件
 
多文件编辑的好处是能够将一个文件的某些内容yy后,p到另一个文件中;
 
vim file1 file2 即可同时编辑两个文件,但是屏幕中同时只会出现一个文件内容,需要进行切换;
(1):n:向下切换;
(2):N:向上切换;
(3):files:列出当前编辑的所有文件;
 
七、多窗口编辑
 
如下图:一个界面能够显示多个文件内容;
 

 
(1) :sp :打开当前文件;
(2) :sp filename:打开filename文件;
 
(1)CTRL+w+↓:光标切换到下一个窗口;
(2)CTRL+w+↑:光标切换到上一个窗口;
(3):q:关闭光标所在的窗口;
 
 
八、编码问题
 
 
可能会出现乱码问题,解决方法: 保持终端的字符编码与文件的字符编码一致!
tty1~tty6不支持显示中文,因此在tty1~tty6中显示中文必出现乱码;
设置终端的字符编码:
 
编码转换命令:iconv
iconv -f 文件的原本编码 -t 文件的新编码 filename -o newfilename
比如有一个big5编码的文件:1.big5,要转换成utf8的文件:2.utf8,则: iconv -f big5 -t utf8 1.big5 -o 2.utf8

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326610329&siteId=291194637