2

Concrete example:

When using React with Styled Components, I want to load the CSS snippets file. I could achieve this with:

autocmd FileType javascript,typescript :UltiSnipsAddFiletypes css

However, I'd like to execute the command above only if styled-components is included in the current buffer.

Is this possible?

2

Put the check in a function so it’s not a mess:

function react#add_snips() abort
  if search('styled-components', 'nw') != 0
    UltiSnipsAddFiletypes css
  endif
endfunction

(This can go in ~/.vim/autoload/react.vim)

And then call the function:

augroup react
  autocmd!
  autocmd Filetype javascript,typescript call react#add_snips
augroup END

Alternatively, place the call portion in both ~/.vim/after/ftplugin/javascript.vim and ~/.vim/after/ftplugin/typescript.vim.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.