Skip to main content

Opening Files Quickly from Inside vim

·191 words·1 min·
vim perl Programming Open::This CPAN
Table of Contents
❤️ It's great to see you here! I'm currently available on evenings and weekends for consulting and freelance work. Let's chat about how I can help you achieve your goals.


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:




CPAN Bus Factor
·1016 words·5 mins
perl Programming CPAN metacpan
Observing Network Traffic with LWP::ConsoleLogger::Everywhere
·469 words·3 mins
CPAN perl LWP::ConsoleLogger LWP::UserAgent UserAgent
UserAgent Debugging Made Easy
·2689 words·13 mins
CPAN perl LWP::ConsoleLogger LWP::UserAgent UserAgent