published on in vim Perl Programming Open::This CPAN

Opening Files Quickly from Inside vim


If you’d like to try this out, make sure you have Open::This installed. You can install it via cpanm:

cpanm Open::This

The Problem

A couple of years ago I wrote an article on how to use Open::This to get to files (and locations within files) more easily. I use this tool from the command line constantly, but I’ve always had to exit vim in order to use ot.

The Solution

I got most of the way there by reading docs, but asking a question on got me to the finish line.

nnoremap <leader>ot :call OT(input("ot: ", "", "file"))<cr>

function! OT(fname)
    let res = system("ot --editor vim --print " . shellescape(a:fname))
    if v:shell_error
        echo "\n" . res
        execute "e " res
    call histadd(':', printf('call OT("%s")', escape(a:fname, '"\')))

After adding the above to my .vimrc I can open files even faster than before. Now from inside vim I can enter ,ot and can then happily pass arguments to ot from vim. This solution even populates vim’s history so that I can easily re-run an earlier command. You can see it in action below: