12月 082010
 

当你混合使用vim和其它编辑器时,你会发现,vim会在文件末尾添加空行(或者说是"\n" 或 "\r\n"),很讨厌是吧?vim很贱是吧? 怎么办呢?

姜源教你怎么做,设置:

:set noendofline binary

每次都设置,很麻烦,是吧? 写在.vimrc 里面

相关参考: http://www.comanswer.com/question/vim-disable-automatic-newline-at-end-of-file

 Posted by at 上午 12:36
2月 092009
 

1. 折叠方式
可用选项 ‘foldmethod’ 来设定折叠方式:set fdm=*****
有 6 种方法来选定折叠:
          manual           手工定义折叠
          indent             更多的缩进表示更高级别的折叠
          expr                用表达式来定义折叠
          syntax             用语法高亮来定义折叠
          diff                  对没有更改的文本进行折叠
          marker            对文中的标志折叠

注意,每一种折叠方式不兼容,如不能即用expr又用marker方式,我主要轮流使用indent和marker方式进行折叠。

使用时,用:set fdm=marker 命令来设置成marker折叠方式(fdm是foldmethod的缩写)。
要使每次打开vim时折叠都生效,则在.vimrc文件中添加设置,如添加:set fdm=syntax,就像添加其它的初始化设置一样。

2. 折叠命令
选取了折叠方式后,我们就可以对某些代码实施我们需要的折叠了,由于我使用indent和marker稍微多一些,故以它们的使用为例:
如果使用了indent方式,vim会自动的对大括号的中间部分进行折叠,我们可以直接使用这些现成的折叠成果。
在可折叠处(大括号中间):
zc      折叠
zC     对所在范围内所有嵌套的折叠点进行折叠
zo      展开折叠
zO     对所在范围内所有嵌套的折叠点展开
[z       到当前打开的折叠的开始处。
]z       到当前打开的折叠的末尾处。
zj       向下移动。到达下一个折叠的开始处。关闭的折叠也被计入。
zk      向上移动到前一折叠的结束处。关闭的折叠也被计入。

当使用marker方式时,需要用标计来标识代码的折叠,系统默认是{{{和}}},最好不要改动之:)
我们可以使用下面的命令来创建和删除折叠:
zf      创建折叠,比如在marker方式下:
                   zf56G,创建从当前行起到56行的代码折叠;
                   10zf或10zf+或zf10↓,创建从当前行起到后10行的代码折叠。
                   10zf-或zf10↑,创建从当前行起到之前10行的代码折叠。
                   在括号处zf%,创建从当前行起到对应的匹配的括号上去((),{},[],<>等)。
zd      删除 (delete) 在光标下的折叠。仅当 ‘foldmethod’ 设为 "manual" 或 "marker" 时有效。
zD     循环删除 (Delete) 光标下的折叠,即嵌套删除折叠。
          仅当 ‘foldmethod’ 设为 "manual" 或 "marker" 时有效。
zE     除去 (Eliminate) 窗口里“所有”的折叠。
          仅当 ‘foldmethod’ 设为 "manual" 或 "marker" 时有效。

关于vim的代码折叠,小弟也是初学,仅做参考。

 Posted by at 下午 7:34
11月 152008
 

vim,编辑器之王。

一般的,vim打开中文文件时会出现乱码,原因比较复杂,不罗嗦了。直接讲解决办法

set fileencoding=gb18030
set fileencodings=utf-8,gb18030,utf-16,big5

想看这样设置的原因吗?请继续。下文在网络中广泛流传

vim里面的编码主要跟三个参数有关:enc(encoding), fenc(fileencoding)和fencs(fileencodings)

其中fenc是当前文件的编码,也就是说,一个在vim里面已经正确显示了的文件(前提是你的系统环境跟你的enc设置匹配),你可以通过改变 fenc后再w来将此文件存成不同的编码。比如说,我:set fenc=utf-8然后:w就把文件存成utf-8的了,:set fenc=gb18030再:w就把文件存成gb18030的了。这个值对于打开文件的时候是否能够正确地解码没有任何关系。

