Vim编辑器是Vi的升级版,在选择最小化安装Linux系统,默认没有安装Vim工具。

一、安装Vim

1、查询安装包

[root@www ~]# yum search vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
========================================= N/S Matched: vim =========================================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System
vim-common.x86_64 : The common files needed by any version of the VIM editor
vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements
vim-filesystem.x86_64 : VIM filesystem layout
vim-minimal.x86_64 : A minimal version of the VIM editor

  Name and summary matches only, use "search all" for everything.

2、安装Vim

[root@www ~]# yum -y install vim-common vim-enhanced


二、vim的使用

1、vim的一般模式

j
向下
k向上
n + j
向下跳到第n行
n + k向上跳到第n行
gg顶部
底部
ctrl + f 
向下翻一页
ctrl + b向上翻一页
shift + $ 光标移动到行尾
shift + ^ 或者 0
光标移动到行首
数字n + G 
移动到指定的第n行

dd 
剪切光标所在行
p
粘贴到下一行
P
粘贴到上一行
D剪切当前行光标所在后面的内容
P
粘贴在光标
P粘贴在光标后
u恢复上一步操作
ctrl + r   撤销上一步操作
X向前删除一个字符
x
向后删除一个字符
数字+x
向后删除多个字符
数字+X向前删除多个字符
yy
复制当前行

向下粘贴yy复制的行
P向上粘贴(大写P)
2+yy
向下复制两行
v
进入可视化模式


2、vim的编辑模式

a
光标移动到当前后一个字符进入编辑模式
A光标移动到行尾进入编辑模式
i在光标当前位置进入编辑模式
I光标移动到行首进入编辑模式
o在当前行下另起一行
O在当前行上另起一行


3、vim的命令模式

在一般模式下使用”:“或”/“显示该模式在左下角

/word搜索字符,n移动到下一个,N移动到上一个
:%,$s/word/new word/g搜索第一行到最后一行的word并替换为new word,加g表示全部替换
:%,$s/word/new word/gc加c表示逐个询问替换
:1,$s/\/etc\//111111/g 
搜索包含特殊字符/使用\转意
:noh取消高亮
:set nu
设置行号
:set nonu取消行号
:sh跳出vim回到shell,exit返回vim
:e!
撤销回到初始状态
vim +8 /etc/passwd打开并定位到第8行
:X
使用vim工具设置密码,设置空密码取消密码

Warning: Using a weak encryption method; see :help 'cm'
Enter encryption key: ***
Enter same key again: ***
: ! ls  /在vim下使用shell命令

bin   dev    disk6  home  lib64       media  opt   root  selinux  sys  usr
boot  disk1  etc    lib   lost+found  mnt    proc  sbin  srv      tmp  var