jayko03
jayko03

Reputation: 2481

vim add character in front of numeric value

I have some strings,

- astropy=3.1.2=py36h7b6447c_0
- ptyprocess=0.6.0=py36_0
- qt=5.9.7=h5867ecd_1
- xlwt=1.3.0=py36h7b00a1f_0
- lzo=2.10=h49e0be7_2

I want its output like,

- astropy==3.1.2=py36h7b6447c_0
- ptyprocess==0.6.0=py36_0
- qt==5.9.7=h5867ecd_1
- xlwt==1.3.0=py36h7b00a1f_0
- lzo==2.10=h49e0be7_2

I was thinking block these strings and shift + : and change = to ==. however there is another = sign. So I am wondering how to add = sign in front of numeric character.

Upvotes: 3

Views: 189

Answers (1)

Omri Sarig
Omri Sarig

Reputation: 339

It seems that your problem is that you want to change only the first equal sign, and not change the second one.

You can do it with Vim's substitute command. By default, the command would change only the first match in any given line, so you would like to keep it that way.

The actual command that would do this for you would be:

:%s/=/==

For more information, you can read the help of the command by running:

:help :s


In case you want to change all the values of = before a number (to answer your original question), you can change the search pattern to have an equal sign and then a number, and add another equal sign before the first one. This substitute would look like this:

:%s/=\d/=&/g

To break down the previous command:

% - Run the command on the whole file.

s - Run the substitute command

/ - start the search pattern.

= - Find the char of equal sign.

\d - Find any numeric value (1, 143, 94...)

/ - Start the replace value.

= - Append an equal sign to the return value.

& - Append the search pattern to the changed value.

/g - Run this command globally, meaning, change all the matches, and not just the first one in every line.

Upvotes: 6

Related Questions