好程序員分享Linux重器vi編輯器

建立練習文件
[root@tianyun ~]# ll -a > list.txt
文件編輯器 gedit
文件編輯器 vi, vim, nano vim

vi編輯器工做模式
圖片描述
命令模式:
a. 光標定位
hjkl
0 $
gg G
3G 進入第三行
/string (n N 能夠循環的) 快速定位到某一行
/^d
/txt$c#

b. 文本編輯(少許)
y 複製 yy 3yy ygg yG (以行爲單位)
d 刪除 dd 3dd dgg dG (以行爲單位)
p 粘貼
x 刪除光標所在的字符
D 從光標處刪除到行尾
u undo撤銷
^r redo重作
r 能夠用來修改一個字符bash

c. 進入其它模式
a 進入插入模式
i 進入插入模式
o 進入插入模式
A 進入插入模式less

: 進入末行模式(擴展命令模式)
v 進入可視模式
^v 進入可視塊模式
V 進入可視行模式
R 進入替換模式編輯器

插入模式:
^Pspa

可視塊模式:
塊插入(在指定塊前加入字符): 選擇塊,I 在塊前插入字符, ESC
塊替換: 選擇塊,r 輸入替換的字符
塊刪除: 選擇塊,d | x
塊複製: 選擇塊,y命令行

擴展命令模式:
a. 保存退出
:10 進入第10行
:w 保存
:q 退出
:wq 保存並退出
:w! 強制保存
:q! 不保存並退出
:wq! 強制保存退出
:x 保存並退出 ZZ 3d

b. 查找替換
:範圍 s/old/new/選項
:1,5 s/root/yang/ 從1-5行的root 替換爲yang
:5,$ s/root/yang/ $表示最後一行
:1,$ s/root/yang/g = :% s/root/yang/g %表示全文 g表示全局
:% s#/dev/sda#/var/ccc#g
:,8 s/root/yang/ 從當前行到第8行
:4,9 s/^#// 4-9行的開頭#替換爲空
:5,10 s/./#&/ 5-10前加入#字符 (.整行 &引用查找的內容)日誌

c. 讀入文件/寫文件(另存爲)
:w 存儲到當前文件
:w /tmp/aaa.txt 另存爲/tmp/aaa.txt
:1,3 w /tmp/2.txt
:r /etc/hosts 讀入文件到當前行後
:5 r /etc/hosts 讀入文件到第5行後blog

d. 設置環境
臨時設置:
:set nu 設置行號
:set ic 不區分大小寫
:set ai 自動縮進
:set list 顯示控制字符
:set nonu 取消設置行號
:set noic

永久的環境: 修改vim環境配置文件
/etc/vimrc 影響全部系統用戶
~/.vimrc 影響某一個用戶

vim ~/.vimrc

set ic
set nu

刪除必定要使用相對路徑,不要使用絕對路徑

vim -O /etc/hosts /etc/hostname ^ww切換

cp /etc/hosts hosts1

vimdiff /etc/hosts hosts1

==================================================================

建立文件
[root@localhost ~]# touch aa.txt
[root@localhost ~]# touch aa.txt cc.txt tt.txt
[root@localhost ~]# touch abc{1..10}.txt
[root@localhost ~]# touch {a,b,c}{1..3}.txt

建立目錄
[root@localhost ~]# mkdir abc
[root@localhost ~]# mkdir abcd abce abcf
[root@localhost ~]# mkdir abc{1..5}
[root@localhost ~]# mkdir -p /root/aa/cc
[root@localhost ~]# mkdir -pv /root/aa/cc -p 遞歸 -v 顯示過程

複製 cp
cp 源文件地址 目標地址
[root@localhost ~]# cp /root/abc1.txt /tmp/
[root@localhost ~]# cp /root/abc1.txt /tmp/test.txt 複製過程當中重命名
複製目錄
[root@localhost ~]# cp -r /root/abcd/ /tmp/
[root@localhost ~]# cp -r /root/abcd/ /tmp/aaa 重命名

剪切 mv
cp 源文件地址 目標地址
[root@localhost ~]# mv /root/tt.txt /tmp/
[root@localhost ~]# mv /root/tt.txt /tmp/back.txt
移動目錄
[root@localhost ~]# mv /root/abcf/ /tmp/
[root@localhost ~]# mv /root/abcf/ /tmp/back

