tags:

views:

138

answers:

3

I have a line like below.

fullname = (this is a test name);

I want to double quote all the strings inside "(" and ")".

i.e fullname = ("this" "is" "a" "test" "name");

Can someone give me a vim regex to do that?

A: 

This command puts everything which is not space, tab, equals sign, paren or semicolon to quotes:

:s/[^ \t=();][^ \t=();]*/"&"/g

Please note that this quotes fullname as well, but you can remove those quotes manually.

Sometimes it is worth doing part of the work manually, because automating it would be slower.

Run the command:

:help substi

to to get help about regexp substitution in vim.

pts
@pts, I want don't want to any manual removal
chappar
Not working. This encloses everything in quotes.
Tomalak
I think it's working as documented in the answer, I've tried it before submitting my answer. What do you mean by `everything'?
pts
"Everything" as in "Everything that is not a space, a tab, an equals sign or a paren." In fact even the semi-colon at the end of the line comes out as quoted.
Tomalak
Made it more specific in my answer what it does.
pts
+4  A: 

I would do this as follows:

:s/\<\w\+\>/"&"/gc

Due to the confirmation switch 'c' you will be asked for each replacement. Answer 'n' (no) for the replacement of 'fullname' and 'a' (all) for the rest of the line.

fgm
Thanks. I didn't know about c option
chappar
This answer is not really solving the problem. It does the "quote all words" part, but completely forgets the "inside parentheses only" part.
Tomalak
+3  A: 
Tomalak
thanks. this will do
chappar
What does very magic do?
chappar
It removes the necessity to escape certain characters, making the regex shorter than it would be normally. See vim help -- :h \v
Tomalak