open module under cursor in vim

Inspired by I wrote (well ... copied for the larger part) a script to open the Module currently under the cursor in vim.

Typing \fm will lookup the first Module found in available Perl library paths (plus current working directoy . '/lib')

I did search for some time and read a bit about ctags and pltags but ended up confused. add this to your vimrc

" find module in perl INC and edit "
function! GetCursorModuleName()
    let cw = substitute( expand(""), '.\{-}\(\(\w\+\)\(::\w\+\)*\).*$', '\1', '' )
    return cw

function! TranslateModuleName(n)
    return substitute( a:n, '::', '/', 'g' ) . '.pm'

function! GetPerlLibPaths()
    let out = system('perl -e ''print join "\n", @INC''')
    let paths = split( out, "\n" )
    return paths

function! FindModuleFileInPaths()
    let paths = [ 'lib' ] + GetPerlLibPaths()
    let fname = TranslateModuleName( GetCursorModuleName() )

    for p in paths
        let f = p . '/' . fname
        if filereadable(f)
            exec "edit " . f
            return 1

    echo "File not found: " . fname

nmap fm :call FindModuleFileInPaths()


  • returns only the first found module
  • "local" modules not found unless current working directory has the lib/ dir as child


If you're going to shell out anyway, why not use "perldoc -l" to do the finding for you?

"perldoc -l Test::More" returns "/opt/local/lib/perl5/5.12.4/Test/" on my machine, for example.

I believe there's something like this in vim-perl already, but if not, I should add it.

It seems to me using a function call at all is much too complicated:

au FileType perl command! -nargs=1 PerlModuleSource \
    :tabnew `perldoc -lm <args>`
au FileType perl setlocal iskeyword+=:                                                                                       
au FileType perl noremap <leader>P :PerlModuleSource <cword><cr>zR<cr>  

I use gf and it works perfectly. I only have this in my .vimrc:

set path+=$PWD/lib

I used to have this:

set includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.pm','')

but it works without it too.

Consider using perl-support plugin by Prof. Fritz Mehner.

Under the hood, it adds many features including setting 'path' option to contain all the perl lib directories and also changing the iskeyword (which determines how a word looks like). By doing:

set iskeyword+=:

you make a module name to be a single word which means running one of the:


with the cursor anywhere on Data::Dumper will jump straight into the file)
I also use some other nice plugins like:
vim-commentary -

taglist -

Check them out. You may find them very useful.


This is your blog; you can fix up your own comments at will.

Leave a comment

About davewood

user-pic I like Toast.