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.www58058.com/67283

(0)
和風細雨和風細雨
上一篇 2017-02-01
下一篇 2017-02-01

相關推薦

  • 0806文本處理工具

    ———- 文件查看工具 ———- cat:    cat [OPTION]… [FILE]…   //查看文本文件內容,一般后面跟文件名(相對路徑),或者是文件名(絕對路徑) -n 加行號 -b 加行號,…

    Linux干貨 2016-08-07
  • 系統啟動流程相關概念

    前言: 了解系統內核基本知識 內核功能:進程管理、內存管理、網絡協議棧、文件系統、驅動程序、安全功能等  用戶空間:應用程序其中有進程或者線程 運行中的系統可分為兩層:內核空間、用戶空間  內核設計流派:  單內核設計:把每種功能集成于一個程序中;例如:linux 微內核設計:每種功能使用一個單獨的子系統實現;例如:Window…

    Linux干貨 2016-09-19
  • 文件、目錄2——Linux基本命令(8)

    1.復制文件和目錄 cp  SRC  DEST 源 目的地 規則: 如果目標目錄下還有一個目錄與源文件同名,則無法復制:            -i 覆蓋前提示        &n…

    2017-07-18
  • 腳本

    1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;     #!/bin/bash     #     sum=0 &n…

    Linux干貨 2016-12-23
  • linux 常用命令

    linux   常用命令:      pwd: printing working directory                     顯示工作目錄       cd:cha…

    Linux干貨 2016-10-28
  • 什么是文件系統

    文件系統:層級結構;有索引; /: 原初起點; 倒置樹狀結構; /dev/pts/2: 最左側/: 表示根目錄 其它的/: 表示路徑分隔符 Linux的路徑分隔符是/ Windows的是\ 文件的路徑表示: 絕對路徑:從根開始表示出的路徑  相對路徑:從當前位置開始表示出的路徑 文件名使用法則: 嚴格區分字符大小寫:file1, File1, FI…

    Linux干貨 2016-10-29

評論列表(1條)

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

    非常的詳細和認真,加油,再接再勵。

欧美性久久久久