I want to teach Vim how to open Perl 5 modules from names like File::Find
. I already have a wrapper script written in Perl 5 that handles the commandline (see below), but I would like to be able to say things like :tabe File::Find
and have it really execute :tabe /home/cowens/apps/perlbrew/perls/perl-5.14.0/lib/5.14.0/File/Find.pm
.
My current plan is to somehow use autocmd BufNewFile
and/or autocmd BufPreRead
, but I can't figure out how to switch the file name.
#!/usr/bin/perl
use strict;
use warnings;
my @files = @ARGV;
for my $file (@files) {
next if -f $file; #skip files that really exist
#convert from module name to module file
(my $module = $file) =~ s{::}{/}g;
$module .= ".pm";
#look for module in the include paths
for my $dir (@INC) {
my $candidate = "$dir/$module";
if (-f $candidate) {
$file = $candidate;
last;
}
}
}
#replace this script with vim
exec "vim", "-p", @files;
Doing
:verbose au BufReadCmd
Will tell you how other types of plugins do this (e.g. zip, netrw, fugitive). Sample output that should give you plenty of ideas:
zip BufReadCmd
zipfile:* call zip#Read(expand("<amatch>"), 1)
Last set from C:\Program Files\Vim\vim73\plugin\zipPlugin.vim
*.zip call zip#Browse(expand("<amatch>"))
Last set from C:\Program Files\Vim\vim73\plugin\zipPlugin.vim
Network BufReadCmd
ftp://* exe "silent doau BufReadPre ".fnameescape(expand("<amatch>"))|call netrw#Nread(2,expand("<amatch>"))|exe "silent doau BufReadPost ".fnameescape(expand("<amatch>"))
Last set from C:\Program Files\Vim\vim73\plugin\netrwPlugin.vim
http://* exe "silent doau BufReadPre ".fnameescape(expand("<amatch>"))|call netrw#Nread(2,expand("<amatch>"))|exe "silent doau BufReadPost ".fnameescape(expand("<amatch>"))
Last set from C:\Program Files\Vim\vim73\plugin\netrwPlugin.vim
fugitive_files BufReadCmd
*.git/index
exe s:BufReadIndex()
Last set from C:\Program Files\Vim\vimfiles\plugin\fugitive.vim
*.git/*index*.lock
exe s:BufReadIndex()
Last set from C:\Program Files\Vim\vimfiles\plugin\fugitive.vim
fugitive://**//[0-3]/**
exe s:BufReadIndexFile()
Last set from C:\Program Files\Vim\vimfiles\plugin\fugitive.vim
fugitive://**//[0-9a-f][0-9a-f]*
exe s:BufReadObject()
Last set from C:\Program Files\Vim\vimfiles\plugin\fugitive.vim
Consider using ctags
. If you're able to run the ctags process over your source code, you should be able to get to a point where you simply do:
vim -t File::Find
Vim has information about this (:help vim
) I think ctags probably goes well beyond what you're trying to do, allowing you to jump from the middle of one source file to the original function definition in another.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With