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

相关推荐

  • 推荐-DNS架设实验

    DNS架设实验 实验拓扑 实验准备 流程 测试 总结 实验拓扑: 1.对于来自内网的DNS正反向解析,并实现view选择指定解析库解析。2.对于来自外网的DNS正向解析,并实现view选择指定解析库解析。3.实现主从服务器结构。4.实现一个完成对一个子域的授权。5.子域中的所有查询xiao.com.的信息都转向192.168.1.1解析。 1.根据view,…

    2016-04-19
  • DNS and BIND

    DNS and BIND 名称解析:把一种代号转换成为另一种代号的功能是应用程序基于某个搜索键在指定的数据库查询,查询到对应的键以后,对应键被找出来的过程!passwd —《nsswith》/etc/nsswith.conf 注意:查询的时候用到的是UDP的53,主从复制走的是tcp(但是也用到udp) DNS: Domain Name Serv…

    Linux干货 2016-10-21
  • Btrfs文件系统

    btrfs文件系统特性: 1、多物理卷支持 Btrfs可由多个底层物理卷组成:支持raid,以联机添加,移除,修改 2、写时复制机制(cow)     复制,更新及替换指针,而非就地更新。在文件进行修改的时候,首先将文件复制一份出来,在复制的文件中进行修改,修改完成后,将指向原有文件的指针指向到修改完成的文件上,若修改完成的文件出现错误,则我们可以通过源文件…

    2016-04-10
  • 11. vim备查の小手札

    三种模式 命令模式(默认)      command mode:移动光标,剪切/粘贴文本 插入(编辑)模式         edit mode:修改文本 扩展命令模式 extend…

    Linux干货 2016-08-18
  • 开篇

    新人报道

    Linux干货 2016-10-24
  • FHS文件系统介绍及各目录功能说明

     FHS文件系统介绍及各目录功能说明 M21-陆东贵 FHS简介 Filesystem Hierarchy Standard(文件系统目录标准)的缩写,多数Linux版本采用这种文件组织形式,类似于Windows操作系统中c盘的文件目录,FHS采用树形结构组织文件。FHS定义了系统中每个区域的用途、所需要的最小构成的文件和目录,同时还给出了例外处理…

    Linux干货 2016-10-18

评论列表(1条)

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

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