vim使用
使用技巧
vim +12 file
vim + file
vim +/pattern file
shfit + 字母
a,i,o,A,I,O
shift + :
esc esc
: 10d
:10,20d
: set nu
: ! ls /var
: wq!
ZZ
箭头 , #箭头 , w , e , b , #w ,#e ,#b ,ctrl + 箭头
0 ,^ , home , $ , end
G , # G , :10
pageUp pageDown
x , #x
d删除
#dw , #de , #db , dd , #dd
:startline,endline
. 当前行
$ 最后一行 $-2
-# , +#
粘贴 p P
复制 同d 搭配光标移动使用
y
yy #yy
:1,10y
y$
y0
#y + 箭头
先删除内容,再转为编辑模式 同d 搭配光标移动使用
c , cc, c$
c# + 箭头
替换
r 字符
5 r 字符
R 进入替换模式,esc退出
撤销 u #u
反撤销 ctrl+r
重复前一次的操作 .
块选取 v V 选取后,可执行其他操作
查找
/pattern
?pattern
n N
查找并替换
: addr1,addr2 s@pattern@string@gi
:.,$-1s@he@HE@g
1,$ 等同于 % 全部行
vim file1 file2 ...
:next 切换到下一个文件
:prev
:first
:last
:qa 全部退出
分屏显示
ctrl+w,s
ctrl+w,v
vim -o file1 file2
vim -O file1 file2
在窗口间切换光标,ctrl+w 箭头
: addr1,addr2 w pathfile 取某内容另存为
: r pathfile 读取另一个文件内容进来
: ! CMD 执行shell命令
:set number :set nu : set nonu
: set ignorecase : set ic : set noignorecase : set noic
: set autoindent : set ai : set noai
: set hlsearch : set nohlsearch
: syntax no : syntax off
vim配置文件 /etc/vimrc ~/.vimrc
.file.swp 文件未正常保存退出会生成这个
vim -r file 删除swp,重新编辑file文件
-a , -o , ! ==》 与,或,非
if [ $# -gt 1 -a $# -le 3 ]
if [ $# -gt 1 ] && [ $# -le 3 ]
练习
练习:将/etc/yum.repos.d/server.repo文件中的ftp://instructor.example.com/pub替换为http://172.16.0.1/yum
%s@ftp://instructor\.example\.com/pub@http://172.16.0.1/yum@g
添加10个用户user1~user10,密码同用户名;但要求只有用户不存在的情况下才能添加;
for i in {1..10}; do
if id user$i &> /dev/null; then
echo "user$i exists."
else
useradd user$i
echo user$i | passwd --stdin user$i &> /dev/null
echo "Add user user$i finished."
fi
done
---------------------------------------------------------
for i in {1..10}; do
if id user$i &> /dev/null; then
userdel -r user$i
echo "Delete user$i finished."
else
echo "user$i not exists."
fi
done
拓展:接受一个参数;
add:添加用户user1..user10
del: 删除用户user1..user10
其它;退出
if [ $# -lt 1 ]; then
echo "Usage: adminuser.sh ARG"
exit 7
fi
if [ $1 == '--add' ]; then
for i in {1..10}; do
if id user$i &> /dev/null; then
echo "user$i exists."
else
useradd user$i
echo user$i | passwd --stdin user$i &> /dev/null
echo "Add user$i finished."
fi
done
elif [ $1 == '--del' ]; then
for i in {1..10}; do
if id user$i &> /dev/null; then
userdel -r user$i
echo "Delete user$i finished."
else
echo "No user$i."
fi
done
else
echo "Unknown ARG"
exit 8
fi
拓展:期望给什么用户就添加,删除什么用户,如下
adminusers.sh --add user1,user2,hello,hi,user3
if [ $# -lt 2 ]; then
echo "Usage: adminuser2.sh ARG1 ARG2."
exit 11
fi
if [ $1 == '--add' ]; then
for i in `echo $2 | sed 's/,/ /g'`; do
if id $i &> /dev/null; then
echo "$i exists."
else
useradd $i
echo $i | passwd --stdin $i &> /dev/null
echo "Add $i finished."
fi
done
elif [ $1 == '--del' ]; then
for i in `echo $2 | sed 's/,/ /g'`; do
if id $i &> /dev/null; then
userdel -r $i
echo "Delete $i finished."
else
echo "$i not exists."
fi
done
elif [ $1 == '--help' ]; then
echo "Usage: adminuser2.sh --add USER1,USER2,... | --del USER1,USER2,... | --help"
else
echo "Unknown options."
exit 12
fi
扩展:-v选项,显示整个执行过程?
if [ $1 == 'q' -o $1 == 'quit' -o $1 == 'Q' -o $1 == 'Quit' ]; then
echo "Quiting..."
exit 0
else
echo "Unknown Argument."
exit 1
fi
扩展:如果用户给的不是这几个字符串,就添加用户,否则退出?
[ ! \( $1 == 'q' -a $1 == 'quit' -a $1 == 'Q' -a $1 == 'Quit' \) ]
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: