I thought this deserved to be a separate answer so, there.
You can add CSS support to ctags
and use it to jump to definition the same way you would do for JavaScript. It's as simple as adding the following lines to the ~/.ctags
file:
--langdef=css
--langmap=css:.css
--regex-css=/^[ ]*.([A-Za-z0-9_-]+)/.1/c,class,classes/
--regex-css=/^[ ]*#([A-Za-z0-9_-]+)/#1/i,id,ids/
--regex-css=/^[ ]*(([A-Za-z0-9_-]+[
,]+)+){/1/t,tag,tags/
--regex-css=/^[ ]*@medias+([A-Za-z0-9_-]+)/1/m,media,medias/
Once you are done, the clasic :!ctags -R .
will correctly index your CSS files and you'll be able to do :tag .myClass
to jump to the correct CSS definition in the correct CSS file.
There's one catch, though. The classes an ids tags will include their .
or #
so :tag myClass
won't work while :tag .myClass
will. The simplest solution is to use "regexp search" instead of "whole tag search": :tag /myClass
.
These two mappings have worked flawlessly (for JS for a while and for CSS since a few days) :
" alternative to <C-]>
" place your cursor on an id or class and hit <leader>]
" to jump to the definition
nnoremap <leader>] :tag /<c-r>=expand('<cword>')<cr><cr>
" alternative to <C-w>}
" place your cursor on an id or class and hit <leader>}
" to show a preview of the definition. This doesn't seem to be
" very useful for CSS but it rocks for JavaScript
nnoremap <leader>} :ptag /<c-r>=expand('<cword>')<cr><cr>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…