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
下一篇 2017-02-01

相关推荐

  • Man手册功能介绍

    Man:是Linux下的帮助指令,通过man指令可以查看Linux中的指令帮助、配置文件帮助和编程帮助等信息。 语法:man(选项)(参数) 选项: -a:在所有的man帮助手册中搜索 -f:等价于whatis指令,显示给定关键字的简短描述信息 -P:指定内容时使用分页程序 -M:指定man手册搜索的路径。 本地手册:/usr/share/man &nbsp…

    Linux干货 2016-10-17
  • Linux基础之文件管理和bash特性

    部分文件管理命令,bash的一些基础特性,部分glob文件匹配示例

    Linux干货 2018-01-16
  • 学习宣言

     学习计划:参考N24学习时间与线路图,尽最大努力完成。 目标:学习好Linux,提升自我价值,找一份待遇好的工作。 宣言:每一次轻易的放弃,都是人生的一处败笔,为了梦想,加油!

    Linux资讯 2016-10-26
  • UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现

    类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性、操作、关系的对象集合的总称。 2) 在系统中,每个类具有一定的职责,职责指的是类所担任的任务,即类要完成什么样的功能,要承担什么样的义务。一个类可以有多种职责,设计得好的类一般只有一种职责,在定义类的时候,将类的职责分解成为类的属性和操作(即方法)。 3) 类的属性…

    Linux干货 2015-04-07
  • Nginx的安装及其一些配置

    nginx的编译安装    tar xf nginx-1.12.2.tar.gz   cd nginx-1.12.2     yum install pcre-devel  ./configure –help   groupadd -r nginx     useradd -g nginx -r nginx    id nginx     ./c…

    Linux干货 2017-10-25
  • linux基础练习

    马哥教育23期网络班+第6周课堂练习 Linux 基础练习 一、linux基础练习题 1、复制/etc/rc.d/rc.sysinit 文件至/tmp 目录,将/tmp/rc.sysinit 文件中的以至少一个空白字符开头的行的行首加#; 2、复制/boot/grub/grub.conf 至/tmp目录中 删除/tmp/grub.conf 文件中的行首的空白…

    Linux干货 2016-11-01

评论列表(1条)

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

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