Run vim script from vim commandline

最后都变了- 提交于 2019-12-05 20:25:55

问题


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. 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. 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. 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.


回答1:


You could use the command feature of vim. In your unit_test_cpp.vim file you would add something like this:

command Utc call CreateUnitTests()

Now when you type :Utc it will call your function. The only limitation is that your command must start with an uppercase letter.




回答2:


Open Vim and enter the following:

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

If you are currently editing that file, you can also type:

:w <ENTER> :source % <ENTER>

This works because the percent sign (%) means the path to the currently opened file.




回答3:


Script or function? If it is a function, try

:call FunctionName() 



回答4:


:run file.vim

it is like ":source" but don't need the path, search on runtimepath.

For linux runtimepatch is

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


来源:https://stackoverflow.com/questions/3374179/run-vim-script-from-vim-commandline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!