马哥教育N22期第六周作业

关于vim编辑器使用方法整理

编辑器分为文本编辑器、全屏编辑器、模式化编辑器

vim是最常用的编辑器之一,是vi的增强版

  • 基本模式分为编辑模式、命令模式、输入模式、末行模式

  • 下面我们介绍一下vim编辑器常用方法:

打开文件

vim +#:打开文件后,直接光标处于第#行行首
+/PATTERN:打开文件后,直接让光标处于第一个被PATTERN匹配
+:处于尾部的行首
输入模式-->编辑模式:按键盘 ESC
编辑模式-->末行模式:按键盘shift+:
末行模式-->编辑模式:按键盘 ESC
编辑模式下:ZZ保存并退出
:q   退出
:q! 强制退出,不保存
:wq  保存并退出
:x   保存并退出
:w /PATH/TO/SOMEFILE 把文件内容输出到某路径的文件中(无需事先创建文件),相当于另存为

输入和编辑模式:

光标定位:
    - 字符间跳转:除了用上下左右键之外还可以通过h:左,j:下,k:上,i:右  来控制
    - 单词间跳转: w:下一个单词的词首;e:当前或后一个单词的词尾;b:当前或前一个单词的词尾;
    - 行首行尾跳转:^:跳转至行首第一个非空白字符;0:跳转至行首;$:跳转至行尾
    - 行间跳转: #G:跳转至由#指定的行;1G,gg:第一行; G:最后一行
    - 句间跳转:( )
    - 段间跳转: { }
    - 翻屏操作: Crtl+f:向文件尾部翻一屏
                Ctrl+b:向文件首部翻一屏
                Ctrl+d:向文件尾部翻半屏
                Ctrl+u:向文件首部翻半屏
                Enter:按行向后翻

编辑命令:

- 删除和替换命令:x:删除光标所在处的字符
                #x:删除光标所在处起始的#个字符
                xp:光标所在处字符与其后面的字符交换
                替换命令:rCHAR:替换光标所在处的字符
                删除命令:d:删除命令,可结合光标跳转字符,实现范围删除:
                d^/d$:删除光标所在处一直到行首或行尾的内容
                dw:删除单词
                de、db:删除光标所在处前、后的单词
                dd:删除光标所在处的行
- 粘贴和复制命令:p(小写):缓冲区中的内容如果为整行,则粘贴在当前光标所在行的下方;否则粘贴至光标所在处的后方
                P(大写):缓冲区中的内容如果为整行,则粘贴在当前光标所在行的下方;否则粘贴至光标所在处的后方
                y(yank,y):复制,工作类似于d命令 y$\y^\y0\ye\yw\yy
- 撤销和恢复命令:u:撤销之前操作
                Ctrl+r:恢复此前撤销操作

末行模式: 之前介绍了shift+:进入此模式,下面介绍一个常用方式

- 地址定界:$:最后一行
          #:特定的第#行,例如即第5行
          #,#:指定行范围,左侧为起始,右侧为结束
          #,+#:其实开始向后加#行例如3,3+7
          %:表示全文
          /pattern/:从光标所在处起始向文件尾部第一次被模式所匹配到的行
- 查找:/PARTTERN:从当前光标所在处向文件尾部查找能够被当匹配到的所有字符串
       ?PARTTERN:从当前光标所在处向文件首部查找能够被当前匹配到的所有字符串
       n:定位匹配到的字符串,下一个,与命令方向相同
       N:下一个,与命令方向不同(都可以循环查询)
- 查找替换:s:查询命令
           s/要查找的内容/替换为的内容/修饰符
           可把分隔符'/'替换为其它非常用字符:@或#
           修饰符:
           i:忽略大小写;
           g:全局替换,意味着一行中如果匹配到多次,则均替换;
  • 以上是对vim编辑器的一些实用总结,更多内容需要自己查看资料

欢迎访问 我的博客

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

cp /etc/rc.d/rc.sysinit /tmp/
:%s@^[[:space:]]\+@#@
387 次替换,共 387 行

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

cp /boot/grub/grub.conf /tmp/
:%s@^[[:space:]]\+@@
3 次替换,共 3 行

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

:%s@^#[[:space:]]@@
38 次替换,共 38 行

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

:1,3 s/^/#&
3 次替换,共 3 行

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 * * * cp -rf /etc /home/xuc/backup/etc-`date +%Y%m%d%H%M`

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

* * * * 2,4,6 cp -rf /var/log/messages /backup/messages-`date +%Y%m%d%H%M`

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

 0 */2 * * *cat /proc/meminfo |egrep '^S' >> /tmp/stats/memory.txt

**9、工作日的工作时间内,每两小时执行一次echo ""howdy"" 脚本编程练习

* 9-18/2 * * * echo "howdy"

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

[root@localhost tmp]# mkdir testdir-`date +%F-%T`
[root@localhost tmp]# ll |grep test
drwxr-xr-x. 2 root root    6 Sep 14 06:00 testdir-2016-09-14-06:00:44

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

#!/bin/bash
#
for((i=1;i<=100;i++));do
        touch file$i
done
[root@localhost testdir-2016-09-14-06:00:44]# ls
addfile.sh  file19  file3   file40  file51  file62  file73  file84  file95
file1       file2   file30  file41  file52  file63  file74  file85  file96
file10      file20  file31  file42  file53  file64  file75  file86  file97
file100     file21  file32  file43  file54  file65  file76  file87  file98
file11      file22  file33  file44  file55  file66  file77  file88  file99
file12      file23  file34  file45  file56  file67  file78  file89
file13      file24  file35  file46  file57  file68  file79  file9
file14      file25  file36  file47  file58  file69  file8   file90
file15      file26  file37  file48  file59  file7   file80  file91
file16      file27  file38  file49  file6   file70  file81  file92
file17      file28  file39  file5   file60  file71  file82  file93
file18      file29  file4   file50  file61  file72  file83  file94

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

#!/bin/bash
#
declare -i num
num=`cat /etc/passwd |wc -l`
for((i=2;i<=$num;i+=2));do
        head -$i /etc/passwd |tail -1 |cut -d: -f1
done

[root@localhost testdir-2016-09-14-06:00:44]# bash evennumber.sh
bin
adm
sync
halt
operator
ftp
systemd-bus-proxy
dbus
abrt
tss
usbmuxd
saslauth
rpc
chrony
qemu
rpcuser
avahi-autoipd
sssd
gdm
sshd
postfix
xuc
visitor
user1

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

#!/bin/bash
#
for i in {10..19};do
        id user$i &> /dev/null || useradd user$i
        echo "user$i" |passwd --stdin user$i
done

[root@localhost xuc-scripts]# bash -x adduser.sh 
+ for i in '{10..19}'
+ id user10
+ passwd --stdin user10
+ echo user10
Changing password for user user10.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user11
+ echo user11
+ passwd --stdin user11
Changing password for user user11.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user12
+ echo user12
+ passwd --stdin user12
Changing password for user user12.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user13
+ echo user13
+ passwd --stdin user13
Changing password for user user13.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user14
+ echo user14
+ passwd --stdin user14
Changing password for user user14.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user15
+ echo user15
+ passwd --stdin user15
Changing password for user user15.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user16
+ echo user16
+ passwd --stdin user16
Changing password for user user16.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user17
+ echo user17
+ passwd --stdin user17
Changing password for user user17.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user18
+ echo user18
+ passwd --stdin user18
Changing password for user user18.
passwd: all authentication tokens updated successfully.
+ for i in '{10..19}'
+ id user19
+ echo user19
+ passwd --stdin user19
Changing password for user user19.
passwd: all authentication tokens updated successfully.

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

#!/bin/bash
#
for i in {10..19};do
        touch /tmp/file$i
done
~        
[root@localhost xuc-scripts]# bash -x touch_nonefile.sh 
+ for i in '{10..19}'
+ touch /tmp/file10
+ chown user10:user10 /tmp/file10
+ for i in '{10..19}'
+ touch /tmp/file11
+ chown user11:user11 /tmp/file11
+ for i in '{10..19}'
+ touch /tmp/file12
+ chown user12:user12 /tmp/file12
+ for i in '{10..19}'
+ touch /tmp/file13
+ chown user13:user13 /tmp/file13
+ for i in '{10..19}'
+ touch /tmp/file14
+ chown user14:user14 /tmp/file14
+ for i in '{10..19}'
+ touch /tmp/file15
+ chown user15:user15 /tmp/file15
+ for i in '{10..19}'
+ touch /tmp/file16
+ chown user16:user16 /tmp/file16
+ for i in '{10..19}'
+ touch /tmp/file17
+ chown user17:user17 /tmp/file17
+ for i in '{10..19}'
+ touch /tmp/file18
+ chown user18:user18 /tmp/file18
+ for i in '{10..19}'
+ touch /tmp/file19
+ chown user19:user19 /tmp/file19

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

