請詳細總結vim編輯器的使用并完成以下練習題
1、vi的基本概念 基本上vi可以分為三種狀態,分別是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能區分如下: 1) 命令行模式command mode) 控制屏幕光標的移動,字符、字或行的刪除,移動復制某區段及進入Insert mode下,或者到 last line mode。 2) 插入模式(Insert mode) 只有在Insert mode下,才可以做文字輸入,按「ESC」鍵可回到命令行模式。 3) 底行模式(last line mode) 將文件保存或退出vi,也可以設置編輯環境,如尋找字符串、列出行號……等。 不過一般我們在使用時把vi簡化成兩個模式,就是將底行模式(last line mode)也算入命令行模式command mode)。 2、vi的基本操作 a) 進入vi 在 系統提示符號輸入vi及文件名稱后,就進入vi全屏幕編輯畫面:$ vi myfile。不過有一點要特別注意,就是您進入vi之后,是處于「命令行模式(command mode)」,您要切換到「插入模式(Insert mode)」才能夠輸入文字。初次使用vi的人都會想先用上下左右鍵移動光標,結果電腦一直嗶嗶叫,把自己氣個半死,所以進入vi后,先不要亂動,轉換到 「插入模式(Insert mode)」再說吧! b) 切換至插入模式(Insert mode)編輯文件 在「命令行模式(command mode)」下按一下字母「i」就可以進入「插入模式(Insert mode)」,這時候你就可以開始輸入文字了。 c) Insert 的切換 您目前處于「插入模式(Insert mode)」,您就只能一直輸入文字,如果您發現輸錯了字!想用光標鍵往回移動,將該字刪除,就要先按一下「ESC」鍵轉到「命令行模式(command mode)」再刪除文字。 d) 退出vi及保存文件 在「命令行模式(command mode)」下,按一下「:」冒號鍵進入「Last line mode」,例如: : w filename (輸入 「w filename」將文章以指定的文件名filename保存) : wq (輸入「wq」,存盤并退出vi) : q! (輸入q!, 不存盤強制退出vi) 3、命令行模式(command mode)功能鍵 1). 插入模式 按「i」切換進入插入模式「insert mode」,按“i”進入插入模式后是從光標當前位置開始輸入文件; 按「a」進入插入模式后,是從目前光標所在位置的下一個位置開始輸入文字; 按「o」進入插入模式后,是插入新的一行,從行首開始輸入文字。 2). 從插入模式切換為命令行模式 按「ESC」鍵。 3). 移動光標 vi可以直接用鍵盤上的光標來上下左右移動,但正規的vi是用小寫英文字母「h」、「j」、「k」、「l」,分別控制光標左、下、上、右移一格。 按「ctrl」+「b」:屏幕往“后”移動一頁。 按「ctrl」+「f」:屏幕往“前”移動一頁。 按「ctrl」+「u」:屏幕往“后”移動半頁。 按「ctrl」+「d」:屏幕往“前”移動半頁。 按數字「0」:移到文章的開頭。 按「G」:移動到文章的最后。 按「$」:移動到光標所在行的“行尾”。 按「^」:移動到光標所在行的“行首” 按「w」:光標跳到下個字的開頭 按「e」:光標跳到下個字的字尾 按「b」:光標回到上個字的開頭 按「#l」:光標移到該行的第#個位置,如:5l,56l。 4). 刪除文字 「x」:每按一次,刪除光標所在位置的“后面”一個字符。 「#x」:例如,「6x」表示刪除光標所在位置的“后面”6個字符。 「X」:大寫的X,每按一次,刪除光標所在位置的“前面”一個字符。 「#X」:例如,「20X」表示刪除光標所在位置的“前面”20個字符。 「dd」:刪除光標所在行。 「#dd」:從光標所在行開始刪除#行 5). 復制 「yw」:將光標所在之處到字尾的字符復制到緩沖區中。 「#yw」:復制#個字到緩沖區 「yy」:復制光標所在行到緩沖區。 「#yy」:例如,「6yy」表示拷貝從光標所在的該行“往下數”6行文字。 「p」:將緩沖區內的字符貼到光標所在位置。注意:所有與“y”有關的復制命令都必須與“p”配合才能完成復制與粘貼功能。 6). 替換 「r」:替換光標所在處的字符。 「R」:替換光標所到之處的字符,直到按下「ESC」鍵為止。 7). 回復上一次操作 「u」:如果您誤執行一個命令,可以馬上按下「u」,回到上一個操作。按多次“u”可以執行多次回復。 8). 更改 「cw」:更改光標所在處的字到字尾處 「c#w」:例如,「c3w」表示更改3個字 9). 跳至指定的行 「ctrl」+「g」列出光標所在行的行號。 「#G」:例如,「15G」,表示移動光標至文章的第15行行首。 4、Last line mode下命令簡介 在使用「last line mode」之前,請記住先按「ESC」鍵確定您已經處于「command mode」下后,再按「:」冒號即可進入「last line mode」。 A) 列出行號 「set nu」:輸入「set nu」后,會在文件中的每一行前面列出行號。 B) 跳到文件中的某一行 「#」:「#」號表示一個數字,在冒號后輸入一個數字,再按回車鍵就會跳到該行了,如輸入數字15,再回車,就會跳到文章的第15行。 C) 查找字符 「/關鍵字」:先按「/」鍵,再輸入您想尋找的字符,如果第一次找的關鍵字不是您想要的,可以一直按「n」會往后尋找到您要的關鍵字為止。 「?關鍵字」:先按「?」鍵,再輸入您想尋找的字符,如果第一次找的關鍵字不是您想要的,可以一直按「n」會往前尋找到您要的關鍵字為止。 D) 保存文件 「w」:在冒號輸入字母「w」就可以將文件保存起來。 E) 離開vi 「q」:按「q」就是退出,如果無法離開vi,可以在「q」后跟一個「!」強制離開vi。 「qw」:一般建議離開時,搭配「w」一起使用,這樣在退出的時候還可以保存文件。 5、vi命令列表 1) 下表列出命令模式下的一些鍵的功能: h左移光標一個字符 l右移光標一個字符 k光標上移一行 j光標下移一行 ^光標移動至行首 0數字“0”,光標移至文章的開頭 G光標移至文章的最后 $光標移動至行尾 Ctrl+f向前翻屏 Ctrl+b向后翻屏 Ctrl+d向前翻半屏 Ctrl+u向后翻半屏 i在光標位置前插入字符 a在光標所在位置的后一個字符開始增加 o插入新的一行,從行首開始輸入 ESC從輸入狀態退至命令狀態 x刪除光標后面的字符 #x刪除光標后的#個字符 X(大寫X),刪除光標前面的字符 #X刪除光標前面的#個字符 dd刪除光標所在的行 #dd刪除從光標所在行數的#行 yw復制光標所在位置的一個字 #yw復制光標所在位置的#個字 yy復制光標所在位置的一行 #yy復制從光標所在行數的#行 p粘貼 u取消操作 cw更改光標所在位置的一個字 #cw更改光標所在位置的#個字
1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
[root@rhel677850 tmp]#vim /tmp/rc.sysinit :%s@^[[:space:]]@#&@
2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[root@rhel677850 tmp]#vim /tmp/grub.conf :%s@^[[:space:]]\+@@
3、刪除/tmp/rc.sysinit文件中的以#開頭,且后面跟了至少一個空白字符的行行的#和空白字符;
[root@rhel677850 tmp]#vim /tmp/rc.sysinit :%s@^#[[:space:]]\+@@
4、為/tmp/grub.conf文件中前三行的行首加#號;
[root@rhel677850 tmp]#vim /tmp/grub.conf :1,3s@^@#@
5、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改為1;
[root@rhel677850 tmp]#vim /etc/yum.repos.d/CentOS-Media.repo :%s@\([enabled=|gpgcheck=]\)0@\11@
6、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名為形如etc-201504020202
[root@rhel677850 tmp]#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@rhel677850 tmp]#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@rhel677850 tmp]#crontab -e 0 */2 * * * /bin/grep "^[sS]" /proc/meminfo > /stats/memory.txt
9、工作日的工作時間內,每兩小時執行一次echo "howdy"
[root@rhel677850 tmp]#crontab -e 0 9-17/2 * * 1-5 /bin/echo "howdy"
10、創建目錄/tmp/testdir-當前日期時間;
[root@rhel677850 tmp]# mkdir -pv /tmp/testdir-$(date +%Y%m%d%H%M) mkdir: created directory `/tmp/testdir-201609221401'
11、在此目錄創建100個空文件:file1-file100
[root@rhel677850 tmp]# cd testdir-201609221401/ [root@rhel677850 testdir-201609221401]# for x in `seq 1 100`;do touch file${x};done [root@rhel677850 testdir-201609221401]# ls file1 file15 file21 file28 file34 file40 file47 file53 file6 file66 file72 file79 file85 file91 file98 file10 file16 file22 file29 file35 file41 file48 file54 file60 file67 file73 file8 file86 file92 file99 file100 file17 file23 file3 file36 file42 file49 file55 file61 file68 file74 file80 file87 file93 file11 file18 file24 file30 file37 file43 file5 file56 file62 file69 file75 file81 file88 file94 file12 file19 file25 file31 file38 file44 file50 file57 file63 file7 file76 file82 file89 file95 file13 file2 file26 file32 file39 file45 file51 file58 file64 file70 file77 file83 file9 file96 file14 file20 file27 file33 file4 file46 file52 file59 file65 file71 file78 file84 file90 file97
12、顯示/etc/passwd文件中位于第偶數行的用戶的用戶名;
[root@rhel677850 testdir-201609221401]# sed '1~2d' /etc/passwd | awk -F: '{print $1}' bin adm sync halt uucp games ftp dbus vcsa avahi-autoipd haldaemon ntp saslauth pulse tcpdump patrol weblogic
13、創建10用戶user10-user19;密碼同用戶名;
[root@rhel677850 testdir-201609221401]# for x in {10..19};do useradd user${x};echo "user${x}" | passwd user${x} --stdin;done 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.
14、在/tmp/創建10個空文件file10-file19;
[root@rhel677850 tmp]# for x in {10..19};do touch file${x};done [root@rhel677850 tmp]# ls file10 file13 file16 file19 keyring-rUYcu0 rc.sysinit vmware-tools-distrib file11 file14 file17 grub.conf lost+found rhel67.repo file12 file15 file18 hsperfdata_patrol pulse-ziwLymx5Xbwx testdir-201609221401
原創文章,作者:zhangxiaola,如若轉載,請注明出處:http://www.www58058.com/48464
寫的很好,在注意一下排版的問題吧