VIM set ctags in .vimrc

56,588

Solution 1

That is because the current directory of vim is the home directory. You either need to change the path or create nerdtree plugin. See nerdtree_plugin directory for an example. I think that you'll find this function interesting:

g:NERDTreeFileNode.GetSelected()

Solution 2

  1. Functions are supposed to start with a capital letter. Change createTags to CreateTags.

  2. To use project-specific tags files you will need to add something like that to your .vimrc:

    set tags=./tags,tags;
    

    so that Vim looks for a tags file in the current directory first and up and up until it finds it. Additionnaly, I like to set autochdir: the working directory is always the one containing the current file.

Share:
56,588

Related videos on Youtube

Dmitry Teplyakov
Author by

Dmitry Teplyakov

Updated on November 28, 2020

Comments

  • Dmitry Teplyakov
    Dmitry Teplyakov over 3 years

    I have my projects in /srv/http/dev folder. I generated ctags file for one project:

    $ ctags -R --languages=php .
    

    Now I set ctags path:

    :set tags=/srv/http/dev/proj/tags
    

    And it's working fine.

    But I have many projects in dev/ folder. How can I set ctags in .vimrc file? set tags=tags not working properly.

    And I would like to generate tags file by pressing hotkey on selected NERDTree folder. I tried:

    nmap <silent> <F4>
        \ :!ctags -R
        \ --languages=php .<CR>
    

    But it creates tags file in home folder and shows warnings about scanning home folder..

    EDIT: As @Alexandru Plugaru advised I should use g:NERDTreeFileNode.GetSelected() function.

    I am just newbie vim user, so I have one more question. I have added in my .vimrc file:

    function createTags()
        let curNodePath = g:NERDTreeFileNode.GetSelected().path.str
        exec ':!ctags -R --languages=php ' . curNodePath
    endfunction
    nmap <silent> <F4> :execute createTags()<CR>
    

    But by pressing on F4 I see:

    E117: Unknown function: createTags
    E15: Invalid expression: createTags
    

    EDIT 2: Thanks to @Alexandru Plugaru I got this:

    function CreateTags()
        let curNodePath = g:NERDTreeFileNode.GetSelected().path.str()
        exec ':!ctags -R --languages=php -f ' . curNodePath . '/tags ' . curNodePath
    endfunction
    nmap <silent> <F4> :call CreateTags()<CR>
    

    And it works! By pressing F4 I get tags file in project folder.

    • Alex Plugaru
      Alex Plugaru over 12 years
      try :call createTags() also try naming the function with a capital letter.
  • Dmitry Teplyakov
    Dmitry Teplyakov over 12 years
    Thanks, I think it is what I need. Can you answer on more question please? See my EDIT.
  • Dmitry Teplyakov
    Dmitry Teplyakov over 12 years
    Ok, it's works. But what about more one projects. After creating new project I must add full path to this new project? And so again and again? May be it can be automated?
  • Dmitry Teplyakov
    Dmitry Teplyakov over 12 years
    I understand what you mean, but I can't understand how to do it) I added set tags=./tags,tags and set autochdir in my .vimrc file. But tags not founded because vim starts to find them from /home..
  • romainl
    romainl over 12 years
    Vim starts from home because it's probably the working directory. What does :pwd says?
  • Alex Plugaru
    Alex Plugaru over 12 years
    I responded on the original post
  • Dmitry Teplyakov
    Dmitry Teplyakov over 12 years
    Oh, I'm sorry) I forgot semicolon after set tags=./tags,tags. Thank you very much!
  • thom_nic
    thom_nic almost 9 years
    using ./tags; as mentioned in another answer is highly preferable to hard-coding paths in .vimrc.
  • Big McLargeHuge
    Big McLargeHuge about 8 years
    Is the semicolon behavior documented anywhere? It's not mentioned in :help 'tags'.
  • Big McLargeHuge
    Big McLargeHuge about 8 years
    Found it: help file-searching
  • Matthew Hoggan
    Matthew Hoggan over 7 years
    you could also use github.com/embear/vim-localvimrc to have a "local vimrc" per project where you define different tags.