# Define text object that depends on the current operator

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.

• 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. 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. Feb 16, 2016 at 18:32

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! Feb 16, 2016 at 18:28