user2405469
user2405469

Reputation: 2003

vim put quotes and comma around words

I Have a list of currencies in their abbreviations and long forms:

AED United Arab Emirates dirham
AFN Afghani
ALL Lek
AMD Armenian Dram
ANG Netherlands Antillian Guilder
AOA Kwanza
ARS Argentine Peso
AUD Australian Dollar
AWG Aruban Guilder
AZN Azerbaijanian Manat
BAM Convertible Marks
BBD Barbados Dollar
BDT Bangladeshi Taka
BGN Bulgarian Lev

I actually have 182 lines worth of them...in notepad++ I can easily in seconds get quotes around the individual words and commas after each word too, I was wondering whether there was a way to do this in vim or any other editor in Linux.

Even though I have already done it in notepad++ it would be nice to broaden myself to other editors too.

suggestions are much appreciated.

Upvotes: 8

Views: 5910

Answers (4)

Pablo R. Dinella
Pablo R. Dinella

Reputation: 790

I know it's an old question but I thought I would give my take on it.

It's the longest keystroke in the answers, but I think it's the most easy/natural way to do it quickly, at least for me (it really depends on what you are more familiar with and the way you think).

qq
cw"",<Esc>
h
h
p
W
C
""<Esc>
h
p
j
0
q

Then 13@q to run the macro on the rest of the lines.

This is the result:

"AED", "United Arab Emirates dirham"
"AFN", "Afghani"
"ALL", "Lek"
"AMD", "Armenian Dram"
"ANG", "Netherlands Antillian Guilder"
"AOA", "Kwanza"
"ARS", "Argentine Peso"
"AUD", "Australian Dollar"
"AWG", "Aruban Guilder"
"AZN", "Azerbaijanian Manat"
"BAM", "Convertible Marks"
"BBD", "Barbados Dollar"
"BDT", "Bangladeshi Taka"
"BGN", "Bulgarian Lev"

Upvotes: 0

emzap79
emzap79

Reputation: 46

to round up above suggested solutions, you could as well select visual blocks, and surround them with quotes. This might be the most similar approach to NP++ (even if I've never used Notepad so far!). A visual block is selected with ctrl-v, so my solution would be:

  • go to start of first line: gg0
  • select first visual block: <ctrl-v>Ge
  • surround block with quotes: s""<esc>P
  • jump to space and select them all to the bottom: f<space><ctrl-v>G
  • replace selected spaces: s,"

now, to finish the process, add quotes at the end of each line. You can either record a macro (see previous answer from romainl) or -- what I prefer --- execute a quick substitution: :%s/$/"/g<enter>, and you're done!

Upvotes: 1

romainl
romainl

Reputation: 196906

One could record a macro:

gg
qq
I"<Esc>
f<space>
s","<Esc>
A",<Esc>
q

and execute it on every line:

:%norm @q<CR>

Or do the same thing in one go:

:%norm I"<C-v><Esc>f<space>s","<C-v><Esc>A",<C-v><Esc><CR>

Or, of course use a substitution:

:%s/\(\w\+\) \(.\+\)$/"\1","\2",

Upvotes: 5

benjwadams
benjwadams

Reputation: 1580

Try this command in ex mode.

%s/\w\+/"&",/g | $s/,$//

It should do the trick.

Explanation: %s substitutes on all lines

In the match portion:

\w is a "word character", which doesn't include spaces

\+ says to match one or more of the preceding character

In the replacement portion:

& refers to the entire matched string

Lastly, the g means to globally replace on the line, and not just stop the replacement after the first line.

The vertical bar | can be used to separate ex commands and run them in sequence.

$s refers to substituting on the last line. The substitution after the vertical bar will remove any commas at the end of the line, indicated by the $ anchor. This will make sure you don't have a trailing comma at the end of your list.

Upvotes: 14

Related Questions