1、配置文件:/etc/vimrc
设置行号: set number
自动缩进: set autoindent
C缩进 : set cindent
缩进宽度: set shiftwidth=4
空格代替TAB: set expandtab
set tabstop=4 # 空格数量是4
显示列号: set ruler
高亮显示搜索字符: set hlsearch
2、在正常模式下输入“!”,后可根shell命令
3、高亮度显示单词:set hlsearch
取消高亮显示 :noh
4、nx删除n个字符
5、vsplit 新文件 ctrl+w w切换窗口
:res[ize] +/-N //改变高度
:vert res[ize] +/-N //改变宽度
6、跳转到页首:gg (老的vi不支持,用":1"跳转到第一行)
跳转到页尾:GG
7、read ! echo /home/duanbei/test.c
8、复制:yy,复制n行:nyy,粘贴:p
9、"%s/aa/bb/g " 将当前文件中所有"aa"替换成"bb"
10、二进制文件编辑
1) 十六进制显示:"%!xxd"
2) 编辑十六进制部分内容
3) "%!xxd -r" 转回文本模式(必须这样做,否则保存下来的就是显示的十六进制文本)
4) "wq"存盘
11. 搜索:
/单词 正向查找单词
?单词 反向查找单词
12. 记得以前老的vi会显示DOS结束符"^M",现在不显示了,用":e ++ff=unix %"可显示。另外用cat -v file 也可看出"^M"
转成unix格式:
:set fileformat=unix
13. 多行缩进:
(1) 按"v"进入visual模式
(2) 用上下键选择要缩进的行
(3) 手动缩进: 用">""<"键进行缩进
自动缩进: 如果编辑的是代码文件,直接按"="键就可以了
14. 大小写转换: 按 '~' 键可将光标处的字符转换大小写,可结合Visual模式使用
15. 行首行尾跳转
行首: 绝对行首'0', 第一个非空字符'^'
行尾: '$'
16.
u undo
. repeat last modify
cw change word => manul replace
x delete backwords
A insert end of the line
17. 显示当前正在编辑的文件名: ":f"
18. 禁止vim在换行时自动添加"//"注释符
如果当前行开头是注释符"//",那么在换行的时候vim会在新行自动添加"//",挺烦的
解决方法:在"/etc/vimrc"中填加如下内容
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
vim + ctags + taglist + cscope
Install:
1. ctags
sudo apt-get install ctags
2. taglist
(1) download taglist
http://www.vim.org/scripts/script.php?script_id=273
(2) unzip taglist and copy "plugin" sub-directory to /etc/vim/
#maybe not must
#(3) add
# let Tlist_Ctags_Cmd="/usr/bin/ctags"
# to
# /etc/vim/plugin/taglist.vim
3. cscope:
(1) sudo apt-get install cscope
(2) download 'cscope_maps.vim' from
http://cscope.sourceforge.net/cscope_vim_tutorial.html
and copy 'cscope_maps.vim' to '/etc/vim/plugin'
Setup before Usage:
Enter your code root directory, run these commands:
(1) ctags -R
(2) cscope-indexer -r
Usage in Vim:
taglist:
1. show symbol/function list: Tlist
ctags:
1. show symbol definition:
:tselect symbol_name
this command will show all the definition symbol you specified , choose one and input it's number,
then will jump to the definiction place
2. symbol(function or gloabal variable) definition jump(make sure cursor is on the symbol):
goto: ctrl + ]
return: ctrl + t or ctrl + o
reforword: ":tag"
if exists multi same tag, such as have two hello() function definition
jump to the first symbol: press '1' button, then press 'ctrl + ]'
jump to the second symbol: press '2' button, then press 'ctrl + ]'
3. local variable definition jump(make sure cursor is on the symbol):
goto: gd
return: ctrl + o
4. open new window to show symbol definition: ptag 'symbol'
close ptag: pc
cscope:
1. jump to symbol caller:
cs f s symbol_name