馬哥教育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.www58058.com/49754

(0)
N22_熊寶N22_熊寶
上一篇 2016-10-09
下一篇 2016-10-09

相關推薦

  • N26-第三周作業-邢巖

    馬哥門徒-N26-邢巖   “精深練習×一萬小時=世界級技能”。髓鞘質是不可逆的,就讓我們包裹一層厚厚的髓鞘質吧!今天繼續我的練習。   第一題,列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可  ~]# who | cut -d' ' -f1 | sort -u &nbs…

    Linux干貨 2017-02-14
  • 硬盤分區MBR和GPT選哪個好?有什么區別?

    當前主流的硬盤分區方式有兩種:MBR和GPT。 一、MBR與GPT簡介與結構 什么是MBR?         MBR,全稱為Master Boot Record,即硬盤的主引導記錄。是對IBM兼容機的硬盤或者可移動磁盤分區時,在驅動器最前端的一段引導扇區。 MBR的組成部分       &…

    Linux干貨 2016-08-29
  • 最簡單的Linux系統——更加深入了解Linux啟動過程

    自制一個最簡單的Linux: 1、有一個新的磁盤,并創建分區 2、掛載分區,創建目錄 3、拷貝內核文件 4、創建MBR和grub.conf文件 5、創建/etc/fstab文件,設置開機自動掛載 6、拷貝一個bash程序 7、卸載分區,以新磁盤重啟系統

    Linux干貨 2016-09-11
  • 簡單路由實驗

    今天學習了路由相關的基礎知識,為了加深印象,做了如下的一個實驗。根據下面的網絡拓撲圖分別配置兩臺PC和路由,以實現PC1和PC2能夠互相ping通。實驗環境為VM虛擬機 在實驗開始前,我們需要在路由添加兩塊網卡,PC機添加一塊網卡,此實驗網卡的鏈接方式是橋接,一共需要4臺虛擬機,兩臺做PC機,兩臺做路由器 1、R2路由器的配置 [root@linuxpao&…

    Linux干貨 2016-09-05
  • 第六周作業

    博客具體內容請移步博客園:http://www.cnblogs.com/wangenzhi/p/6295141.html

    Linux干貨 2017-01-17
  • Ansible淺談

    ansible特性:         模塊化,調用特定的模塊,完成特定的任務;         基于Python語言實現,由Paramiko、PyYAML和Jinja2三個關鍵模塊;         部署簡單,agentless; &nbs…

    Linux干貨 2016-12-15

評論列表(1條)

  • luoweiro
    luoweiro 2016-10-11 23:48

    作業整理的非常詳細,對于vim還是希望能多加運用,熟能生巧。

欧美性久久久久