5.vim 编辑器(从零开始学)
1.vim 编辑器介绍每一个系统管理员都有一个偏爱的文本编辑器。有的管理员偏爱gedit有的偏爱nano甚至有的偏爱emacs。即使已经有偏爱的编辑器了解vim还是有必要的。因为vim可以安装在任何系统。vim版本Linux 系统中可以安装vim三个不同版本vim-minimal只提供vi和相关命令。vim-enhanced提供vim命令提供语法高亮拼写检查等特性。vim-X11提供gvim图形界面下的vim。gvim包涵菜单栏。当用户不知道命令的时候可以通过菜单操作。# 安装 vim 工具 [fengkaifengkai ~ 18:01:40]$ su 密码 [rootfengkai fengkai 18:02:13]# yum install vim 已加载插件fastestmirror, langpacks Loading mirror speeds from cached hostfile Could not retrieve mirrorlist http://mirrorlist.centos.org/?release7archx86_64repoosinfrastock error was 14: curl#6 - Could not resolve host: mirrorlist.centos.org; 未知的错误 One of the configured repositories failed (未知), and yum doesnt 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 --disablereporepoid ... 4. Disable the repository permanently, so yum wont 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 --disablerepoid 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 --setoptrepoid.skip_if_unavailabletrue 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整行选中或者ctrlv按矩形选中进入该模式。在该模式下用于选中文件随后可以复制或删除选中的内容。[rootfengkai fengkai 18:02:26]# cp /etc/profile ./profile.sh [rootfengkai fengkai 18:03:32]# ls profile.sh 公共 模板 视频 图片 文档 下载 音乐 桌面 [rootfengkai 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示例11使用vim ./profile.sh打开文件。# /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # Its NOT a good idea to change this file unless you know what you # are doing. Its 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撤销当前行中出错的编辑操作。若有需要按ctrlr键反向撤销当前行中出错的编辑操作。# /etc/profile hello world # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # Its NOT a good idea to change this file unless you know what you # are doing. Its 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示例21使用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 服务配置文件定制。#配置仓库 [rootcentos7 fengkai 14:35:03]# curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [rootcentos7 fengkai 14:35:03]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 安装dhcp软件 [rootcentos7 fengkai 13:45:04]# yum install -y dhcp # 准备配置文件 [fengkaicentos7 ~ 14:35:56]$ cp /usr/share/doc/dhcp*/dhcpd.conf.example ~/dhcpd.conf编辑 ~/dhcpd.conf 文件[fengkaifengkai ~ 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.5C然后输入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行可以不用复制进入只做说明不然会有报错[fengkaifengkai ~ 18:51:00]$ vim ~/.vimrc 1 set number 2 set showcmd 3 set ai ts2 4 set cursorcolumn 5 set cursorline 6 7 # 参数说明 8 # 显示行号 set number 9 # 显示操作命令 set showcmd 10 # ai自动缩进tstabspace使用2个空格代替 set ai ts2 11 # 列高亮对齐显示 set cursorcolumn 12 # 行高亮对齐显示 set cursorline6.综合测试完成示例1完成示例2完成示例3ShiftZZ之后再进入查看[fengkaifengkai ~ 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;