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.

  • 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 Feb 16 '16 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. – Karl Yngve Lervåg Feb 16 '16 at 18:32
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.

  • Thanks! The v:operator variable was just what I was looking for! – Karl Yngve Lervåg Feb 16 '16 at 18:28

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.