Vim, and I'm sure neovim, allow visually selecting an area by lines. If you then type ":" to initiate a search and replace, it will apply only to those lines.
Say I want to change "foo" to "bar" in a single function in my file. I hit "V" to enter by-line visual mode at the top of the function, go to the end of the function and type ":s/foo/bar/g". If there are other words in the function that contain foo as a substring, then ":s/\<foo\>/bar/g".
> If there are other words in the function that contain foo
How would you know that???
So that's the first roundabout eliminated:
- set word selection by default not to have to worry about substrings
But otherwise you're describing a different workflow: while there is no semantic meaning in the "next 5 lines", you actually don't know whether that would be 5 of 7, that's the whole point/benefit of incrementalism, there is in "this function", so block operations are a valid contender and I could also select the function and search&replace within the selection instead of doing word-by-word.
But then the original also works in a more direct way (maybe you actually don't want to replace all "foo" in a function, but skip some of them):
So you would skip your two roundabouts:
- go to the beginning of a function
- go+select to the end of a function
Then proceed with the "direct" way:
- go to your desired word anywhere within the function
- invoke select forward/backward commands until visually reaching the beginning/end of said function (the candidates will all be highlighted)
Thank you for your detailed reply. So let me address the points.
> How would you know that???
Typically I'm working on code that I wrote, or I've been studying it enough that I've gotten to the point that I'm refactoring and editing it. It isn't like a gameshow where I have a blinder on and then the blinder is removed and I'm given a task to perform.
> - set word selection by default not to have to worry about substrings
So you have to do another step? I can pick word selection or not dynamically on a case by case basis without having to go somewhere else and turn word mode on or off. Or maybe I'm misunderstanding how your editor works. At any rate, I don't see how my approach is roundabout -- I directly express if I want complete word matching or not, and I can even mix both types in a single search/replace operation.
I don't understand your next point -- I can select the body of a function without knowing how many lines it has. It isn't clear to me what your approach is, "incrementalism". Are you doing a word search and replace one at a time? I thought we were discussing multi-cursor editing.
> Then proceed with the "direct" way:
> - go to your desired word anywhere within the function
> - invoke select forward/backward commands until visually reaching the beginning/end of said function (the candidates will all be highlighted)
What you've described is just another way of doing it, not fewer steps.
> Typically I'm working on code that I wrote, or I've been studying it enough that I've gotten to the point that I'm refactoring and editing it
That doesn't explain it. You can't know for any "foo" whether all the content, including comments, has "foo" as a subword for any non-trivial amount of text because you're not a computer.
> so you have to do another step?
No, you reduce many steps having to specify word-based selection every time with a cognitive overhead ("If there are other words") with a single default.
> I can pick word selection or not dynamically
So can I, and I don't need to do precision jumping in the middle of a combined text field to do that, but that's a different benefit
> It isn't clear to me what your approach is, "incrementalism"
when instead of counting 5 lines and selecting them you press a selection key the amount of times is needed to select what you need
> I can select the body of a function
and as I've pointed out, that's a different workflow. You've added a "in this semantic block" requirement and mistakenly compare it to the original incremental one
> just another way of doing it, not fewer steps.
it is fewer steps, I've pointed out which steps are not needed, and then there is another condition complicating your simple explanation when it's not a certainty that you actually want to replace everything within a block.
But again, within semantic blocks there are different tradeoffs
I'm not dumb and I'm sure you aren't either, so I think we are failing to imagine the same scenario the other is.
>> I can select the body of a function
> and as I've pointed out, that's a different workflow. You've added a "in this > semantic block" requirement and mistakenly compare it to the original > incremental one
Yes, I picked that scenario because doing search & replace on all vars of a given name across the whole file doesn't require selection. Yes, I would normally want to replace that var name when it appears in comments too. Yes, I typically have a very good if not perfect knowledge of whether foobar is a variable name only or might appear as a substring of something else. If my variable name was "n" then I'd have to take more care.
The original scenario explicitly states "not every instance in the open file ", so not sure why you bring that up
> in comments
same, where is this coming from? That adds no difference to these 2 workflows
> very good ... knowledge
This is just self-deception, you don't have the full text with auto-matching built into your brain, and since 'n' is an rethorical alternative, in reality you wouldn't even have a great way of knowing when to take more care
I hate to break it to you but you don't. I know that because there have been studies trying to show how using the keyboard is more efficient than the mouse. Every time, even when people swear they don't use the mouse to navigate text files, they do, and they do it a lot, nearly as much as anyone else. It's a great demonstration to how you just can't trust people's testimonies as they lie while actually believing their own lies (i.e. the really believe to be telling the truth which is fascinating).
I am proficient in emacs and don't need the mouse to do anything, but I am conscious I still reach for the mouse when more complex movements are required because it would be extremely dogmatic and unproductive to do otherwise, as it's just so much easier, and there's absolutely no shame in using the right tool for the job.