0

I have this part of code:

function! Test()
    echom synIDattr(synID(line('.'), col('.'), 1), 'name')
endfunction

autocmd TextChangedI <buffer> call Test()

I expected that every time I change some stuff the current "type" would be printed out. For example, if I'm in a commentary line of vim, I'd expect to see that at the bottom: vimLineComment. But nothing pops up.

If I enter

:echom synIDattr(synID(line('.'), col('.'), 1), 'name')

I'm getting my expected output. What am I doing wrong?

I've also tried this (which was written in :h synIDattr):

echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "name")

But it also doesn't work inside my Test() function:

function! Test()
    echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "name")
endfunction

autocmd TextChangedI <buffer> call Test()

If I insert echom "Test" I'm getting Test at the bottom everytime if I enter something. What am I doing wrong?

2
  • Are you sure you're typing in an area of text that matches defined syntax? I tried your code and nothing shows up if, say, I'm on an empty line but if I start inserting in the middle of something with syntax highlighting it works. Another illustration: type on the same line as endif in a function. Cursor before endif: success, cursor after endif: nothing. Which makes sense.
    – B Layer
    Jan 24 at 0:16
  • Oh wait, if I do that what you said, than I'm getting it as well. Hm.... I thought that it would work more "in general" and not in this "specifique" cases. Looks like I have to look for something else... Thank you! Could please write that as an answer so I can tick this question as answered? And what do you mean with "Which makes sense."? Is it because this function is written like that?
    – TornaxO7
    Jan 24 at 0:24
2

The provided code seemed to be working when I tried it and we established that OP is seeing the same behavior as I am. So that leaves a difference in expectations, I think, about what the correct behavior is.

Three important pieces of information:

  1. Text colored by syntax highlighting is selected by rules, often regex patterns, and they specify where a matching block of text ends.
  2. If you start appending text to the end of a line the cursor will usually be beyond the end of one of those defined blocks and so, naturally, you won't be inside a syntax item and nothing will be displayed by the autocmd/function.
  3. In Normal mode the cursor is less likely to be beyond the end of a block since even after running $ it sits on top of the last character of the line rather than beyond it.

The example I gave in a comment was entering text before and after and endif statement in a vimscript function. With the former you'll see something like VimFuncBody but with the latter nothing will be displayed. Conversely, with the cursor at the end of the same line in Normal mode if you enter :call Test() you'll see VimCommand. Hopefully the three points above explain all this behavior.

Ah, but what about a line that begins with ", i.e. a line comment. Since the pattern for that extends to the end of the line (regex $) we should see vimlineComment when adding to the end of the comment but we dont! Well, no, because with " my comment the block ends on that final t ($ is a zero-width match) but the cursor is beyond that when we begin appending to the line.

(Admittedly, that last case is a little troubling because the text you type actually gets the appropriate comment color. Seems like a quirk of the implementation. Clearly dynamic text coloring and the output of synID*() don't operate by precisely the same rules and/or the same refresh timings.)

2
  • Good explanation! Thank you so much sir! c(^-^)c
    – TornaxO7
    Jan 24 at 9:57
  • No problem. Cheers.
    – B Layer
    Jan 24 at 22:41

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.