Search code examples
vimnavigationproject-navigator

vim "set property" to a file and then use it with Project navigation


I use vim for programming. my current job based on Yii MVC framework.

When you work with MVC framework, you are always navigating among models, controllers and views. I want to make save time navigation for my tasks.

The first is: i want jump to a model related controller. One model could have one controller for jumping to. Which i want to setup once in a project.

What is the best solution for this ?

My opinion is create a comment in a model header like phpDoc

/*
* @controller ControllerName
*/

And then write a vim function, which will find "ControllerName" in current file and open this file. Then bind it to a key combination.

What is your idea to implement it?

Thanks.


Solution

  • This functionality is known as tag navigation. Chances are that your fileformat is already supported by the omnipresent exuberant ctags (guessing it is php, for sure).

    Then you'd basically do:

    :!ctags -R .
    :tj ControllerName
    

    You can use tab-completion (Control + Tab), do searches (:tj /troll + Tab)

    Ctags has numerous options to enrich/limit the kinds of objects tagged. To selectively act on certain files only:

    :tags +=controllertags
    :!ctags -o controllertags **/*Controller.php
    

    which will tag only controller sources in a separate tags file so you can keep working with the rest of your tags setup as before (in case you were already using it for other stuff)