gVim 和多种编程语言
我的日常工作涉及使用 Perl 进行编码。在家里我会玩 Python 和 Erlang。对于 Perl,我想用两个空格缩进我的代码。而 Python 的标准是 4。此外,我还有一些用于打开函数声明的键绑定,我希望将其与所有编程语言一起使用。在 gVim 中如何实现这一点?
例如,有没有办法为每种编程语言或类似的语言维护配置文件?
My day job involves coding with Perl. At home I play around with Python and Erlang. For Perl I want to indent my code with two spaces. Whereas for Python the standard is 4. Also I have some key bindings to open function declarations which I would like to use with all programming languages. How can this be achieved in gVim?
As in, is there a way to maintain a configuration file for each programming language or something of that sort?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 $HOME 中,创建 .vim/ 目录(或 Windows 上的 vimfiles/ ),在其中创建 ftplugin/ 目录,并在其中保留名为“perl.vim”或“python.vim”或“html.vim”或 . ..
当您打开/创建给定文件类型的新文件时,只要您不要忘记在 .vimrc(或 Windows 下的 _vimrc)中添加
:filetype plugin on
然后, 这些应该会自动加载vim 选项应该使用
:setlocal
定义(而不是:set
,否则它们的定义将覆盖默认的全局设置)。映射是用
:n/i/v(nore)map
以及缩写定义的。命令是使用-b
选项定义的。如果没有插件的帮助,菜单就无法本地化。local
、
和-b
对于防止副作用很重要。In your $HOME, make .vim/ directory (or vimfiles/ on Windows), in it make ftplugin/ directory, and in it keep files named "perl.vim" or "python.vim" or "html.vim" or ...
These should be loaded automatically when you open/create new file of given filetype as long as you don't forget to add
:filetype plugin on
in your .vimrc (or _vimrc under windows)Then, vim options should be defined with
:setlocal
(and not:set
, otherwise their definition will override the default global setting).Mappings are defined with
:n/i/v(nore)map <buffer>
, as well as the abbreviations. Commands are defined with the-b
option. Menus can't be made local without the help of a plugin.local
,<buffer>
, and-b
are important to prevent side effects.您应该能够通过利用文件类型来完成...例如,将其添加到您的 vimrc (并针对不同语言进行适当修改):
You should be able to do with by leveraging filetypes ... e.g., add this to your vimrc (and modify appropriately for different languages):
除了 rangerchris 的答案之外,您还可以考虑使用模型行。模型行告诉编辑器如何配置自身:
该模型行告诉 vi 使用 4 个字符制表符和自动缩进,使用空格而不是制表符,并且当光标到达 76 个字符时应插入换行符。
您可以使用两个变量(最有可能在 .vimrc 中设置)来控制 Vim 读取模型行的方式:
modeline
变量告诉 Vim 查找模型行(如果已设置)。 modelines 变量告诉 Vim 从顶部和底部扫描多少行来查找 modeline(在这种情况下,如果 modeline 位于文件的前五行或最后五行,它将找到 modeline)。与任何从不受信任来源获取指令的系统一样,模型行可能是安全威胁,因此
root
用户永远不应该使用模型行,并且你应该保持你的 Vim 副本是最新的。模型行的真正好处是它们是针对每个文件的。大多数 Perl 人都是用四个空格缩进的,但我是八个字符制表符的人。当使用其他人的代码时,我使用反映他们用法的模型行。其余时间我用我自己的。
In addition to rangerchris's answer, you might consider using modelines. Modelines tell the editor how to configure itself:
That modeline tells vi to use 4 character tabs and autoindents, to use spaces instead of tabs, and that it should insert a newline when the cursor gets to 76 characters.
You can control how Vim reads modelines with two variables (most likely set in your .vimrc):
The
modeline
variable tells Vim to look for modelines if it is set. Themodelines
variable tells Vim how many lines from the top and bottom to scan looking for the modeline (in this case it will find the modeline if it is in the first or last five lines of the file).Like any system that takes instructions from untrusted sources, modelines can be a security threat, so the
root
user should never use modelines and you should keep your copy of Vim up-to-date.The real benefit to modelines is that they are per file. Most Perl people are four spaces as indent people, but I am an eight character tab person. When working with other people's code, I use a modeline that reflects their usage. The rest of the time I use my own.
我是这样做的。下面是我的
.vimrc
的摘录,我维护每种语言的进一步配置,并在加载新缓冲区时加载这些配置。请注意,虽然我获取了一个文件,但我可以执行任何 VIM 命令或调用函数。例如,为了加载新的 Java 文件,我这样做:
其中
GeneratePackage()
是一个 VIM 函数。Here's how I do it. The below is an excerpt from my
.vimrc
, and I maintain further configs per language, and load those when a new buffer is loaded.Note that although I source a file, I can execute any VIM command, or call a function. e.g. for loading a new Java file I do this:
where
GeneratePackage()
is a VIM function.