2

This is for an indent function that should maintain the indentation should the cursor happen to be in a string region. It maintains the indentation when I hit == in normal mode but changes when I open a new line in the same string.

if synIDattr(synID(line('.'), line('.'), 0), 'name') =~ 'string'
  return -1
else
  return correct_indentation
endif

Running the function in insert mode returns an empty string:

<C-R>=synIDattr(synID(line('.'), line('.'), 0), 'name')

The docs don't mention anything about editing modes, just that it'll give different result when in gui mode from when in term mode

  • 1
    If I remember correctly, when in insert mode try to use col('.') - 1 or similar, to check before the character you are actually just inserting. Ah, I see that Christian's answer suggests exactly that. – VanLaser Jan 16 '16 at 19:42
3

The function synID has to be called with the line and column position of where you want to get the syntax information from. Therefore, you need to replace the second line('.') call by a col('.') call.

If you get a zero, this means, that there is no syntax information available for that position. This could be the case, because in insert mode, your cursor is usually one column behind the last inserted text.

  • i meant to write col('.') on the second argument, small mistake. I've just noticed it returns an empty string because the newly opened line has no text. Is there a way around this? i really need it – Segfault Jan 16 '16 at 12:53
  • why do you need this in a new line? – Christian Brabandt Jan 17 '16 at 13:06
  • For correct indentation when in a multiline string for example. Why does synID fail exactly when there's no content on the line? How else would one detect whether they are in a string or code section? – Segfault Jan 18 '16 at 20:34
  • I think, use col('.') -1 if available. Because in insert mode with col('.') you are outside of any context. – Christian Brabandt Jan 19 '16 at 1:49

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.