請詳細總結vim編輯器的使用并完成以下練習題
單詞間跳轉
w:下一個單詞的詞首
e:當前或后一個單詞的詞尾
b:當前或前一個單詞的詞首
行首行尾跳轉:
^:跳轉至行首的第一個非空白字符
0:跳轉至行首
$: 跳轉至行尾
句間跳轉:
)
(
段落跳轉:
}
{
字符編輯
x:刪除光標所在處的字符
#x:刪除光標所在處起始的#個字符
xp:交換光標所在處的字符與其后面的字符的位置
替換命令:
r替換光標所在處的字符
刪除命令:
d:可結合光標跳轉字符,實現范圍刪除
粘貼命令
p:緩沖區中的內容如何為整行,則粘貼在當前光標所在行的下方:否則,粘貼至當前光標所在處的后方
P:緩沖區中的內容如何為整行,則粘貼在當前光標所在行的上方:否則,粘貼至當前光標所在處的前方
復制命令
y:復制,工作行為相似于d命令
可視化模式:
v:按字符選定
V:按行選定
結合編輯命令使用:d,c,y
撤銷操作:
u:撤銷此前的操作
#u:撤銷此前的#個操作
撤銷此前的撤銷:
Ctrl+r
重復執行前一個編輯操作
vim命令行模式:
地址定界:
start_pos[,end_pos]
#:特定的第#行,例如5即第五行;
#,#:指定行范圍,左側為起始行,右側為結束行;
#,+#:指定行范圍,左側為起始行絕對編號,右側為相對左側行號的偏移量
點,表示當前號
$:最后一行
%:表示全文
/pattern/:從光標所在處第一次被模式所匹配到的行
查找并替換:
s:命令行模式的命令,使用格式:
s/要查找內容/替換的內容/修飾符
要查找的內容:可使用正則表達式
替換的內容:不能使用正則表達式,但可以引用
如果“要查找的內容”部分在模式中使用分組符號:在“替換為的內容”中使用后向引用
直接引用查找模式匹配到的全部文本
修飾符:i:忽略大小寫
g:全局替換,意味著一行中如果匹配到多次,則均替換
1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
[root@localhost ~]# cp /etc/rc.d/rc.sysinit /tmp/rc.sysinit (先復制/etc/rc.d/rc.sysinit文件至/tmp目錄)
[root@localhost ~]# vim /tmp/rc.sysinit (用vim編輯文件)
在命令行模式輸入:%s/^[[:space:]]\+/#&/g
部分截屏:
if [ -x /usr/sbin/system-config-network-cmd ]; then
# if strstr "$cmdline" netprofile= ; then
# for arg in $cmdline ; do
# if [ "${arg##netprofile=}" != "${arg}" ]; then
# /usr/sbin/system-config-network-cmd –profile ${arg##netprofile=}
# fi
# done
# fi
fi
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[root@localhost ~]# cp /boot/grub/grub.conf /tmp/grub.conf (復制/boot/grub/grub.conf至/tmp目錄中)
[root@localhost ~]# vim /tmp/grub.conf (用vim編輯文件)
在命令行模式輸入:%s/^[[:space:]]\+//g
部分截屏
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=fec8b8a8-9718-42e2-af47-26e513138aa1 rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-431.el6.x86_64.img
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符
[root@localhost ~]# sed 's/^#[[:space:]]\+//g' /tmp/rc.sysinit
(部分截屏)
Set up binfmt_misc
/bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1
Boot time profiles. Yes, this should be somewhere else.
if [ -x /usr/sbin/system-config-network-cmd ]; then
if strstr "$cmdline" netprofile= ; then
for arg in $cmdline ; do
if [ "${arg##netprofile=}" != "${arg}" ]; then
/usr/sbin/system-config-network-cmd –profile ${arg##netprofile=}
fi
done
fi
fi
4、為/tmp/grub.conf文件中前三行的行首加#號;
[root@localhost ~]# sed '1,3 s/^.*/#&/g' /tmp/grub.conf
## grub.conf generated by anaconda
##
## Note that you do not have to rerun grub after making changes to this file
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1;
[root@localhost ~]# sed -r 's/(gpgcheck=|enabled=)0/\11/g' /etc/yum.repos.d/CentOS-Media.repo
部分截屏
gpgcheck=1
enabled=1
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201504020202
[root@localhost /]# mkdir backup (創建備份目錄/backup)
[root@localhost /]# crontab -e
輸入如下:
0 */4 * * * /bin/cp -a /etc /backup/etc-$(date +\%Y\%m\%d\%H\%M)
7、每周2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20150402
[root@localhost /]# crontab -e
輸入如下:
0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-$(date +\%Y\%m\%d)
8、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
[root@localhost /]# crontab -e
輸入如下:
0 */2 * * * /bin/grep "^S" /proc/meminfo > /stats/memory.txt
9、工作日的工作時間內,每兩小時執行一次echo "howdy"
0 8-12/2 * * 1-5 echo "howdy"
0 14-18/2 * * 1-5 echo "howdy"
腳本編程練習
10、創建目錄/tmp/testdir-當前日期時間;
11、在此目錄創建100個空文件:file1-file100
[root@localhost ~]# vim mkd-testdir.sh (創建腳本)
#!/bin/bash
# Author: liuguohui
# Date: 2016/09/24 17:20
# Email: liuguohui1008@sina.com
# Version: 0.0.1
# Description: mkdir a directory and touch one hundred file1-file100 empty file
#
dirname="testdir-$(date +\%Y\%m\%d\%H\%M)"
mkdir /tmp/$dirname
for i in {1..100}
do
touch /tmp/$dirname/file$i
done
[root@localhost ~]# bash mkd-testdir (執行腳本)
[root@localhost ~]# ls /tmp/testdir-201609241129 (查詢結果)
file1 file17 file25 file33 file41 file5 file58 file66 file74 file82 file90 file99
file10 file18 file26 file34 file42 file50 file59 file67 file75 file83 file91
file100 file19 file27 file35 file43 file51 file6 file68 file76 file84 file92
file11 file2 file28 file36 file44 file52 file60 file69 file77 file85 file93
file12 file20 file29 file37 file45 file53 file61 file7 file78 file86 file94
file13 file21 file3 file38 file46 file54 file62 file70 file79 file87 file95
file14 file22 file30 file39 file47 file55 file63 file71 file8 file88 file96
file15 file23 file31 file4 file48 file56 file64 file72 file80 file89 file97
file16 file24 file32 file40 file49 file57 file65 file73 file81 file9 file98
12、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名;
[root@localhost ~]# vim show-evenline.sh (創建腳本)
#!/bin/bash
# Author: liuguohui
# Date: 2016/09/24 19:15
# Email: liuguohui1008@sina.com
# Description: display even number line
#
sed -n '2~2p' $1 |cut -d ':' -f 1
[root@localhost ~]# bash show-evenline.sh /etc/passwd (執行腳本)
bin
adm
sync
halt
uucp
games
ftp
vcsa
postfix
13、創建10用戶user10-user19;密碼同用戶名;
14、在/tmp/創建10個空文件file10-file19;
15、把file10的屬主和屬組改為user10,依次類推。
[root@localhost ~]# vim user.sh (創建腳本)
#!/bin/bash
# Author: liuguohui
# Date: 2016/09/24 19:32
# Email: liuguohui1008@sina.com
# Description: useradd user10-user19 and password same to useranme and touch file10-file19
# file empty and file10-19 owner-user,owner-group change user10-19,group10-19
#
for i in $(seq 10 19);
do
# useradd user
useradd "user$i"
# set password
echo "user$i" | passwd –stdin "user$i"
# touch file
touch /tmp/file$i
# change owner-user and owner-group
chown "user$i":"user$i" /tmp/file$i
done
[root@localhost ~]# bash user.sh (執行腳本)
Changing password for user user10.
passwd: all authentication tokens updated successfully.
Changing password for user user11.
passwd: all authentication tokens updated successfully.
Changing password for user user12.
passwd: all authentication tokens updated successfully.
Changing password for user user13.
passwd: all authentication tokens updated successfully.
Changing password for user user14.
passwd: all authentication tokens updated successfully.
Changing password for user user15.
passwd: all authentication tokens updated successfully.
Changing password for user user16.
passwd: all authentication tokens updated successfully.
Changing password for user user17.
passwd: all authentication tokens updated successfully.
Changing password for user user18.
passwd: all authentication tokens updated successfully.
Changing password for user user19.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# ll /tmp/ (查詢屬主和屬組)
total 32
-rw-r–r–. 1 user10 user10 0 Sep 24 12:45 file10
-rw-r–r–. 1 user11 user11 0 Sep 24 12:45 file11
-rw-r–r–. 1 user12 user12 0 Sep 24 12:45 file12
-rw-r–r–. 1 user13 user13 0 Sep 24 12:45 file13
-rw-r–r–. 1 user14 user14 0 Sep 24 12:45 file14
-rw-r–r–. 1 user15 user15 0 Sep 24 12:45 file15
-rw-r–r–. 1 user16 user16 0 Sep 24 12:45 file16
-rw-r–r–. 1 user17 user17 0 Sep 24 12:45 file17
-rw-r–r–. 1 user18 user18 0 Sep 24 12:45 file18
-rw-r–r–. 1 user19 user19 0 Sep 24 12:45 file19
原創文章,作者:heianyangguo,如若轉載,請注明出處:http://www.www58058.com/46678
寫的很好,但是還需要注意下排版的問題