天天看點

1.5Vim工具使用詳解

        Vim編輯器是Vi的更新版,在選擇最小化安裝Linux系統,預設沒有安裝Vim工具。

一、安裝Vim

1、查詢安裝包

[root@www ~]# yum search vim

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

 * base: mirrors.btte.net

 * extras: mirror.bit.edu.cn

 * updates: mirror.bit.edu.cn

========================================= N/S Matched: vim =========================================

vim-X11.x86_64 : The VIM version of the vi editor for the X Window System

vim-common.x86_64 : The common files needed by any version of the VIM editor

vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements

vim-filesystem.x86_64 : VIM filesystem layout

vim-minimal.x86_64 : A minimal version of the VIM editor

  Name and summary matches only, use "search all" for everything.

2、安裝Vim

[root@www ~]# yum -y install vim-common vim-enhanced

二、vim的使用

1、vim的一般模式

向下

k

向上

n + j 

向下跳到第n行

n + k

向上跳到第n行

gg

頂部

底部

ctrl + f  

向下翻一頁

ctrl + b

向上翻一頁

shift + $ 

光标移動到行尾

shift + ^ 或者 0 

光标移動到行首

數字n + G  

移動到指定的第n行

dd  

剪切光标所在行

粘貼到下一行

粘貼到上一行

D

剪切目前行光标所在後面的内容

粘貼在光标

P

粘貼在光标後

u

恢複上一步操作

ctrl + r

   撤銷上一步操作

X

向前删除一個字元

向後删除一個字元

數字+x 

向後删除多個字元

數字+X

向前删除多個字元

yy 

複制目前行

p  

向下粘貼yy複制的行

向上粘貼(大寫P)

2+yy 

向下複制兩行

進入可視化模式

2、vim的編輯模式

光标移動到目前後一個字元進入編輯模式

A

光标移動到行尾進入編輯模式

i

在光标目前位置進入編輯模式

I

光标移動到行首進入編輯模式

o

在目前行下另起一行

O

在目前行上另起一行

3、vim的指令模式

在一般模式下使用”:“或”/“顯示該模式在左下角

/word

搜尋字元,n移動到下一個,N移動到上一個

:%,$s/word/new word/g

搜尋第一行到最後一行的word并替換為new word,加g表示全部替換

:%,$s/word/new word/gc

加c表示逐個詢問替換

:1,$s/\/etc\//111111/g  

搜尋包含特殊字元/使用\轉意

:noh

取消高亮

:set nu 

設定行号

:set nonu

取消行号

:sh

跳出vim回到shell,exit傳回vim

:e! 

撤銷回到初始狀态

vim +8 /etc/passwd

打開并定位到第8行

:X 

使用vim工具設定密碼,設定空密碼取消密碼

Warning: Using a weak encryption method; see :help 'cm'

Enter encryption key: ***

Enter same key again: ***

: ! ls  /

在vim下使用shell指令

bin   dev    disk6  home  lib64       media  opt   root  selinux  sys  usr

boot  disk1  etc    lib   lost+found  mnt    proc  sbin  srv      tmp  var

本文轉自 HMLinux 51CTO部落格,原文連結:http://blog.51cto.com/7424593/1720133