Reputation: 677
I have a list of dates in a .txt file:
11/05/2017
12/05/2017
15/05/2017
16/05/2017
17/05/2017
I need to add a quote before and after them and end each line with a semicolon instead of a space.
My output should be:
'11/05/2017'; '12/05/2017';'15/05/2017'; '16/05/2017'; '17/05/2017'
How do I get this in Vim?
Upvotes: 2
Views: 1144
Reputation: 550
There is a cool plugin that can surround the statement in quotes. It is called Surround.Vim. http://vimawesome.com/plugin/surround-vim. As far as the second part I would write a macro to remove the line break at the end of the line and add a semicolon. You can run macros on multiple lines at once. http://vim.wikia.com/wiki/Macros. I hope this helps. Let me know if you have any questions.
Upvotes: 0
Reputation: 29458
Replace the text with regex, then join the lines.
:%s/\(.*\)/'\1';
Then ggVG
to select all, then J
to join the lines.
Update: As @pbogut mentioned in the comment, you can do this at once with this regex:
:%s/\(.*\)\n/'\1';_
Note that I added _
instead of (space) as it is not visible. You should use space instead.
Upvotes: 4
Reputation: 195269
usually macro will beat ex command in vimgolf. ;)
(assume your cursor is on first line):
0qqi'<ESC>A'; <ESC>Jq
then
99@q
To replay.
Or using recursive macro to avoid the 99
Oh, after that there would be a ;
at the end.
Upvotes: 0
Reputation: 32976
You can also do everything in one :substitute
by matching the newline.
%s/\v(.{-})\n/'\1';
Upvotes: 0