fencs就是用来在打开文件的时候进行解码的猜测列表。文件编码没有百分百正确的判断方法,所以vim只能猜测文件编码。比如我的vimrc里面这个的设置是

set fileencodings=utf-8,gb18030,utf-16,big5

所以我的vim每打开一个文件,先尝试用utf-8进行解码,如果用utf-8解码到了一半出错(所谓出错的意思是某个地方无法用utf-8正确地解码),那么就从头来用gb18030重新尝试解码,如果gb18030又出错(注意gb18030并不是像utf-8似的规则编码,所以所谓的出错只是说某个编码没有对应的有意义的字,比如0),就尝试用utf-16,仍然出错就尝试用big5。这一趟下来,如果中间的某次解码从头到尾都没有出错,那么 vim就认为这个文件是这个编码的,不会再进行后面的尝试了。这个时候,fenc的值就会被设为vim最后采用的编码值,可以用:set fenc?来查看具体是什么。

当然这个也是有可能出错的,比如你的文件是gb18030编码的,但是实际上只有一两个字符是中文,那么有可能他们正好也能被utf-8解码,那么这个文件就会被误认为是utf-8的导致错误解码。

至于enc,其作用基本只是显示。不管最后的文件是什么编码的,vim都会将其转换为当前系统编码来进行处理,这样才能在当前系统里面正确地显示出来,因此enc就是干这个的。在windows下面,enc默认是cp936,这也就是中文windows的默认编码,所以enc是不需要改的。在 linux下,随着你的系统locale可能设为zh_CN.gb18030或者zh_CN.utf-8,你的enc要对应的设为gb18030或者 utf-8(或者gbk之类的)。

最后再来说一下新建空文件的默认编码。看文档好像说会采用fencs里面的第一个编码作为新建文件的默认编码。但是这里有一个问题,就是fencs 的顺序跟解码成功率有很大关系,根据我的经验utf-8在前比gb18030在前成功率要高一些,那么如果我新建文件默认想让它是gb18030编码怎么办?一个方法是每次新建文件后都:set fenc=gb18030一下,不过我发现在vimrc里面设置fenc=gb18030也能达到这个效果。

另外,在ubuntu中文论坛还有人提出了这样的办法,直接就配置了

所有代码直接粘贴到终端运行即可!
安装程序
代码:
sudo apt-get install vim-gtk vim-doc cscope

创建启动项
代码:

cat > /usr/share/applications/gvim.desktop << "EOF"
[Desktop Entry]
Name=Gvim
Comment[zh_CN]=Gvim编辑器
Exec=gvim
Icon=/usr/share/pixmaps/gnome-word.png
Terminal=false
X-MultipleArgs=false
Type=Application
Categories=Application;Development;
Encoding=UTF-8
StartupNotify=true
EOF

locale为zh_CN.utf8的配置文件
代码:

cat > $HOME/.vimrc << "EOF"
"===========================================================================
" 项目: gvim 配置文件
" 作者: yonsan [QQ:82555472]
" 安装: sudo apt-get install vim-gtk
" 用法: 将本文件(.vimrc)拷贝到$HOME/
"===========================================================================

" 使用 murphy 调色板
colo murphy
" 设置用于GUI图形用户界面的字体列表。
set guifont=SimSun 10
"
set nocompatible
" 设定文件浏览器目录为当前目录
set bsdir=buffer
set autochdir
" 设置编码
set enc=utf-8
" 设置文件编码
set fenc=utf-8
" 设置文件编码检测类型及支持格式
set fencs=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
" 指定菜单语言
set langmenu=zh_CN.UTF-8
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" 设置语法高亮度
set syn=cpp
"显示行号
set nu!
" 查找结果高亮度显示
set hlsearch
" tab宽度
set tabstop=4
set cindent shiftwidth=4
set autoindent shiftwidth=4
" C/C++注释
set comments=://
" 修正自动C式样注释功能 <2005/07/16>
set comments=s1:/*,mb:*,ex0:/
" 增强检索功能
set tags=./tags,./../tags,./**/tags
" 保存文件格式
set fileformats=unix,dos
" 键盘操作
map gk
map gj
" 命令行高度
set cmdheight=1
" 使用cscope
if has("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 cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endi
" 中文帮助
if version > 603
set helplang=cn
endi
EOF

locale为zh_CN.gbk的配置文件
代码:

cat > $HOME/.vimrc << "EOF"
"===========================================================================
" 项目: gvim 配置文件
" 作者: yonsan [QQ:82555472]
" 安装: sudo apt-get install vim-gtk
" 用法: 将本文件(.vimrc)拷贝到$HOME/
"===========================================================================

" 使用 murphy 调色板
colo murphy
" 设置用于GUI图形用户界面的字体列表。
set guifont=SimSun 10
"
set nocompatible
" 设定文件浏览器目录为当前目录
set bsdir=buffer
set autochdir
" 设置编码
set enc=chinese
" 设置文件编码
set fenc=chinese
" 设置文件编码检测类型及支持格式
set fencs=gbk,utf-8,ucs-bom,gb18030,gb2312,cp936
" 指定菜单语言
set langmenu=zh_CN.GBK
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" 设置语法高亮度
set syn=cpp
"显示行号
set nu!
" 查找结果高亮度显示
set hlsearch
" tab宽度
set tabstop=4
set cindent shiftwidth=4
set autoindent shiftwidth=4
" C/C++注释
set comments=://
" 修正自动C式样注释功能 <2005/07/16>
set comments=s1:/*,mb:*,ex0:/
" 增强检索功能
set tags=./tags,./../tags,./**/tags
" 保存文件格式
set fileformats=unix,dos
" 键盘操作
map gk
map gj
" 命令行高度
set cmdheight=1
" 使用cscope
if has("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 cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endi
" 中文帮助
if version > 603
set helplang=cn
endi
EOF

 Posted by at 上午 7:33
8月 282008
 

1. 经常遇到vim中backspace不能删除字符的问题,下面是解决办法:

在vimrc中添加如下内容即可:

se nocompatible
se bs=2

具体含义,参看
:help
compatible

vi中就不要这么弄了,不支持

2. vim中方向键不好使的解决办法

set t_ku=^[OA
set t_kd=^[OB
set t_kr=^[OC
set t_kl=^[OD

不过可能和term有关系,set term=xterm 试试

 Posted by at 上午 6:47
8月 272008
 

说一下如何使用 Tab 键自动完成,按照习惯,Tab 键通常是用来缩进行的,如输入 4 个空格如果要把 Tab 映射为自动完成,可能使用上稍微有点麻烦,下面会讲如何解决这个问题。

首先需要为 Vim 提供一个 PHP 的函数列表文件,这样 Vim 才能知道如何自动完成一个 PHP 函数。函数列表不需要自己做,PHP 网站上有现成的,拿来用就可以了:

http://cvs.php.net/viewvc.cgi/phpdoc/funclist.txt

得到这个文件,直接拷贝到 Vim 目录中,比如根目录下面,然后在 vimrc 中加入如下代码,告知 vim 在自动完成时,需要扫描这个文件来分析关键字。

"You can obtain the completion dictionary file from:

" http://cvs.php.net/viewvc.cgi/phpdoc/funclist.txt

set dictionary-=$VIM/funclist.txt dictionary+=$VIM/funclist.txt

"Use the dictionary completion

set complete-=k complete+=k

那么如何解决缩进的问题呢?我们肯定不愿意在映射另外一个键来做缩进用,感觉会很别扭。下面的函数会解决这个问题。

"Auto completion using the TAB key

"This function determines, wether we are on

"the start of the line text(then tab indents)

"or if we want to try auto completion

function! InsertTabWrapper()

let col=col(‘.’)-1

if !col || getline(‘.’)[col1] !~ ‘\k’

return "\<TAB>"

else

return "\<C-N>"

endif

endfunction

"Remap the tab key to select action with InsertTabWrapper

inoremap <TAB> <C-R>=InsertTabWrapper()<CR>

 

这个函数的作用是判断当前光标前的一个字符是否为一个 Keyword 字符(:help iskeyword),vim 中用 \k 表示,这个字符的范围大约是所有非空白的可打印的字符,但除开某些特殊字符,比如 @ * " 等等。

 

于是效果就出来了,当前导字符为空白或者 @ * 等字符时,Tab 就映射为 成为缩进键。否则就映射为 ,也就是 CTRL+N 调用自动完成功能。

 Posted by at 上午 3:50
8月 272008
 

首先安装taglist
1.到
http://www.vim.org/scripts/script.php?script_id=273下载taglist
2.把解压出来的plugin里面的文件,放到D:\Program Files\Vim\vimfiles\plugin里面
3.在 Vim 中运行 :helptags D:\Program Files\Vim\vimfiles\doc 安装文档
4.输入Tlist 即可启动函数列表功能
注意:如果启动VIM时跳出「Taglist: Exuberant ctags (
http://ctags.sf.net) not found in PATH. Plugin is not loaded.」,则需要下载ec57w32.zip (Exuberant Ctags),將其中的ctags.exe放到VIM安装时的根目录D:\Program Files\Vim\Vim70。

下面是在Tlist窗口下的按键功能介绍
Taglist window key list~
The following table lists the description of the keys that can be used
in the taglist window.

Key Description~

<CR> Jump to the location where the tag under cursor is
defined.
o Jump to the location where the tag under cursor is
defined in a new window.
P Jump to the tag in the previous (Ctrl-W_p) window.
p Display the tag definition in the file window and
keep the cursor in the taglist window itself.
t Jump to the tag in a new tab. If the file is already
opened in a tab, move to that tab.
Ctrl-t Jump to the tag in a new tab.
<Space> Display the prototype of the tag under the cursor.
For file names, display the full path to the file,
file type and the number of tags. For tag types, display the
tag type and the number of tags.
u Update the tags listed in the taglist window
s Change the sort order of the tags (by name or by order)
d Remove the tags for the file under the cursor
x Zoom-in or Zoom-out the taglist window
+ Open a fold
– Close a fold
* Open all folds
= Close all folds
[[ Jump to the beginning of the previous file
<Backspace> Jump to the beginning of the previous file
]] Jump to the beginning of the next file
<Tab> Jump to the beginning of the next file
q Close the taglist window
<F1> Display help

 Posted by at 上午 3:39
6月 162008
 

vim 下载地址:
Linux版:wget ftp://ftp.vim.org/pub/vim/unix/vim-7.1.tar.bz2

帮助文档地址:
http://vimcdoc.sourceforge.net/
http://vimcdoc.sourceforge.net/doc/help.html

VIM帮助手册pdf
VIM在线手册

手把手教你把Vim改装成一个IDE编程环境(图文)
http://blog.csdn.net/wooin/archive/2007/12/30/2004470.aspx

Vim颜色设置
http://zywangyan54.blog.163.com/blog/static/31810358200752993227703/

Vim程序调试
http://www.wangchao.net.cn/bbsdetail_69434.html

Vim使用经验
http://blog.csdn.net/camry_camry/archive/2004/09/23/114188.aspx

Vim自动给脚本加注释
http://blog.chinaunix.net/u/6542/showart.php?id=357716

VIM 插件大全 及 不错介绍
http://hi.baidu.com/00%C6%F3%B6%EC/blog/item/fd456c03a2d40f8bd53f7c29.html

Vim即学即用
http://blog.linuxpk.com/3973/viewspace-2644

小技巧:

全文档代码格式化,命令模式下: gg=G 即可搞定;
当前行代码格式化,命令模式下: == 即可搞定;
代码块格式化,命令模式下: =a{ ; 格式化{}里面的代码
更多的还要看上面的手册呢

删除所有空行: :g /^$/d #注意不要 :% s/^$//

 

 Posted by at 上午 1:42