5

I'm writing a VimL function that needs to behave differently for different filetypes. I know I can use &filetype to detect that, but my issue is with files that have embedded languages. Let's say I have this:

<html>
<head>
  <style>
    div {
      text-color: red;
    }
  </style>
</head>
<body>
  <div>Some text</div>
</body>
</html>

If the cursor is inside the style tag, I want to know that I'm dealing with CSS. Is this possible?

1 Answer 1

5

synIDattr(synID(line("."), col("."), 0), "name") will return css* (like cssStyle or cssDefinition) inside the style tag.

4
  • 4
    If this is something you do often then Tim Pope's scriptease.vim plugin provides zS mapping. Poor man's mapping: nnoremap zS :echo join(reverse(map(synstack(line('.'), col('.')), 'synIDattr(v:val,"name")')),' ')<cr> Mar 24, 2017 at 21:27
  • seems to return blank for me. Jan 13 at 13:16
  • @artfulrobot you need to have ft defined for it to work (for example in a syntax highlighted file)
    – laktak
    Jan 13 at 19:48
  • yeah it is defined. Jan 16 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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