刪除 rm rmdir
rmdir 只能刪除空目錄
rm
[root@localhost ~]# rm aa.txt
[root@localhost ~]# rm -f cc.txt
[root@localhost ~]# rm -rf abce/
[root@localhost ~]# rm -rf a1.txt a2.txt
[root@localhost ~]# rm -rf *.txt
[root@localhost ~]# rm -rf abc*
[root@localhost ~]# rm -rf *
[root@localhost ~]# rm -rf /install.log

查看文件內容
head
[root@localhost ~]# head /root/install.log
[root@localhost ~]# head -n 20 /root/install.log
[root@localhost ~]# head -20 /root/install.log

tail
[root@localhost ~]# tail /root/install.log
[root@localhost ~]# tail -20 /root/install.log
[root@localhost ~]# tail -f /var/log/messages 監控日誌文件

more
[root@localhost ~]# more /root/install.log
空格向後一頁 回車向後一行 b 向前一行

less
[root@localhost ~]# less /root/install.log

cat
[root@localhost ~]# cat -n /root/install.log
[root@localhost ~]# cat -n aa.txt 全部行
[root@localhost ~]# cat -b aa.txt 有效行

[root@localhost ~]# cat -n /root/install.log | less

練習:
1.建立如下目錄結構?而且驗證?(一條命令?)
/aa
/ \
bb/ bc/
/ \
ca/ cb/ cc/
[root@localhost ftp]# mkdir -pv /aa/bb/c{a,b} /aa/bb/cc
[root@localhost ftp]# mkdir -pv /aa/b{b/c{a,b},c/cc}
[root@localhost ftp]# ls -R /aa/
[root@localhost ftp]# tree /aa
/aa
├── bb
│ ├── ca
│ └── cb
└── bc
└── cc
[root@localhost ftp]# tree -L 2 / -L level

2.將/etc/man.config拷貝到ca目錄 (2種寫法) 當前目錄時/
[root@localhost ~]# cp /etc/man.config /aa/bb/ca/
[root@localhost /]# cp etc/man.config /aa/bb/ca/

3.將ca下的man.config移動到cc目錄並重命名爲test.txt(2種) 當前目錄bb
[root@localhost bb]# mv /aa/bb/ca/man.config /aa/bc/cc/test.txt
[root@localhost bb]# mv ca/man.config /aa/bc/cc/test.txt

4.兩次拷貝同一文件到同一目錄下,不出現覆蓋提示
[root@localhost ~]# cp /root/install.log /tmp/
[root@localhost bb]# /bin/cp /root/install.log /tmp/install.log

[root@localhost bb]# unalias cp
[root@localhost bb]# cp /root/install.log /tmp/install.log

5.只顯示/root/install.log文件的第1001行
[root@localhost bb]# cat -n /root/install.log | head -1001 | tail -1

VIM文本編輯器
vim的3種基本模式:命令模式,輸入模式,末行模式
由命令模式切換到輸入模式:
a 當前字符後輸入
A 當前行行尾輸入
i 當前字符前輸入
I 當前行行首輸入
o 當前行下一行輸入
O 當前行上一行輸入
s 刪除當前字符後輸入
S 刪除當前行後輸入

命令模式下
u 撤銷一步操做
ctrl+r 重作
複製一行 yy 粘貼p
複製50行 50yy

剪切/刪除一行 dd
剪切/刪除50行 50dd

^行首
$行尾
d^ 刪除當前字符到行首
d$ 刪除當前字符到行尾
y^
y$

替換
:s/ab/**/ 替換當前行匹配到的第一個字符串
:s/ab/**/g 替換當前行全部的匹配字符串 s 替換 g 全局

4.打開新的標籤ctrl+shift+t alt+1..... 切換標籤 ctrl+shift+w 關閉標籤 ctrl+shift+q 關閉全部終端

bin 下的存的是命令

使用Shell的兩種方式:
輸入命令 效率低 適合少許的工做
Shell Script(腳本) 效率高 適合完成複雜,重複性工做
pwd 顯示當前完整目錄

表示咱們當前使用的是超級帳戶

$ 表示咱們當前使用的是普通帳戶
長主機名:zhuangyao.baidu.com
輸入bash命令注意事項:
輸入命令:

命令 【選項】 參數 操做符

ls

全部命令後必須有空格(空格的個數沒有上線)
tab:自動補全
沒補全的緣由: 命令寫錯 沒有可執行權限
ctrl+c :中斷當前的輸入
經常使用快捷鍵:
Ctrl + c:終止命令
Ctrl + 上下左右: 以單詞移動
移到命令行首 home
移到命令行尾 end

思考題:

從光標處刪除至命令行首從光標處刪除至命令行尾

相關文章
相關標籤/搜索