vim 好用技巧总结

来源:互联网 发布:软件验收单模板 编辑:程序博客网 时间:2024/04/30 11:51

未完待续


置顶:不要使用Esc

http://vim.wikia.com/wiki/Avoid_the_escape_key

插入模式转命令模式:使用“ ctrl+[ ”或“ ctrl+C”代替Esc


http://www.slideshare.net/c9s/vim-script-programming 

http://vim.wikia.com/wiki/Manipulate_quoted_strings

" If the cursor is in the middle of a quote block when <CR> is pressed" a quote is added to the end of the line followed by a plus, a <CR>" and another quote.function! Quotereturn()  let before=strpart(getline(line(".")), 0, col(".")-1)  "let after=strpart(getline(line(".")), col("."))  if(before =~ '^\([^"]*"\([^"\\]\|\\.\)*"\)*[^"]*"\([^"\\]\|\\.\)*$')    return "\"+\"\<Left>\<CR>\<Right>"  else    return "\<CR>"  endifendfinoremap <CR> <C-R>=Quotereturn()<CR>" If you backspace over a quote and it's a continuation fromanother line" the two strings will be concatenated with quotes, spaces, and + removed.function! Quotebackspace()  if( (strpart(getline(line(".")), 0, col(".")-1) =~ '^\s*"$') && (getline(line(".")-1) =~ "\"+$") )    return "\<Esc>d0kgJhhxxxi"  else    return "\<C-H>"  endifendfinoremap <BS> <C-R>=Quotebackspace()<CR>


http://stackoverflow.com/questions/7560911/vim-do-something-in-a-function-based-on-character-under-a-cursor

I think the easiest way to retrieve the char under cursor is:

getline(".")[col(".")-1]

Alternatively, you can do it with strpart()

strpart(getline("."), col(".")-1, 1)

The first expression first calls the function getline() passing "." as argument which means the line where the cursor is positioned will be returned. Then we use the so called expr8 or expr-[] (see the help) to retrieve a single character. The number passed comes from another function, col() which returns the current cursor column. As indexes start in 0, one is subtracted.

You can use it like

if getline(".")[col(".")-1] == '*'        ...


*** bang! ***


*** reading command output ***

:r filename:r ! ls



*** ctags + include 自动完成 ***

ctrl-n 自动提示相关tag

对 c++ 声明的 virtual 等函数,ctags默认不识别,需要使用以下参数

ctags --c-kinds=+p -R


*** 区域搜索 ***

ctrl-v 选中待搜索区域Esc /\%Vkeyword


*** vim 中,ctags 使用分割窗口打开tag ***

ctrl-w, ctrl-]  Open the definition in a horizontal split


*** 设置TAB和空格替换 ***

在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:

set ts=4  (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab

对于已保存的文件,可以使用下面的方法进行空格和TAB的替换:
TAB替换为空格:

:set ts=4:set expandtab:%retab!

空格替换为TAB:

:set ts=4:set noexpandtab:%retab!


*** vim编辑插入注释 ***

:.,+2s/^/\/\/  在当前行到后两行开头加上 //,需转义

:.,+2s#^#//  使用 #作分隔符,这样不需要对/作转义处理

或者:

按ctrl-v进入可视化,使用j或ctrl-d向下选择行,再用大写I在可视化块的开头插入,输入//,在按Esc


*** 大小写 ***

按v,或(V, ctrl-v)进入可视化,移动选择

gu 变小写

gU 变大写


*** 宏录制 ***

qa, 操作序列, q, @a, @@
qa 开始宏录制,q 结束宏录制,操作记录会在寄存器 a中。
于是 @a 会replay被录制的宏。
@@ 是一个快捷键用来replay最新replay的宏。


加行号:

方法1> 

在第一行开头输入“1.”,按Esc切换回命令模式

qa ctrl-v f. y j P ctrl-a 0 q (注意,中间没有空格,ctrl-a表示将整数加1)

@a

100@@

方法2>

在命令模式下,输入

:g/^/exec "s/^/".strpart(line(".")." ", 0, 4)



*** 删除指定行 ***

删除包含特定字符的行:
:g/pattern/d   (全局删除匹配行)

删除不包含指定字符的行:
:v/pattern/d
:g!/pattern/d


删除偶数行:
:%s/\(^.*$\)\n^.*$/\1/g
删除奇数行:
:%s/^.*$\n\(^.*$\)/\1/g

正则表达式:
匹配任意字符 ( 除换行符 ) .
匹配重复零次或多次前一字符 *
匹配集合中任意字符 [...]
匹配不属集合 中 任意字符 [^...]
匹配行首、行尾 ^, $
匹配词首、词尾 \<, \>
正则表达式 分组 \(...\)
第 1 个分组内容 \1


*** 自动缩进 ***

可视化模式,选中区块后,可执行下列缩进操作

J → 把所有的行连接起来(变成一行)
< 或 > → 左右缩进
= → 自动缩进 

默认会以8个空格(shiftwidth=8)缩进。所以可在.vimrc下做如下设置:

set tabstop=4
set cindent shiftwidth=4


*** 代码折叠 ***

在一个代码块的括号上,比如{,执行:

zf%,即可折叠代码,重新展开zo,展开全部zO。


*** 编辑二进制 ***

使用 vim -b filename 打开文件filename

:set display=uhex 显示16进制值

或者,调用外部xxd程序

:%!xxd  转成16进制

:%!xxd -r 恢复


*** 复制粘贴 ***

编辑~/.vimrc,加上下面这句set pastetoggle=<F5>这样就可以用F5切换成粘贴模式了使用"+寄存器,关联系统剪切板,ubuntu默认vi不支持特殊寄存器,需
$ sudo apt-get install vim-gnome


***  搜索,大小写敏感搜索 ***

编辑 ~/.vimrc,加上

set hlsset background=dark  “black background, use dark, default use light设置搜索高亮,使用dard背景,高亮更加清晰
当需要临时性的关掉搜索高亮时,可以:noh or :nohlsearch 下次重新搜索时,仍会高亮

set ignorecase
set smartcase

当/abc时,对大小写不敏感

当/Abc时,敏感

/abc\C,强制只匹配abc


*** map ***

映射,可能的前缀有:
    nore 表示非递归,默认递归
    n 表示在普通模式下生效
    v 表示在可视模式下生效
    i 表示在插入模式下生效
    c 表示在命令行模式下生效 

常见的:nnoremap表示n+nore+map


*** mapleader ***

重新定义映射

let mapleader = ','
nnoremap <leader>t : tabe<CR>  

这样在命令模式下,输入 “,t” 就可以打开标签页了


*** VIM中如何去掉DOS/Windows文件中的换行符^M  ***

方法一:
1. vim hello.c  (edit the file hello.c)
2. press the key:  Esc
3. enter this string:  %s/^M//g  (^M = Ctrl v + Ctrl m)
4. press the key: enter  (the ^M cleared!)
5. :wq (save the file)
方法二:
执行命令:  dos2unix   hello.c


*** 状态栏显示行号列好  ***

" Always show the status line 

set laststatus=2 

" Format the status line

set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Col:\ %c