Wednesday, July 2, 2014

Re: Configure goto file command


On Wed, Jul 2, 2014 at 10:21 AM, Fabien Meghazi <agr@amigrave.com> wrote:
when doing web development, in views, I deal with a lot with references to absolute urls. Eg:

    <script src="/foo/bar.js" />

My goal is to put my cursor over the /foo/bar.js and type ` gf `

But /foo/bar.js is an absolute path, and my paths may vary from project to project.

When opening a file in vim, I always autocmd :lcd to the project's root

So given that my static files could be in  <project>/static/foo/bar.js for a project but it could also be <project>/module_foo/static/foo/bar.js (the place can change from project to project)

So how can I configure vim so  the  ` gf ` command will find the good file ?
Is it possible to hook a python function in place of isfname in order to return the good filename or something like this ?


This is what I put in my .vimrc:


augroup MyProjectName
    au!
    " Set a default web root directory for use later
    " When using Vims 'gf' command, includeexpr can manipulate the file to be
    " opened (via gf) and change it in an appropriate way.  In this case, we
    " need to replace '/myroot' with the filesystem location (projectWebDir).
    autocmd BufRead */dev/rem/web/*
                \ setlocal sw=4 expandtab tabstop=4|
                \ let projectWebDir = substitute(fnamemodify(expand("<afile>"), ':p:h'), 'rem\(\\\|\/\)web\zs.*', '', '')|
                \ setlocal includeexpr=substitute(v:fname,'^/myroot/',substitute(g:projectWebDir,'\\','/','g').'/','/')
augroup end

The BufRead allows me to specific a specific directory for this augroup to function over as like you I will have many projects.


You may have to mess around with the paths and how you specify the includeexpr, you can add something like this as well:
                \ setlocal includeexpr=substitute(v:fname,'^/myroot/',substitute(g:projectWebDir,'\\','/','g').'/','/')|
                \ echo 'includeexpr:'.substitute(v:fname,'^/myroot/',substitute(g:projectWebDir,'\\','/','g').'/','/')

To see what it was set to, then modify and try again.

HTH,
David

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: