vim put quotes and comma around words
Asked Answered
U

4

8

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.

Unipolar answered 13/6, 2014 at 16:41 Comment(1)
Do you want individual words quoted or the the currency abbreviations and then enclose the country names in quotes as well, which may include spaces?Acromion
A
14

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.

Acromion answered 13/6, 2014 at 16:56 Comment(0)
I
4

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",
Iaverne answered 13/6, 2014 at 19:27 Comment(0)
M
1

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!

Misbehave answered 14/6, 2014 at 21:40 Comment(0)
K
0

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"
Kickstand answered 10/4, 2020 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.