AndrewRadev/switch.vim

Switching all the flags at once

pachiras opened this issue · 2 comments

Hello,
Thank you for sharing the nice library.

I'm wondering if I could switch all the flags in a file at once.
Suppose I have a bash script like

#!/usr/bin/env bash

if true; then
    ./script1.sh
fi

if true; then
    ./script2.sh
fi

if true; then
    ./script13sh
fi

if true; then
    ./script4.sh
fi

I sometimes need to stop running all the scripts.
It would be nice to accomplish this by typing like gas

I would be happy if you could consider this.
Thank you.

It's an interesting idea, but I have a hard time imagining how it would work, exactly. The plugin looks for the text under the cursor and looks for what pattern the text matches. For instance, for the if true example for bash, it would match \C\<true\>.

What I could do is provide a command that replaces it, and then searches to replace the same pattern throughout the same file. But this would affect every true, not just the if true parts, so if you have any other usage of true (even in strings) it would also change and that might be unexpected.

Additionally, for a change like this, it would be good if the range of the operation was controllable, it's rare to have an action that replaces in the whole file. If I wanted to achieve this, I'd use something like %s/if true; then/if false; then/g. But I can also run this substitution on a limited area by selecting visually the first two blocks for example. With a "global switch", the cursor would have to be positioned on something first and then it would be impossible to select an area to operate on.

Can you describe how you'd imagine this global switch action to work in terms of interface? For now, I feel like this is not a good fit for the plugin, and I'd recommend creating a bash-specific command to do this, like putting these two commands in ftplugin/sh.vim:

command! -buffer -range=% Disable <line1>,<line2>s/if true; then/if false; then/g
command! -buffer -range=% Enable <line1>,<line2>s/if false; then/if true; then/g

Thank you for your kind reply. I appreciate it.

The command %s/if true; then/if false; then/g is exactly what I do always in the situation like this. And I was tired of typing this every time I needed it. Using your library, which is very helpful !, I just thought it would be nice to switch those flags in a single command.

But, you are right. That kind of functionality could cause other side-effects. We should not change it.
The commands you kindly proposed, by the way, look fit my needs. I will try it.

Thank you very much.