Suppose I have the following input as shown below. What I would like to do is to visually select lines 2 through 4 (shift v) and then delete the word dog.
How can I do that? I know I can use something like :s/dog//
on my selection, but I was wondering if there's a more straightforward way.
1 The quick brown dog
2 dog jumps
3 over the
4 lazy dog,
5 but it should be just a fox.
The final output should be (affected only by the visual selection on lines 2 through 4):
1 The quick brown dog
2 jumps
3 over the
4 lazy ,
5 but it should be just a fox.
CodePudding user response:
I think there is no way in way to just replace a specific word in specific lines with visual selection. You can use sed
for that (look at #4).
Anyways:
Here are 3 way to delete the word dog in a file and one way to do it with sed
:
1 (with visual mode):
- Type
v
to enter visual character mode - Highlight
dog
- Press d for deleting
2 (with substitute):
:%s/dog//gc
g
stands for global
c
stand for confirmation
You will be ask for every entry, what to do with.
3 (with search mode):
/dog
Type: n
for next match
Type: d
for deleting the match
For further information: Search and Replace
4 (with
sed
):
sed 2,4\s/dog// filename
CodePudding user response:
That's impossible.
Why?
tl;dr
I think, you are envisioning something like a normal mode within visual mode (so that you can "move and delete like in Vim" while you are in visual mode). Such a thing doesn't exist.
Think about it: if you visual select some lines, then those lines, and nothing else, are the object of whatever action you do next (d, s, c, or whatever).
But you want to take an action not on those visually selected lines, but on words within them. But how can you tell Vim to take action on the words dog and not on other words? You can do that with movements, but as long as you are in visual mode, that's not possible, because any movement will just change the visual selection, and not allow you to somehow move within it.
This means that you need to abandon the visual selection so that you can move to those words and use them as the textual object for the action.
If you really want to start from the visual selection, then the only way to give the info that you want to take action on the words dog, is to type them out while you are in visual mode. And that's precisely what the :s
approach does.