vimtex icon indicating copy to clipboard operation
vimtex copied to clipboard

Change change surroundings

Open aloispichler opened this issue 3 years ago • 4 comments

Hello again!

I keep forgetting about the csc, cs$, csd, cse, and … commands, and there are these toggle commands, in addition. There are too many of them, and they are all too specific. It would be nice to have an intelligent and smart change environment ce (i.e., change surrounding anything) to change whatever is next to the cursor. If necessary, vimtex would add \begin{…} and \end{…}. Examples include

from (based on cursor position)      to (based on user input)       comment
( … )                                [ … ]                          delimiters
{ … }                                \{ … \}                        delimiters
\{ … \}                              \left\{ … \right\}             brackets
\bigl( … \bigr)                      \big[ … \big]                  paired brackets
\begin{equation} … \end{equation}    \[ … \]
\[ … \]                              $ … $
$ … $                                \begin{align} … \end{align}
\textit{…}                           \emph{…}

The dialogue from csc, cs$, csd, cse is good, it should be reused! With the new ce, the old csc, cs$, csd, cse and the togglings can be abandoned.

Remark. At the moment, the dialog of csd and cs$ is even the same, so this should be harmonized and merged anyway. Tusen takk for your super useful extension!

aloispichler avatar Jul 24 '22 09:07 aloispichler

I keep forgetting about the csc, cs$, csd, cse, and … commands, and there are these toggle commands, in addition. There are too many of them, and they are all too specific.

This is clearly subjective. Personally, I really do use the different forms, and I believe a lot of people do.

It would be nice to have an intelligent and smart change environment ce (i.e., change surrounding anything) to change whatever is next to the cursor.

Interesting suggestion. Perhaps there's room for a new target here: css and dss (for deleting). I believe it could be implemented by detecting the type of context and then delegating to the existing functions.

With the new ce, the old csc, cs$, csd, cse and the togglings can be abandoned.

I don't agree. Simple counterexample: Given $ f(x) = (1 + x)^2$. Your suggested ce would not allow to change the $...$ if the cursor is within any of the parentheses. In this case, cs$ would be useful. Similar for all of the above. So, the specific forms should not be abandoned.

Remark. At the moment, the dialog of csd and cs$ is even the same, so this should be harmonized and merged anyway.

Curious: Could you expand on this? Perhaps there's a possible simplification available here, but it is not immediately clear to me.

Tusen takk for your super useful extension!

Vær so god :)


To summarize, it seems you may have a useful idea in implementing a smart change surrounding operator, e.g. css, that would delegate to the existing operators based on the current/closest context.

lervag avatar Jul 24 '22 21:07 lervag

Hello again!

The new super change should be a two-letter abbreviation, not 3. Let me use ce here. It will act at the cursor position: In further detail, with | indicating the cursor position: $|f(x) = (1 + x)^2$: type ce: dialogue change surrounding environment: $ … $ $f(x) = (|1 + x)^2$: type, ce: dialogue change surrounding environment: ( … ) | $f(x) = (1 + x)^2$: type, ce: dialogue change surrounding environment: document.

Delegating is perhaps not enough. Ideally, this would include to change $ … = (| …) $, after user input \left(, to $ … = \left(| …\right) $ and so on. I think vimtex does not cover this at the moment.

Agree, csc, cs$, csd, cse don't need to be abandoned, but they will be superfluous to a high extent. For this reason, 2-letters are better than 3. Well, it's just a suggestion. Thanks again.

aloispichler avatar Jul 24 '22 22:07 aloispichler

Sorry, I'm with @lervag here: the current mappings are good -- and they are good for a reason: they follow basic Vim grammar that matches both builtin text objects and vim-surround/sandwich. This means that they both profit from and reinforce muscle memory from the broader vim ecosystem.

Also, shorter mappings -- especially ones that don't follow established grammar -- run into the danger of shadowing other mappings. So not only would I personally not use your "super mappings", I'd caution against adding them by default.

(This is not an argument against adding a new <plug>(vimtex-super-change) mapping. Although I'm skeptical about a single mapping being able to do all the things you want it to do, even more often than not.)

clason avatar Jul 24 '22 22:07 clason

The new super change should be a two-letter abbreviation, not 3. ... Agree, csc, cs$, csd, cse don't need to be abandoned, but they will be superfluous to a high extent. For this reason, 2-letters are better than 3. Well, it's just a suggestion. Thanks again.

No, I still strongly disagree. These will not be superfluous at all. Yet, I still think you may be right that the smart version is interesting.

Further, @clason more or less explained the point about 3 letter mappings. The point is to adhere to something that is more or less a standard. But the default mapping is just that, and if I do implement this, you could easily create a personal, custom mapping.

$f(x) = (|1 + x)^2$: type, ce: dialogue change surrounding environment: ( … )

The parentheses are not an environment, so the dialogue should still be similar as the current csd.

| $f(x) = (1 + x)^2$: type, ce: dialogue change surrounding environment: document.

Just a short FYI: cse (and friends) will never act on the outer document environment, as that is more or less never what anyone wants.

Delegating is perhaps not enough. Ideally, this would include to change $ … = (| …) $, after user input \left(, to $ … = \left(| …\right) $ and so on. I think vimtex does not cover this at the moment.

That's true. There is a valid reason, in my opinion: Changing delimiters is not the same as changing the delimiter modifiers. I know many find this somewhat confusing, but having this distinction makes it easier to implement everything, and I think it works well. When you want to add/change the modifiers, you use tsd, if you want to change the delimiters, you use csd.

So, the new mapping would probably work something like this:

from                                 to                             comment
----                                 ----                           ----
( … )                                [ … ]                          like csd
\begin{equation} … \end{equation}    \[ … \]                        like cs$
\[ … \]                              $ … $                          like cs$
$ … $                                \begin{align} … \end{align}    like cs$
\textit{…}                           \emph{…}                       like csc

A couple of comments:

  • { ... } would never be turned into \{ ... \}, because {} are not delimiters! They are part of the TeX syntax itself.
  • You really never want to go from \bigl...\bigr to \big...\big (unless you know what you are doing and do want that; but it would be a rare thing!).

lervag avatar Jul 25 '22 09:07 lervag

Closing due to inactivity.

lervag avatar Feb 21 '23 19:02 lervag