简体   繁体   English

如何从 vim 命令行以交互方式运行 vim 脚本?

[英]How to run a vim script interactively from vim command line?

Is there a way to run these scripts from the : commandline with a few keystrokes?有没有办法从:命令行中通过几次按键运行这些脚本?

Over the last couple of months, I've built a series of files full of vim-commands to auto-generate boilerplate code for my projects.在过去的几个月里,我构建了一系列充满 vim 命令的文件来为我的项目自动生成样板代码。 It's allowed me to work faster.它让我可以更快地工作。

However, the only way I know how to run these scripts is by assigning them to key-combinations in ~/.vimrc .但是,我知道如何运行这些脚本的唯一方法是将它们分配给~/.vimrc键组合。 There's only so many keys I can remap.我可以重新映射的键只有这么多。

Is there a way to run these scripts from the : commandline with a few keystrokes?有没有办法从:命令行中通过几次按键运行这些脚本?

For example, I have a unit_test_cpp.vim script, which builds a boilerplate unit test cpp file.例如,我有一个unit_test_cpp.vim脚本,它构建了一个样板单元测试 cpp 文件。 I'd like to be able to type我希望能够打字

:utc

or some other simple combination of letters with a simple mnemonic to run this script on my currently open file.或其他一些带有简单助记符的简单字母组合,以在我当前打开的文件上运行此脚本。

Open Vim and enter the following:打开 Vim 并输入以下内容:

:source /path/to/vim/file/name.vim

If you are currently editing that file, you can also type:如果您当前正在编辑该文件,您还可以键入:

:w <ENTER>
:source % <ENTER>

The percent sign (%) means the path to the currently opened file.百分号 (%) 表示当前打开的文件的路径。

You could use the command feature of vim.您可以使用 vim 的command功能。 In your unit_test_cpp.vim file you would add something like this:在您的unit_test_cpp.vim文件中,您将添加如下内容:

command Utc call CreateUnitTests()

Now when you type :Utc it will call your function.现在,当您键入:Utc它会调用您的函数。 The only limitation is that your command must start with an uppercase letter.唯一的限制是您的命令必须以大写字母开头。

Script or function?脚本还是函数? If it is a function, try如果是函数,试试

:call FunctionName() 
:run file.vim

:run is like :source , but :run doesn't need the path. :run就像:source ,但:run不需要路径。 It searches the runtimepath .它搜索运行时runtimepath

A Linux runtimepath : Linux 运行时runtimepath

$HOME/.vim,
$VIM/vimfiles,
$VIMRUNTIME,
$VIM/vimfiles/after,
$HOME/.vim/after

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM