0

So I have a configuration file that I want to modify using Vim. I want to change a ******* into something else, but after selecting the ******* in visual mode, I am not sure what am I supposed to do to change the string into something else.

Say I want to change ******* to www.sample.com after maneuvering the cursor to the start of the * and keep pressing l to move to the end of *(by the way, is there a better way to select the string that is composed of the same text?)I am not sure how to modify the string.

If I press esc to get into normal mode, the selecting is apparently no longer there.

2

Short answer:
You can press c to delete the selection and enter insert mode right away


To go to the end of the ******, the easiest thing would be to know what's following it. If there's a space after, for example:

I really like the smell of ****** flowers.
                           ^

Here are the simplest solutions to replace ****** by yellow:

  • c E yellow
  • v E c yellow

If you want to replace all the ****** with yellow in the document, you can use:

:%s/\M******/yellow/g

The \M is the nomagic flag, that allows you to use the * in the expression. You could also use:

:%s/\*\*\*\*\*\*/yellow/g

2
  • 1
    :s/from/to/g will change the all the froms on the current line. That is what the g flag does. To change occurrences in the whole file, you need to give a range to the command, like so: :%s/from/to/g. % is the shortcut to indicate the whole file.
    – Phil R
    Jan 25 at 13:28
  • You are completely right, let me change that!
    – Zorzi
    Jan 26 at 9:35
0

Using visual selections is a practice that should be avoided. It's handy in some limited situations, but you can do most everything without it. One of the big reasons to avoid it is the non-repeatability of the whole operation, which is to say the . command will not do what you want it to do. The reason is that selecting something is one operation, and then changing the text is another operation. If you want to repeat the change somewhere else in the document by visually selecting and then pressing ., it will not work because . repeats the visual selection.

If you use @Zorzi's :s suggestion, you can add the c flag to the end to make Vim ask for confirmation of each change: :%s/\*\+/www.sample.com/gc (The pattern \*\+ looks for one or more asterisks.)

Another approach is to use the search command: /\*\+ followed by the cgn command. This will change the found text under your cursor. . will find and change the next one, and n will find the next one without changing it. I use this approach quite often.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.