1.vim 编辑器介绍
每一个系统管理员都有一个偏爱的文本编辑器。有的管理员偏爱gedit,有的偏爱nano,甚至有的偏爱emacs。即使已经有偏爱的编辑器,了解vim还是有必要的。因为vim可以安装在任何系统。
vim版本,Linux 系统中可以安装vim三个不同版本:
- vim-minimal:只提供vi和相关命令。
- vim-enhanced:提供vim命令,提供语法高亮,拼写检查等特性。
- vim-X11:提供gvim,图形界面下的vim。gvim包涵菜单栏。当用户不知道命令的时候,可以通过菜单操作。
# 安装 vim 工具 [fengkai@fengkai ~ 18:01:40]$ su 密码: [root@fengkai fengkai 18:02:13]# yum install vim 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误" One of the configured repositories failed (未知), and yum doesn't have enough cached data to continue. At this point the only safe thing yum can do is fail. There are a few ways to work "fix" this: 1. Contact the upstream for the repository and get them to fix the problem. 2. Reconfigure the baseurl/etc. for the repository, to point to a working upstream. This is most often useful if you are using a newer distribution release than is supported by the repository (and the packages for the previous distribution release still work). 3. Run the command with the repository temporarily disabled yum --disablerepo=<repoid> ... 4. Disable the repository permanently, so yum won't use it by default. Yum will then just ignore the repository until you permanently enable it again or use --enablerepo for temporary usage: yum-config-manager --disable <repoid> or subscription-manager repos --disable=<repoid> 5. Configure the failing repository to be skipped, if it is unavailable. Note that yum will try to contact the repo. when it runs most commands, so will have to try and fail each time (and thus. yum will be be much slower). If it is a very temporary problem though, this is often a nice compromise: yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true Cannot find a valid baseurl for repo: base/7/x86_642.vim 模式
- command 模式:该模式下键盘中的字母有特殊含义。例如G,跳转到最后一行;gg,跳转到第一行。
- edit 模式:在
command模式下按i,进入该模式。在该模式可以输入内容。在该模式下按Esc返回command模式。 - extended command 模式:在
command模式下按:,进入该模式。在该模式下,可以保存文件(:w),退出编辑(:q),强制退出编辑(:q!),报错并退出(:wq)。 - visual edit 模式:在
command模式下按 v(单个字符选中)、V(整行选中)或者ctrl+v(按矩形选中),进入该模式。在该模式下,用于选中文件,随后可以复制或删除选中的内容。
[root@fengkai fengkai 18:02:26]# cp /etc/profile ./profile.sh [root@fengkai fengkai 18:03:32]# ls profile.sh 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@fengkai fengkai 18:03:35]# vim ./profile.sh3.命令行模式快捷键
定位文件内容:
w,向后1个word(光标在单词开头);e,向后1个word(光标在单词末尾)b,向前1个word^或0,行首$,行尾(,上一个句子头部;),下一个句子尾部。{,上一个自然段;},下一个自然段。gg,第一行;G,最后一行。
less 使用g,第一行;G,最后一行。
进入插入模式:
- i ,当前位置插入。(insert)
- I,行首插入。
- a,当前位置追加。(append)
- A,行末追加。
- o,向下新开一个空白行。
- O,向上新开一个空白行。
复制:yy,复制1行,相当于y1y。以此类推 y2y y2w y2b。
粘贴:p 当前位置后粘贴,P 当前位置前粘贴
删除:
- x,删除当前单个字符。5x,删除当前字符和后续4个字符。
- X,删除前一单个字符。
- dw,删除1个word,以此类推 d5w d3b d2d d$。
- D,从当前删除到最后,相当于d$。
回退:
- u,撤销上一个操作。
- U,撤销整行修改。
修改,相当于删除后插入:
- s,删除当前字符,并进入编辑模式。
- S,删除当前行,并进入编辑模式。
- c,代表替换动作,例如cw,删除1个word,并进入编辑模式。以此类推 c5w。
- C,从当前删除到最后,并进入编辑模式。
替换:
- r,替换单个字符后回退到命令行模式。
- R,替换模式,一直替换直到按
Ese返回命令行模式。
4.示例
4.1示例1
1.使用vim ./profile.sh打开文件。
# /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } ·····2.根据任务要求,多次重复此文本输入循环:
- 使用方向键定位光标。
- 按i键进入插入模式。
- 输入文本
hello world。 - 按Esc键返回命令模式。
- 若有需要,使用
u撤销当前行中出错的编辑操作。 - 若有需要,按
ctrl+r键反向撤销当前行中出错的编辑操作。
# /etc/profile hello world # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } ·····3.根据任务要求,多次重复此文本删除循环:
- 使用方向键定位光标。
- 按
x键删除选定的文本。 - 若有需要,使用
u撤销当前行中出错的编辑操作。
#按x删除 # /etc/profile hello # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc ······#按u恢复 # /etc/profile hello world # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc4.若要保存或退出,可选择下列操作之一来写入或放弃文本编辑:
- 按
:w写入(保存)文件,并留在命令模式中进行更多编辑。 - 按
:wq写入文件并退出 Vim。 - 按
:q!退出 Vim,但放弃上次写入以来进行的所有更改。
#按Shift+:出现最下面的显示,按照要求输入wq保存,再次打开之前输入的信息还在 ············ if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`/usr/bin/id -u` UID=`/usr/bin/id -ru` fi USER="`/usr/bin/id -un`" LOGNAME=$USER MAIL="/var/spool/mail/$USER" fi # Path manipulation :wq4.2示例2
1.使用vim ./profile.sh打开文件。
2.根据任务要求,多次重复此文本选择循环。
- 使用方向键将光标定位到第一个字符(gg)。
- 按
v键进入可视模式(选择对象)。 - 使用方向键将光标定位到最后一个字符。
- 按y键拖拉(复制)所选内容。
- 使用方向键将光标定位到插入位置。
- 按p键放置(粘贴)所选内容。
#按v键从当前光标开始,选择了如下4行并按y完成复制 # /etc/profile hello world #在下面的位置按p键粘贴显示如下 # /etc/profile hello world # /etc/profile hello world # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc ·········3.若要保存或退出,可选择下列操作之一来写入或放弃文本编辑。
- 按:w 写入(保存)文件,并留在命令模式中进行更多编辑。
- 按:wq 写入文件并退出 Vim。
- 按:q! 退出 Vim,但放弃上次写入以来进行的所有更改。
#不保存退出 ····· if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`/usr/bin/id -u` UID=`/usr/bin/id -ru` fi USER="`/usr/bin/id -un`" LOGNAME=$USER MAIL="/var/spool/mail/$USER" fi # Path manipulation :q!4.3示例3
完成 DHCP 服务配置文件定制。
#配置仓库 [root@centos7 fengkai 14:35:03]# curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@centos7 fengkai 14:35:03]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 安装dhcp软件 [root@centos7 fengkai 13:45:04]# yum install -y dhcp # 准备配置文件 [fengkai@centos7 ~ 14:35:56]$ cp /usr/share/doc/dhcp*/dhcpd.conf.example ~/dhcpd.conf编辑 ~/dhcpd.conf 文件:
[fengkai@fengkai ~ 18:32:02]$ vim dhcpd.conf1.:set number showcmd,显示行号和操作命令(多了行显和输入显示)
1 # dhcpd.conf 2 # ······ 39 40 subnet 10.254.239.32 netmask 255.255.255.224 { :set number showcmd2.gg,光标定位到第一行。
3.d46d,删除当前行以及后面的45行。(!!!注意不要用小键盘!!!)
4.:11,28d,删除11到28行。
5./You can,搜索字符串You can,并定位到该字符。
6.dG,删除当前位置行到最后,就剩下如下15行内容。
7.:%s/10.5.5/10.1.8/g,将所有行中10.5.5替换为10.1.8。
% 针对所有内容,s查找并替换,g替换查找到的所有内容。
8./224,定位这个数字。
9.cw,替换为0,然后esc。
10.将30换成130。
11.光标定位到ns1,修改dns为223.5.5.5:C然后输入223.5.5.5;。
12.修改路由器为10.1.8.2,使用 r 替换末尾1为2。
13.修改广播地址10.1.8.31为10.1.8.255。
14.ZZ,保存退出。
5.vim 配置
两个位置:
- 全局:
/etc/vimrc,给所有用户设置一些默认值。 - 用户:
~/.vimrc,特定用户的个性化设置。
常见的设置命令:(7-11行可以不用复制进入,只做说明,不然会有报错)
[fengkai@fengkai ~ 18:51:00]$ vim ~/.vimrc 1 set number 2 set showcmd 3 set ai ts=2 4 set cursorcolumn 5 set cursorline 6 7 # 参数说明: 8 # 显示行号 set number 9 # 显示操作命令 set showcmd 10 # ai,自动缩进;ts,tabspace使用2个空格代替 set ai ts=2 11 # 列高亮对齐显示 set cursorcolumn 12 # 行高亮对齐显示 set cursorline6.综合测试
完成示例1
完成示例2
完成示例3
Shift+ZZ之后再进入查看
[fengkai@fengkai ~ 18:44:24]$ vim dhcpd.conf subnet 10.1.8.0 netmask 255.255.255.0 { range 10.1.8.26 10.1.8.130; option domain-name-servers 223.5.5.5; option domain-name "internal.example.org"; option routers 10.1.8.2; option broadcast-address 10.1.8.255; default-lease-time 600; max-lease-time 7200; } host fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com;