- 文件常用命令less、more、tail、head、cat
- vim编辑器使用
-
常用命令
-
cat、less、more command file more和less可以分屏查看文件,more翻屏至尾部后自动退出,less可以往回翻屏查看 注:more和less在没有退出时可以按v键,默认调用vi编辑器进入编辑命令界面,进行编辑操作。
-
tail:查看文件的后几行,默认查看后十行 tail [options] file -n # :查看后几行 -# -f :查看文件尾部内容结束后不退出,跟随显示新增的行 1.查看刷新的日志 2.日志排查 tail -f /var/log/messages | grep -i error
-
head:查看文件的前几行,默认前10行 head [options] file 常用opts: -n # :查看前几行 -#
-
-
vim编辑器使用 vi:visual interface,vim :vi improved增强版 基本模式:命令模式、输入模式、末行模式 vim file 进入默认为命令模式,三者间切换: i:insert,在光标所在处插入 a: append,在光标所在处后插入 o:在光标所在处的下方添加一个新行 I: 在光标所在行的行首插入 A:在光标所在行的行尾插入 O:在光标所在处的上方添加一个新行
-
打开文件 vim [options] file...
-
关闭文件 ZZ 命令行模式,保存退出 :q 退出 :q! 强制退出 :wq 保存并退出 :x 等同于:wq :w /path/to/somefile保存至给定文件
-
光标跳转 字符跳转方向键 单词间跳转: w:下一个单词词首 e:当前或后一个单词词尾 b:当前或前一个单词词首 行首行尾行间跳转: ^ | home键:跳至行首第一个非空白字符 0:跳至行首 $ | end键:跳至行尾 #G:跳至#指定行 1G,gg:第一行 G:跳到最后一行 翻屏操作: Ctrl+f:向文件尾翻一屏 Ctrl+b:向文件首部翻一屏 Ctrl+d:向文件尾部翻半屏 Ctrl+u:向文件首部翻半屏 Enter:按行向后翻
-
vim命令模式
-
字符编辑:
x:删除光标所在处的字符;
#x:删除光标所在处起始的#个字符;
替换命令(replace):
r:替换光标所在处的字符;
rCHAR
删除命令:
d:删除命令,可结合光标跳转字符,实现范围删除;
d$:删除光标所在处至行尾的所有字符
d^: 删除光标所在处至行首的所有字符
dw:向后删除光标所在处的单词,但光标所在的字符不会删除
de:向后删除光标所在处的单词
db:向前删除光标所在处的单词
dd:删除光标所在处的行;
#dd:删除光标所处的行起始的共#行;
粘贴命令:
p:缓冲区中的内容如果为整行,则粘贴在当前光标所在行的下方;否则,则粘贴至当前光标所在处的后方;
P:缓冲区中的内容如果为整行,则粘贴在当前光标所在行的上方;否则,则粘贴至当前光标所在处的前方;
复制命令:y 复制,工作行为相似于d命令;
y$
y^
y0
ye
yw
yb
yy:复制一整行
#yy:复制光标处起#行
其它编辑操作:
可视化模式:
v:按字符选定
V:按行选定
结合编辑命令使用:d, c, y实现批量操作
撤销操作:
u:撤销此前的操作;
#u:撤销此前的#个操作;
恢复此前的撤销:
Ctrl+r
- vim末行模式
地址定界
:start_pos[,end_pos]
#:指定第#行
.:当前行;
$:最后一行;
#,#:指定行范围,左侧为起始行,右侧为结束行;
#,+#:指定行范围,左侧为超始行绝对编号,右侧为相对左侧行号的偏移量;例如:5,+2
.,$-1:当前行至倒数第一行
1,$:全文
%:全文
/pattern/:从光标所在处起始向文件尾部第一次被模式所匹配到的行;
/first/,$
/pat1/,/pat2/:从光标所在处起始,第一次由pat1匹配到的行开始,至第一次由pat2匹配到的行结束之间的所有行;
可同编辑命令一同使用,实现编辑操作:d、y、c
查找: /PATTERN:从当前光标所在处向文件的尾部查找能够被当前模式匹配到的所有字符串; ?PATTERN:从当前光标所在处向文件的首部查找能够被当前模式匹配到的所有字符串; n:下一个 N:上一个 注:搜索区分大小写,set ignorecase可以忽略 查找并替换: s:末行模式的命令;使用格式:s/ ../ ../ s/old_content/new_content/修饰符 old_content:可使用正则表达式; new_content:不可以使用下则表达式,但可以引用; 修饰符: i:忽略大小写; g:全局替换 注:分隔符替换为其它非常用字符 s@@@ s###
-
vim其他使用功能 编辑多个文件: vim [-o|-O] file1 file2 ... -o:水平分割窗口 -O:垂直分割 窗口间切换:Ctrl+w,方向键切换即可 注:单个文件也可以分屏:Ctrl+w,s/v #s表示水平分割,v表示垂直分割
-
vim的配置文件:可以定制vim的工作特性 注意:在末行模式下的设定,仅对当前vim进程有效;永久有效: 全局:/etc/vimrc 用户个人:~/.vimrc 可以使用帮助:help
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set fileencodings=ucs-bom,utf-8,latin1
endif
set nocompatible " Use Vim defaults (much better!)
set bs=indent,eol,start " allow backspacing over everything in insert mode
set ai " always set autoindenting on
set sw=4
set si "smart suo jin
"set backup " keep a backup file
set viminfo='20,\"50 " read/write a .viminfo file, don't store more
" than 50 lines of registers
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set tabstop=4
" Only do this part when compiled with support for autocommands
if has("autocmd")
augroup redhat
autocmd!
" In text files, always limit the width of text to 78 characters
" autocmd BufRead *.txt set tw=78
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ endif
" don't write swapfile on most commonly used directories for NFS mounts or USB sticks
autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
" start with spec file template
autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
augroup END
endif
if has("cscope") && filereadable("/usr/bin/cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add $PWD/cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endif
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
filetype plugin on
if &term=="xterm"
set t_Co=8
set t_Sb=[4%dm
set t_Sf=[3%dm
endif
" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"
"set auto add shellscript title #设置脚本xxx.sh默认配置
autocmd BufNewFile *.py,*.cc,*.sh,*java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == "sh"
call setline(1, "#!/bin/bash")
call setline(2, "#Author:tong")
call setline(3, "#Blog:http://www.xuetong.51cto.com")
call setline(4, "#Time:".strftime("%F %T"))
call setline(5, "#Name:".expand("%"))
call setline(6, "#Version:v1.0")
call setline(7, "#Description:this is a test script")
endif
endfunc