我想要找到一个vim插件或者写一个vimscript函数来打开 与我当前文件位于同一目录中(或下)的文件。该文件应该 匹配列表中定义的正则表达式文件列表之一。
我有一个看起来像这样的项目:
src
|- controllers
| ...
|- util
| ...
|- widgets
| - widgetA
| | - widgetA.js
| | - widgetA.template.html
| -widgetB
| | - widgetB.js
| | - widgetB.template.html
| -widgetC
| | - widgetC.js
| | - widgetC.template.html
| | - someHelpers.js
示例用例(在命令行中):
cd src
vim widgets/widgetA/widgetA.js
内部vim:
press F4 while in command mode
结果:
widgetA.template.html is opened in vertical split mode with widgetA.js
模板文件将属于以下之一:
s/js$/tempate.html/
s/js$/html/
我目前正在使用nerdtree和ctrl-p来加速文件打开,但这是一个工作流程 我经常使用,我认为可以有意义加快速度。有什么建议吗?
答案 0 :(得分:3)
感觉您正在寻找projectionist或fswitch的内容。
使用放映员,理论上可以执行:A
/ :SA
切换到备用文件。
所以在你的.projections.json
(未经测试)文件中看起来像这样:
{
"widgets/*.js": {
"alternate": "widgets/{}.template.html",
"type": "widget"
},
"widgets/*.template.js": {
"alternate": "widgets/{}.js",
"type": "template"
}
您还可以使用:Ewidget
和:Etemplate
命令查找小部件/模板。这些命令也将采用模糊文件名。例如:Ewidget wta
。您还可以分别通过:Swidget
,:Vwidget
和:Twidget
打开拆分,垂直拆分和标签中的文件。有关详细信息,请参阅:h projectionist
。
另一种选择是使用类似fswitch的东西,它是一个C / C ++ .h / .c切换器。有关详细信息,请参阅:h fswitch-setup
。
还有一些其他插件可以执行类似的操作:altr和a.vim等等。
如果插件不是你的东西,那么你可以使用%
技巧。例如:sp %<.template.html
或许是一个快速而肮脏的映射:
nnoremap <f4> %:p:s,\.js$,.X123X,:s,\.template\.html$,\.js,:s,\.X123X$,\.template\.html,<CR>
有关更多信息,请参阅以下vim wiki页面:Easily switch between source and header file
我个人使用放映员并发现它符合我的需求,特别是对于导航结构化项目,我发现它比仅仅像fswitch这样的简单切换器更有用。当你的需求变得更加狂野时,投影仪也会比香草更容易。
答案 1 :(得分:0)
你想要的东西在shell中很容易做到:
$ cd src
$ vim -O w*/*A/*
但是我不确定你是怎么看到在Vim本身工作的。你想要在新标签中发生这种情况吗?您是否希望新的文件对替换当前的文件对?
答案 2 :(得分:0)
将它放入你的vimrc并重新启动vim会有效。
目前仅适用于以.template.html
结尾的文件,这些文件位于同一目录中,但很容易看到它如何适用于多个其他情况。当它找不到模板时,它会打开nerdTree到当前目录。
map <F4> :call OpenTemplate()<cr>
function! OpenTemplate()
" Get the Current directory
let l:jake = expand('%')
" Replace .js with .template.html
let l:jake = substitute(l:jake, ".js$", ".template.html", "")
" Verify that the file exists and it it does open in using vs (see :help vs)
" if the file can't be found, open nerdTree
if filereadable(l:jake)
execute 'vs ' . l:jake
else
echo 'Cant find template'
:NERDTreeFind
endif
endfunction