查看源代码
出自Linux Wiki
对
用Vim编程——配置与技巧
的源代码
根据以下的原因,您无权限进行编辑这个页面操作:
您刚才请求的操作只有这个用户组中的用户才能使用:
用户
您可以查看并复制此页面的源代码:
{{Hint|本文难免有不足之处,热切期盼您改进本页,要改进本页,请先登录,并选择编辑。}} {{Note|由于转载不当很可能造成格式的破坏,降低文档价值,本文不推荐转载。但是您确实有转载本文档的权利,如果您一定要转载,请一定保留本文的所有信息。}} 本文介绍如何用强大的文本编辑器[[:Category:Vi/Vim编辑器 | Vim]]编程。 {{Hint|针对各具体语言,还有些针对性的设置方法,如[[配置基于Vim的Python编程环境]]。}} ==配置== 基本配置<ref>[http://stackoverflow.com/questions/65076/how-to-setup-vim-properly-for-editing-python-files-py How to setup VIM properly for editing Python files - *.py (StackOverflow)]</ref> <ref>[http://amix.dk/vim/vimrc.html amix的vimrc]</ref>: <source lang="vim"> "开启语法高亮 syntax on "依文件类型设置自动缩进 filetype indent plugin on "显示当前的行号列号: set ruler "在状态栏显示正在输入的命令 set showcmd "关闭/打开配对括号高亮 "NoMatchParen DoMatchParen </source> 行号的显示与隐藏<ref>[http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/ Python and vim: Make your own IDE (2009.2)]</ref>: <source lang="vim"> "显示行号: set number "为方便复制,用<F2>开启/关闭行号显示: nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR> </source> 启用Modeline(即允许被编辑的文件以注释的形式设置Vim选项,详见[http://vim.wikia.com/wiki/Modeline_magic Vim Wiki: Modeline magic])<ref>[http://wiki.python.org/moin/Vim Python官网上的Vim介绍]</ref>: <source lang="vim"> set modeline </source> 如果终端使用的是深色背景: <source lang="vim"> "为深色背景调整配色 set background=dark </source> ==代码补全== Vim 7已经内置了代码补全功能<ref>[http://linuxtoy.org/archives/vim_7_new_features.html 细说 Vim 7 之新特性-LinuxToy]</ref>,补全操作可分为两种: ;关键字补全:即简单地补全到文档中已有的词,快捷键为 <tt>Ctrl-N</tt> 或 <tt>Ctrl-P</tt>。 ;智能补全:Vim 7中引入 Omni Complete,可根据语义补全,快捷键为 <tt>Ctrl-X Ctrl-O</tt>。 Vim的补全菜单操作方式与一般IDE的方式不同,可加入如下设置<ref>[http://vim.wikia.com/wiki/VimTip1228 VimTip 1228: Improve completion popup menu]</ref>: <source lang="vim"> "让Vim的补全菜单行为与一般IDE一致(参考VimTip1228) set completeopt+=longest inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" inoremap <expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>" inoremap <expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>" inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>" inoremap <expr> <PageUp> pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>" </source> 如需'''自动补全''',可安装[http://www.vim.org/scripts/script.php?script_id=1879 AutoComplPop]插件,安装后如需在补全是显示文档(预览),可加入设置: <source lang="vim"> let g:acp_completeoptPreview = 1 </source> ==注释管理== 手动地逐行加注释一般较繁,可用插件来快速注释(或取消注释)代码。 常见的注释管理插件为[http://www.vim.org/scripts/script.php?script_id=1218 The NERD Commenter],默认支持多种语言,使用方法也很简单:只需在可视(V)模式中选择一段代码,按下<tt>\cc</tt>加逐行注释,<tt>\cu</tt>取消注释,<tt>\cm</tt>添加块注释。 如有其它需求,也可考虑[http://www.vim.org/scripts/script.php?script_id=1528 comments]、[http://www.vim.org/scripts/script.php?script_id=23 EnhCommentify]等插件。 ==缩进== 使用自动缩进可能需要设置,vim中对自动缩进的详细设置办法见[[Vim代码缩进设置]]。 在不同的模式中调整缩进的方法不同: ;插入模式: <tt>Ctrl-T</tt>增加缩进,<tt>Ctrl-D</tt>减小缩进。 ;命令模式: <tt> >> </tt>右缩进, <tt> << </tt>左缩进,注意<tt>n<< </tt>或 <tt>n>></tt>是缩进多行,如<tt>4>></tt> ;可视模式: <tt> < </tt>、<tt> > </tt>用于左右缩进,<tt> n< </tt>、<tt> n> </tt>可做多节缩进,如 <tt> 2> </tt>。 另外,<tt>=</tt> 可对选中的部分进行自动缩进;<tt>]p</tt>可以实现p的粘贴功能,并自动缩进。 ==代码浏览与跳转== 代码跳转类似于IDE中的''Ctrl+点击''功能。与代码补全类似,代码浏览工具亦可分为基于文本分析的和基于代码理解的两类。 ===简单代码跳转=== 在命令模式下可直接使用下述指令:<ref>Vim文档</ref> ;跳转到定义:<tt>gd</tt>到局部变量的定义,<tt>gD</tt>到全局变量的定义 ;搜索:<tt>*</tt>, <tt>#</tt> 可对光标处的词向前/向后做全词搜索,<tt>g*</tt>, <tt>g#</tt> 做相对应的非全词匹配搜索 ;代码块首尾:<tt>[[</tt>, <tt>]]</tt> 可跳到当前代码块起始或结束的大括号处。 ;括号首尾:<tt>%</tt> 可在配对的括号、块首尾之前跳转。 ;位置历史:<tt>Ctrl-O</tt> 在历史记录中后台,<tt>Ctrl-I</tt> 则为前进。 ===基于代码理解的跳转=== 该功能依赖于ctags工具。<ref>[http://vim.wikia.com/wiki/Browsing_programs_with_tags Browsing programs with tags -- Vim Tips Wiki]</ref>安装好ctags后,在存放代码的文件夹运行 ctags -R . 即可生成一个描述代码结构的tags文件。 {{Hint|ctags的功能很强大,更详细的配置请参考其文档。}} 建议在''~/.vimrc''中添加如下配置以使Vim在父目录中寻找tags文件<ref>[http://stackoverflow.com/questions/563616/vim-and-ctags-tips-and-tricks Vim and Ctags tips and tricks]</ref>: <source lang="vim"> set tags=tags;/ </source> 设置好后,可在Vim中使用如下功能:<tt>Ctrl-]</tt>转至最佳匹配的相应Tag,<tt>Ctrl-T</tt>返回上一个匹配。如果有多个匹配,<tt>g Ctrl-]</tt>可显示所有备选的tags。如有需要,可互换<tt>Ctrl-]</tt>和<tt>g Ctrl-]</tt> <ref>[http://stackoverflow.com/questions/7640663/use-tjump-instead-of-tag-vim-on-pressing-ctrl use :tjump instead of :tag vim on pressing ctrl-] (StackOverflow)]</ref>: <source lang="vim"> "在普通和可视模式中,将<c-]>与g<c-]>互换 nnoremap <c-]> g<c-]> vnoremap <c-]> g<c-]> nnoremap g<c-]> <c-]> vnoremap g<c-]> <c-]> </source> ==代码折叠== 代码较长时可启用代码折叠功能,如按语法高亮元素折叠: <source lang="vim"> set foldmethod=syntax "默认情况下不折叠 set foldlevel=99 </source> 随后即可使用z系列命令管理代码折叠。如<tt>za</tt>会翻转当前位置的折叠状态,<tt>zA</tt>会递归翻转当前层所有代码的折叠状态等。当然也可以把功能键映射到za: <source lang="vim"> map <F3> za </source> ==代码框架== 要查阅代码的大体结构,如类、函数、变量等,可使用[http://www.vim.org/scripts/script.php?script_id=273 taglist插件],其可配置性很强,可将下面的代码按需加入''~/.vimrc''中(用<tt><F4></tt>开启/关闭Tag list): <source lang="vim"> " F4: Switch on/off TagList nnoremap <silent> <F4> :TlistToggle<CR> "let Tlist_Show_One_File = 1 " Displaying tags for only one file~ let Tlist_Exist_OnlyWindow = 1 " if you are the last, kill yourself "let Tlist_Use_Right_Window = 1 " split to the right side of the screen let Tlist_Sort_Type = "order" " sort by order or name let Tlist_Display_Prototype = 0 " do not show prototypes and not tags in the taglist window. let Tlist_Compart_Format = 1 " Remove extra information and blank lines from the taglist window. let Tlist_GainFocus_On_ToggleOpen = 1 " Jump to taglist window on open. let Tlist_Display_Tag_Scope = 1 " Show tag scope next to the tag name. "let Tlist_Close_On_Select = 1 " Close the taglist window when a file or tag is selected. let Tlist_Enable_Fold_Column = 0 " Don't Show the fold indicator column in the taglist window. let Tlist_WinWidth = 40 " let Tlist_Ctags_Cmd = 'ctags --c++-kinds=+p --fields=+iaS --extra=+q --languages=c++' " very slow, so I disable this " let Tlist_Process_File_Always = 1 " To use the :TlistShowTag and the :TlistShowPrototype commands without the taglist window and the taglist menu, you should set this variable to 1. ":TlistShowPrototype [filename] [linenumber] </source> ==查看文档== <tt>K</tt> 键可用于查看当前函数的文档。 ==其它插件== 其它常用插件: ;[http://www.vim.org/scripts/script.php?script_id=69 Project]:方便管理工程中的文件 ;[http://www.vim.org/scripts/script.php?script_id=2540 snipmate.vim]:在编辑中实现模板展开 ;[http://www.vim.org/scripts/script.php?script_id=31 a.vim]:在''.cpp/.h''等文件对中跳转 ;[http://www.vim.org/scripts/script.php?script_id=1697 surround.vim]:快速删除/修改光标周围配对的括号 ;[http://www.vim.org/scripts/script.php?script_id=1658 The NERD tree]:以树形结构浏览文件夹中的文件 ;[http://www.vim.org/scripts/script.php?script_id=39 matchit.zip]:增强<tt>%</tt>的功能 ==特定编程语言的配置 == 参见[[配置基于Vim的Python编程环境]] ==参考资料== <references /> [[Category: Vi/Vim编辑器]]
在这个页面上使用的模板有:
Template:Hint
(
查看源码
)
Template:谨慎转载
(
查看源码
)
返回到
用Vim编程——配置与技巧
。
导航
首页
社区入口
当前事件
最近更改
随机页面
帮助
查看
页面
讨论
查看源代码
历史
个人工具
登录/创建账户
搜索
简体繁体转换
不转换
简体
繁體
工具箱
链入页面
链出更改
特殊页面