1. 程式人生 > >5.1 vim介紹 5.2 vim顏色顯示和移動光標 5.3 vim一般模式下移動光標 5.4 vim一般模式下復制、剪切和粘貼

5.1 vim介紹 5.2 vim顏色顯示和移動光標 5.3 vim一般模式下移動光標 5.4 vim一般模式下復制、剪切和粘貼

介紹 vim 顏色顯示

  • 5.1 vim介紹

  • 5.2 vim顏色顯示和移動光標

  • 5.3 vim一般模式下移動光標

  • 5.4 vim一般模式下復制、剪切和粘貼

5.1 vim介紹

  • vim 是 vi的一個升級版本

  • vim 是帶有顏色顯示的

  • 技術分享

  • 再試下vim 命令

[[email protected] ~]# vi /etc/passwd[[email protected]
/* */ ~]# vim /etc/passwd-bash: vim: 未找到命令 [[email protected] ~]# yum install -y vim-enhanced
  • 運行yum install -y vim-enhanced 命令

技術分享

  • 一般模式

  • 一般模式做的有 比如DD命令 把一行刪掉,還可以把其中某些行復制粘貼到其他行下面去,還沒進入編輯模式之前可以做的一些操作

  • 編輯模式

  • 編輯模式 就是按完i之後,進入到編輯模式,可以針對這個文檔進行操作,增加字符,刪除字符,回車換行等等,進入編輯模式按i鍵或者a鍵

  • 命令模式

  • vi之前講的 less去裏面的/搜索,vim裏面也可以,可以進行查找替換批量操作,比如把文檔裏面123,改為456 包括:wq 保存退出 這也是命令模式

  • 這些模式下有好多用法,這一章節主要針對這三個模式介紹vim的用法

5.2 vim顏色顯示和移動光標

[[email protected] ~]# vim /etc/passwd

技術分享

[[email protected] ~]# vim /etc/passwd[[email protected] ~]# cp /etc/passwd /tmp/[[email protected] ~]# vim /tmp/passwd
  1. 把/etc/paasswd 拷貝到 /tmp/目錄下再用vim打開/tmp/passwd文件顏色就沒有了 技術分享 /etc/下面的文件往往都是配置文件,在/etc/目錄下就可以顯示顏色,把它放到其他目地方它就不會顯示顏色 這是它的一個特性

    技術分享

[[email protected] ~]# vim /etc/fstab
[[email protected] ~]# cp /etc/fstab /tmp/
[[email protected] ~]# vim /tmp/fstab

技術分享

  1. 同樣的/etc/目錄 拷貝到/tmp/目錄下fstab文件就有顏色顯示,而passwd就沒有顏色,因為fstab文件裏面有以#開頭的行 技術分享

  2. 下面來做一個實驗 先用打開vim/tmp/passwd文件吧這個文件做一個變更


[[email protected] ~]# vim /tmp/passwd
root:x:0:0:root:/root:/bin/bashbin:x:1:1:
bin:/bin:/sbin/nologindaemon:x:2:2:
daemon:/sbin:/sbin/nologinadm:x:3:4:
adm:/var/adm:/sbin/nologinlp:x:4:7:
lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:
sync:/sbin:/bin/syncshutdown:x:6:0:
shutdown:/sbin:/sbin/shutdownhalt:x:7:0:
halt:/sbin:/sbin/haltmail:x:8:12:
mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:
operator:/root:/sbin/nologingames:x:12:100:
games:/usr/games:/sbin/nologinftp:x:14:50:
FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:
Nobody:/:/sbin/nologinsystemd-bus-proxy:x:999:997:
systemd Bus Proxy:/:/sbin/nologinsystemd-network:x:192:192:
systemd Network Management:/:/sbin/nologindbus:x:81:81:
System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:
Account used by the trousers package to sandbox the tcsd 
daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:
Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1005::/home/aming:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/aming111:/sbin/nologinuser5:x:1007:1007::/home/user5:/bin/bash
user6:x:1008:1010::/home/user6:/bin/bash

敲2個小g 把光標挪到最上面去,再按大寫的O 就可以把光標定位到剛剛位置的所在的上一行去,並且進入編輯模式

技術分享 在前面倆行隨便寫點東西

技術分享

保存退出,然後再一次編輯它