#!/bin/bash
#
for i in {10..19};do
        touch /tmp/file$i &> /dev/null
        chown user$i:user$i /tmp/file$i
done
~                      
[root@localhost xuc-scripts]# bash -x touch_nonefile.sh 
+ for i in '{10..19}'
+ touch /tmp/file10
+ chown user10:user10 /tmp/file10
+ for i in '{10..19}'
+ touch /tmp/file11
+ chown user11:user11 /tmp/file11
+ for i in '{10..19}'
+ touch /tmp/file12
+ chown user12:user12 /tmp/file12
+ for i in '{10..19}'
+ touch /tmp/file13
+ chown user13:user13 /tmp/file13
+ for i in '{10..19}'
+ touch /tmp/file14
+ chown user14:user14 /tmp/file14
+ for i in '{10..19}'
+ touch /tmp/file15
+ chown user15:user15 /tmp/file15
+ for i in '{10..19}'
+ touch /tmp/file16
+ chown user16:user16 /tmp/file16
+ for i in '{10..19}'
+ touch /tmp/file17
+ chown user17:user17 /tmp/file17
+ for i in '{10..19}'
+ touch /tmp/file18
+ chown user18:user18 /tmp/file18
+ for i in '{10..19}'
+ touch /tmp/file19
+ chown user19:user19 /tmp/file19

原创文章,作者:N22_熊宝,如若转载,请注明出处:http://www.178linux.com/49754

(0)
N22_熊宝N22_熊宝
上一篇 2016-10-09 09:06
下一篇 2016-10-09 09:06

相关推荐

  • 第一周作业

    1、描述计算机的组成及其功能。
    2、按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别。
    3、描述Linux的哲学思想,并按照自己的理解对其进行解释性描述。
    4、说明Linux系统上命令的使用格式;详细介绍ifconfig、echou、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,并配合相应的示例来阐述。
    5、如何在Linux系统上获取命令的帮助信息,请详细列出,并描述man文档的章节是如何划分的。
    6、请罗列Linux发行版的基础目录名称命名法则及共用规定。

    Linux干货 2017-12-03
  • 浅谈编译kernel+busybox构建拥有远程ssh登录和web功能最小linux系统(二)

    忘了介绍本文的源码的版本了 dropbear-2013.58.tar.bz2    busybox-1.21.1.tar.bz2 linux-3.13.6.tar.xz  nginx-1.4.7 基于上文,我们还差group文件没有写 root@mysql etc]# vi group  …

    Linux干货 2015-09-22
  • Hello World 集中营

    编程的人应该都知道什么是Hello World。这是一个最简单的程序,其只在屏幕上输出“Hello World”字样,这通常是初学者的在学习编程时的第一个示例。把打印出 “Hello World” 作为第一个范例程序,现在已经成为编程语言学习的传统。“Hello World”起源于Brian Kernighan 和Dennis MacAlistair Rit…

    2016-05-17
  • 【译文】如何正确设定MySQL程序的选项

    MySQL手册文档版本:5.6  章节:4.2 Using MySQL Programs  略去了部分windows下使用mysql的相关的说明 译者:古二娃 时间:2015-03-15 章节目录: 4.2 Using MySQL Programs 4.2.1 Invoking MySQL Programs …

    Linux干货 2015-03-10
  • varnish 缓存服务器配置与使用

    varnish 缓存服务器配置与使用 一、cache 1.缓存为什么会存在? 一个快的设备要想和慢的设备交互,只有一种办法就是让快的设备工作在慢的设备的频段上!这样的话就浪费了,我们可以在两者之间加上一个加速器,这个加速器就叫做缓存! CPU要读取一个数据时,首先从Cache中查找,如果找到就立即读取并送给CPU处理;如果没有找到,就用相对慢的速度从内存中读…

    2016-11-13
  • NFS服务器设置

    一、NFS的相关概念     NFS(Network File System)即网络文件系统的缩写,由Sun公司研发,其目的是为了解决网络文件共享的问题。用户可以实现像挂载本地文件系统一样挂载NFS服务器的共享目录;其具有配置简单、使用高效的特点,但只能在Linux系统使用,不能跨平台使用。 &nb…

    Linux干货 2015-06-25

评论列表(1条)

  • luoweiro
    luoweiro 2016-10-11 23:48

    作业整理的非常详细,对于vim还是希望能多加运用,熟能生巧。