Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctags unable to find variables that contain hyphen

I'm a big user of Vim's ctags ctrl-] shortcut. I recently made a tag file in which the primary language is Make. When I try to ctrl-] with my cursor over a variable with a hyphen (ex. dl-routines), I get an error. If my cursor is over 'dl' within the 'dl-routines' variable I get the error

tag not found: dl

If my cursor is over 'routines' within the 'dl-routines' variable I get the error

tag not found: routines

I'm aware of Vim's

:ta tagname

However I'd like to use ctrl-] as it gives me less room for error.


2 Answers

In this case it's probably worth altering 'iskeyword' option to include dash. It may have many other effects, but they all should be quite useful. The only trick is to make these changes locally:

autocmd FileType make setlocal iskeyword+=45
like image 59
Matt Avatar answered Jan 24 '26 10:01

Matt


Setting 'iskeyword' option locally is probably the simplest solution. As an addendum to @Matt's answer, if you want to keep your option clean and add 45 only when you press <C-]>, you could use this trick function.

function! CWordWithKey(key) abort
    let s:saved_iskeyword = &iskeyword
    let s:saved_updatetime = &updatetime
    if &updatetime > 200 | let &updatetime = 200 | endif
    augroup CWordWithKeyAuGroup
        autocmd CursorHold,CursorHoldI <buffer>
                    \ let &updatetime = s:saved_updatetime |
                    \ let &iskeyword = s:saved_iskeyword |
                    \ autocmd! CWordWithKeyAuGroup
    augroup END
    execute 'set iskeyword+='.a:key
    return expand('<cword>')
endfunction

which adds the parameter key to iskeyword and sets a self-destructing autocmd to restore your older iskeyword after 200 ms.

Than remap <C-]> in ftplugin/make.vim or with autocmd FileType make ... as the previous answer.

nnoremap <buffer> <silent> <C-]> :execute 'tag '.CWordWithKey(45)<CR>
like image 26
perelo Avatar answered Jan 24 '26 12:01

perelo