技術分享 會發現確實也會有顏色,但是這顏色只在前面倆行出現,哪怕在中間插入也還是有顏色 技術分享 這個表示,其實這個文件本身,它並不是沒有顏色,只不過咱們給它的內容不具備,它不具備顯示顏色的條件。

給它改下名字

[[email protected] ~]# mv /tmp/passwd /tmp/passwd.sh[[email protected] ~]# vim /tmp/passwd.sh

技術分享

改下名字會發現也有顏色,不僅僅跟第一行的內容有關系,也和文件的名字有關系,總之,vim顯示顏色的條件很多,最終的結果也不太一樣,大家不用糾結什麽時候顯示顏色,往往是根據文件的具體內容有關系 vim 有自己的配置文件,在vim /etc/vimrc下(這點做個了解) 可以通過vim /etc/vimrc 去編輯配置文件,當然也可以在用戶自己的家目錄下可以編輯

[[email protected] ~]# vim /root/.vimrc

默認這個目錄不存在,當遇到一些vim比較高級的用法的時候,搜文檔文件,可以嘗試在這個配置文件當中去配置。

5.3 一般模式下移動光標

  1. 技術分享

首先拷貝一個文件

[[email protected] ~]# cp /etc/dnsmasq.conf /tmp/1.txt
[[email protected] ~]# vim /tmp/1.txt# Configuration file for dnsmasq.
## Format is one option per line, legal options are the same# as the long options legal on the command line. See
# "/usr/sbin/dnsmasq --help" or "man 8 dnsmasq" for details.
# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.
#port=5353# The following two options make you a better netizen, since they
# tell dnsmasq to filter out queries which the public DNS cannot
# answer, and which load the servers (especially the root servers)
# unnecessarily. If you have a dial-on-demand link they also stop
# these requests from bringing up the link unnecessarily.
# Never forward plain names (without a dot or domain part)#domain-needed
# Never forward addresses in the non-routed address spaces.#bogus-priv
# Uncomment this to filter useless windows-originated DNS requests# which can trigger dial-on-demand links needlessly.
# Note that (amongst other things) this blocks all SRV requests,
# so don‘t use it if you use eg Kerberos, SIP, XMMP or Google-talk.
# This option only affects forwarding, SRV records originating for

如果想讓它顯示顏色,把名字改下

[[email protected] ~]# vim /tmp/1.txt
[[email protected] ~]# mv /tmp/1.txt /tmp/xxx.conf
[[email protected] ~]# vim !$vim /tmp/xxx.conf

這裏的!$ 表示上一次命令的最後一次命令 技術分享

在vim 裏面 第一行 前面加上#!/bin/bash 表示他是一個shell 地下也會有顏色 技術分享 技術分享

技術分享

  • vim 進入文件配置內容後,方向鍵可以不用小鍵盤

  • h 向左 按數字10 再按h 就向左移動了10個字符,

  • 按l向右移動,同樣按數字 9再按l 向右移動9個字符

  • 按j 向上 同樣按數字 5再按j 向上移動9個字符

  • 按k 向下 同樣按數字 5再按k向下移動5個字符

  • 上下左右 都是支持 數字 + 按鍵方向的

  • 空格鍵也是向右移動的 鍵位

  1. Ctrl + B 文本頁面向前翻一頁

  2. Ctrl + F 文本頁面向後翻一頁

  3. 數字0 shift+6 可以快速移動到本行的行首

  4. shift+4 可以快讀移動到本行的行尾

  5. 快速移動到首行 按倆下 小gg 到首行 大寫的 G 快速移動到 行尾

  6. nG 任意數字 + G 定位到移動到第n 行

5.4 一般模式下復制剪切黏貼

技術分享

  • dd刪除、剪切光標所在的那一行

  • ndd 刪除、剪切光標所在行之後的n行

  • yy復制光標所在行

  • nyy 從光標所在行開始,向下復制n行

  • p 從光標所在行開始,向下黏貼已經復制或者黏貼內容

  • 大寫P 從光標所在行開始,向上黏貼已經復制或者黏貼內容

  • u 還原上一步操作,撤銷的意思

  • Ctrl r 反著恢復

  • x表示向後刪除一個字符,大寫的X表示向前刪除一個字符

  • nx 向後刪除n個字符 -按v後 移動光標會選中指定字符,然後可以實現復制、黏貼等操作


5.1 vim介紹 5.2 vim顏色顯示和移動光標 5.3 vim一般模式下移動光標 5.4 vim一般模式下復制、剪切和粘貼