Reputation: 1716
I have one gigantic file I need to update to create a list (list a csv) but I have only 1 column.
I want to create 2 columns with a key/value pair combo.
I have for example:
Key 1 is NICE
Key 2 is good
Key 3 is Awesome
I need:
key 1 is nice|Key 1 is NICE
key_2_is_good|key 2 is good
key_3_is_awesome|Key 3 is Awesome
So the first will be all lowercase with _ instead of space and the second one is the normal string
I have so far:
:%s/^\(.*\)$/\1|\1/
Which is good but how can I fix the space to be _ and all lowercase?
thanks
Upvotes: 2
Views: 223
Reputation: 196751
Macros can be more intuitive than substitutions:
qq " start recording in register q then…
0 " go to the first column then…
y$ " yank from here to end of line then…
A| " append a pipe at the end of the line then…
<C-r>=substitute(@", " ", "_", "g")<CR> " insert text of first column with spaces replaced by underscores then…
<Esc> " get out of insert mode then…
q " stop recording
after that, select down to the last line and do
:'<,'>norm @q
Upvotes: 5
Reputation: 8248
In this case, you can make use of having the second part of the substitution being evaluated as an expression as explained in the help below :h sub-replace-special
.
If I have understood your question correctly, this should work:
%s/.*/\=substitute(tolower(submatch(0)), ' ', '_', 'g')."|".submatch(0)
submatch(0)
returns the complete matched text in the :s
command, the tolower()
function converts it to lower case and finally the substitute()
call replaces all whitespace by _
Upvotes: 4
Reputation: 45672
As a start try this:
:%s/^\(.*\)$/\L&\E|&/
output:
key 1 is nice|Key 1 is NICE
key 2 is good|Key 2 is good
key 3 is awesome|Key 3 is Awesome
Read more at vim.wikia
Upvotes: 2