8

I'm curious if it is possible to make a text object aware of its operator. In particular, I am writing a text object for LaTeX environments, where I want different behaviour depending on the operator. For instance, consider the following LaTeX snippet:

\begin{example}
  Hello world
\end{example}

Here it is most convenient of die deletes the content in a linewise fashion, whereas cie deletes "Hello world", but preserves the indentation, i.e., gives

\begin{example}
  |
\end{example}

where | is the cursor.

2
  • 1
    This is normally controlled not by which operator is used, but by defining the motion as linewise or not. You want to avoid writing to specific operators so that your text object can with nicely with custom operators as well.
    – tommcdo
    Commented Feb 16, 2016 at 16:47
  • The entire point is that I want the motion to be linewise in some cases, but not in other cases, depending on which operator is used. I understand that this may seem inconsistent, but in this particular case, I think such a differentiation is natural. Commented Feb 16, 2016 at 18:32

1 Answer 1

5

The following example comes close to what you are asking for:

onoremap <expr> w '<esc>' . v:operator . v:count1 . (v:operator ==# 'd' ? 'aw' : 'iw')

It creates a textobject w that is either aw, in case it is used by the delete operator, that is, dw = daw, or iw otherwise, for example cw = ciw.

1
  • Thanks! The v:operator variable was just what I was looking for! Commented Feb 16, 2016 at 18:28

Your Answer

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

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