N26 第六周作业

总结vim编辑器的使用

Vim 模式

   Vim 有六种基本模式
    Normal mode             
    Visual mode  "– VISUAL –"           
    Select mode  "– SELECT –"
    Insert mode  "– INSERT –"           
    Command-line mode       
    Ex mode                 
    另外还有六种附加模式
    Operator-pending mode
    Replace mode  "– REPLACE –"
    Virtual Replace mode "– VREPLACE –"
    Insert Normal mode  "– (insert) –"    
    Insert Visual mode "– (insert) VISUAL –"      

    Insert Select mode "– (insert) SELECT –"

模式切换

如果不清楚正处于哪种模式,可以按两次 Esc 键回到 Normal 模式,这有一个例外,从 Ex 模式输入“:vi”才能返回到 Normal模式。

模式之间切换可以参考下图:(^V 代表组合锓 Ctrl – V)

2017-01-29 20 10 05.png

*1 Go from Normal mode to Insert mode by giving the command "i", "I", "a",
  "A", "o", "O", "c", "C", "s" or S".
*2 Go from Visual mode to Normal mode by giving a non-movement command, which
  causes the command to be executed, or by hitting <Esc> "v", "V" or "CTRL-V"
  (see v_v), which just stops Visual mode without side effects.
*3 Go from Command-line mode to Normal mode by:
  – Hitting <CR> or <NL>, which causes the entered command to be executed.
  – Deleting the complete line (e.g., with CTRL-U) and giving a final <BS>.
  – Hitting CTRL-C or <Esc>, which quits the command-line without executing
    the command.
  In the last case <Esc> may be the character defined with the 'wildchar'
  option, in which case it will start command-line completion.  You can
  ignore that and type <Esc> again.  {Vi: when hitting <Esc> the command-line
  is executed.  This is unexpected for most people; therefore it was changed
  in Vim.  But when the <Esc> is part of a mapping, the command-line is
  executed.  If you want the Vi behaviour also when typing <Esc>, use ":cmap
  ^V<Esc> ^V^M"}
*4 Go from Normal to Select mode by:
  – use the mouse to select text while 'selectmode' contains "mouse"
  – use a non-printable command to move the cursor while keeping the Shift
    key pressed, and the 'selectmode' option contains "key"
  – use "v", "V" or "CTRL-V" while 'selectmode' contains "cmd"
  – use "gh", "gH" or "g CTRL-H"  g_CTRL-H
*5 Go from Select mode to Normal mode by using a non-printable command to move
  the cursor, without keeping the Shift key pressed.
*6 Go from Select mode to Insert mode by typing a printable character.  The
  selection is deleted and the character is inserted.

使用Vim打开文件

vim [options] [file ..]
vim functions
vim +2 functions  +2 表示打开文件后光标位于第2行的开头
vim + functions 如果省略"+"号后面的数字,则表示打开文件后,光标位于文件最后一行的开头
vim +/if functions 打开文件后,光标位于第1次匹配模式(if)的位置

保存文件

:w 保存修改
ZZ:保存并退出
: x  保存并退出
:wq  保存并退出;
:w file_name 保存结果到file_name文件中,而vim中的文件仍旧是先前打开在文件

关闭文件

:q!  强制退出,不保存修改结果
:q  如果文件自从上次保存后未曾修改,直接退出,否则提示错误

光标移动

:help motion.txt 可查看完整的相关光标移动的帮助
:help usr_03.txt 可查看较重要的相关光标移动的帮助

字符跳转

(1)可以使用键盘的上下左右箭头
(2)可以使用键盘的hjkl键:
      h 向左
      l 向右(小写L)
      j 向下
      k 向上
(3)使用数字与命令组合实现按指定字符移动光标,例如:
   3k 表示向上移动3行
   4<-(4加左箭头)表示向左移动4个字符

单词跳转

   w 光标移到下一个单词的首字母
   e 光标移到当前或下一个单词的尾字母
   b 光标移到当前或前一个单词的首字母

   #(w|e|b) 数字与指定命令之一组合,每次跳转指定数目在单词

行首行尾跳转

 ^ 转到行首第一个非空白字符
 0 转到行首第一个字符
 $ 转到行尾第一个字符
 段间跳转

 } 转到下一段(光标定位在空白行)
 { 转到下一段(光标定位在空白行)

 句间跳转

 ( 转到前一句
 ) 转到后一句

 翻屏

 Ctrl+f 向下一屏 Forwards (downwards)
 Ctrl+b 向上一屏 Backwards (upwards)

 Ctrl+d 向下半屏 Downwards
 Ctrl+u 向上半屏 Upwards

 Enter:按行向后翻

vim的编辑命令:

字符编辑:

x 删除光标位置字符
#x 删除从光标开始指定个数字符

xp 可实现光标位置字符与其后字符对调位置
rCHAR 将光标位置字符替换为CHAR所指定的字符,注意是小写的r,大写R直接进入替换模式了

删除(d)

dd 删除光标所在行
d$ 删除光标开始至行尾字符
d^ 删除光标前一个字符至行首第一个非空白字符
dw 删除光标开始至下一个单词首字母之间的字符
de 删除光标位置至下一个词尾之间的字符
db 删除渔村位置前一个字符至前一个词首之间的字符

#d(w|b|e|$|^|d) 删除指定数量的字符、单词、行

复制(y)

工作方式类似 d 命令

粘贴

p(小写)在光标位置后面或当前行后粘贴
#p 在光标位置后或当前行后粘贴指定次数
P(大写)在光标位置前面或当前行之前粘贴
#P 在光标位置前面或当前行前面粘贴指定次数

删除(c)

工作方式类似d,所不同在是删除后直接转为Insert模式

撤销(u)

#u 撤销n步
反撤销(Ctrl+r)
#ctrl+r反撤销n步

重复执行前一个编辑操作:
[#].

按行选定(V)
按字符选定(v)
按块选定(Ctrl+V)
以上3种可视化选定方法结合上下左右来选择,然后结合d、c、y、x等命令使用

vim自带的练习教程:vimtutor

命令行模式

   命令范围

   # 特定行
   . 当前行
   $ 最后一行
   % 所有行,相当于1,$
   #,# 某行到某行
   /pattern/ 由pattern匹配的下一行
   #,+#:指定行范围,如3,+2 为第3行加2行,总共3行

   结合编辑命令,实现编辑操作
   d
   y
   c
   r fild_name 将指定文件内容插入命令范围之后
   w file_name 将选定范围写入某文件

查找

/pattern  按模式匹配自上向下查找
?pattern  按模式匹配自下向上查找
n 在查找结果中按查找方向依次导航
N 在查找结果中按查找方向相反的方向依次导航

查找与替换

:[range]s/{pattern}/{string}/[flags]
range为命令范围
pattern为要查找内容的匹配模式
string为需要替换的内容,可以后向引用
flags为修饰符
   i,忽略大小写
   g,行内全局匹配

可把分隔符替换为其它非常用字符:
   s@@@
   s###

同时打开多个文件

vim FILE1 FILE2 …
            在文件间切换:
               :next  下一个
               :prev  上一个
               :first   第一个
               :last   最后一个

               退出所有文件:
               :wqall 保存所有文件并退出;
               :wall
               :qall

               打开多个文件时可以使用选项拆分窗口
               vim -o abc bcd  拆分成上下两个窗口
               vim -O abc bcd  拆分成左右两个窗口

               窗口切换可以使用 Ctrl+w, Arrow

               单个文件拆分窗口
                           Ctrl+w, s:拆分成上下两个窗口
                           Ctrl+w, v:拆分成左右两个窗口

Vim 工作特性

   配置文件
      /etc/vimrc     System wide Vim initializations.
       ~/.vimrc       Your personal Vim initializations.

:help option-list
查看特性值 :se[t] {option}?
   行号
      打开::set number
      关闭::set nonumber
   自动缩进
      打开::set autoindent
      关闭::set noautoindent
   高亮搜索
      打开::set hlsearch
      关闭::set nohlsearch
   语法高亮
      打开::syntax on
      关闭::syntax off
   搜索pattern忽略大小写
      打开::set ignorecase
      关闭::set noignorecase

 获取帮助:

 :help
 :help subject

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#

root@localhost ~]# cp /etc/rc.d/rc.local /tmp/
[root@localhost ~]# vim /tmp/rc.sysinit
:%s/^[[:space:]]\+[^[:space:]]/#&/

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

[root@localhost ~]# cp /boot/grub/grub.conf /tmp
[root@localhost ~]# vim /tmp/grub.conf
:%s/^[[:space:]]\+//g

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符

[root@localhost ~]# cp /etc/rc.sysinit /tmp/
[root@localhost ~]# vim /tmp/rc.sysinit
:%s/^#[[:space:]]\+//

4、为/tmp/grub.conf文件中前三行的行首加#号;

:1,3s/^./#&/

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

:%s/\(enabled\|gpgcheck\)=0/\1=1/g

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202

0 */4 * * * /bin/bash -c 'filename=`date +/backup/etc-\%Y\%m\%d\%H\%M`&& mkdir $filename && cp -r /etc/* $filename && echo $filename && unset filename'

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402

0 0 * * 2,4,6 /bin/bash -c 'filename=`date +messages-\%Y\%m\%d` && cp /var/log/messages /backup/messages/$filename'

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

0 */2 * * * /bin/bash -c 'echo -e `date +"\n\%F \%T"` >> /stats/memory.txt && cat /proc/meminfo | grep -i "^s" >> /stats/memory.txt'

9、工作日的工作时间内,每两小时执行一次echo ""howdy""

0 */2 * * 1-5 echo ""howdy""

10、创建目录/tmp/testdir-当前日期时间;

#!/bin/bash
declare curtime;
curtime=$(date +"%Y%m%d%H%M")
mkdir /tmp/testdir-$curtime

11、在此目录创建100个空文件:file1-file100

#!/bin/bash
declare curtime;
curtime=$(date +"%Y%m%d%H%M")
if [ ! -d $curtime ]; then
  mkdir /tmp/testdir-$curtime
fi
declare -i i=0;
while [ $i -lt 100 ]; do
  i+=1
  touch /tmp/testdir-$curtime/file$i
done

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

#!/bin/bash
declare -a users=(`cut -d: -f1 /etc/passwd`)
declare -i len=${#users[@]}
declare -i i=0
while [ $i -le $len ] ;do
  if [ $(($i%2)) -eq 1 ];then
    echo ${users[$i]}
  fi
  i+=1
done

13、创建10用户user10-user19;密码同用户名;

#!/bin/bash
for i in {10..19};do
  id user$i &> /dev/null
  if [ ! $? -eq 0 ];then
    useradd user$i
  fi
done

14、在/tmp/创建10个空文件file10-file19;

#!/bin/bash
for i in {10..19};do
  if [ ! -f /tmp/file$i ]; then
    touch /tmp/file$i
  fi
done

15、把file10的属主和属组改为user10,依次类推。

#!/bin/bash
for i in {10..19};do
        if [ -f /tmp/file$i ];then
                id user$i &> /dev/null
                if [ $? -eq 0 ];then
                        chown $(cat /etc/passwd | grep "^user$i" |cut -d: -f3-4) /tmp/file$i
                fi
        fi
done

原创文章,作者:和风细雨,如若转载,请注明出处:http://www.178linux.com/67283

(0)
上一篇 2017-02-01 19:12
下一篇 2017-02-01 23:23

相关推荐

  • 使用keepalive实现nginx反向代理高可用

    简介: 在网站架构中,为了分散客户端对服务器的访问压力,可以使用nginx作为反向代理。但是使用一个nginx作为代理服务器必定会面对单点故障的情况,所以一般使用多台nginx反代服务器,而使用多台nginx服务器还要面对如何协调调度的问题。在此,我给大家介绍使用keepalive协调调度nginx反代服务器的方法。   keepalive简介 说…

    2017-05-15
  • Nginx反向代理https,提供PhpMyAdmin

    准备工作:主机1、双网卡:外–172.16.252.187  内–192.168.42.140 主机2:192.168.42. 145 主机3:172.16.251.112 主机4:192.168.42.141 在主机2上:     安装httpd  yum  install…

    2017-07-01
  • 【招聘福利】上海/北京/深圳丨 云络科技丨招募linux系统运维工程师

    岗位要求: 1,安装服务器 – Linux CentOS + netinstall 2.服务器安全 – 用户、权限、iptables、sudo 3.计划部署Linux?高可用性,包括HA,LB 4.协助Leader完成复杂的网络设计 (完全冗余/故障迁移/高可用性) 5.用linux命令查看服务器(top/iostat/vmstat/sar/等) 6.安装、…

    Linux干货 2016-04-05
  • shell 脚本之数组 和 yum 【下】

    shell 脚本之数组 和 yum 【下】 数组   变量:存储单个元素的内存空间   数组:存储多个元素的连续的内存空间,相当于多个变量的 集合。   数组名和索引        索引:编号从0开始,属于数值索引        注意:索引可支…

    Linux干货 2016-08-24
  • corosync的高可用——高可用mariadb数据库

    corosync, pacemaker,nfs高可用mariadb 实验环境: 4台CentOS 7 的主机,并关闭的iptalbes和selinux功能 主机1:10.1.43.101 node1 corosync+pacemaker+amp 主机2:10.1.43.102 node2 corosync+pacemaker+amp 主机3:10.1.43….

    Linux干货 2016-11-24
  • NET25-第11周作业

    1、详细描述一次加密通讯的过程,结合图示最佳。 第一步:他们需要实现协商好对称加密算法,单向加密算法,公钥加密算法,交换公钥等。 第二步:B用户想要将数据传给A,首先需要使用单向加密算法取出数据的特征码,并用自己的私钥对这段特征码进行加密(数字签名),B用户生成临时对称密钥,并用对称密钥加密整段数据,B用户使用 A用户的公钥加密一次性对称密钥,附加在整段数据…

    Linux干货 2017-05-15

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-02 20:05

    非常的详细和认真,加油,再接再励。