Reputation: 1841
I'm having trouble coming up with a vim find/replace to delete all the words on a line except the last one, i.e. in a bit of SQL, it be nice to generate a list of all the aliases from a bunch of code
select
column_a alias_a,
column_b alias_b,
column_c alias_c
from
...
I'd like to just generate the list
alias_a, alias_b, alias_c
So I think i want to delete all words that aren't immediately followed by a comma and line ending
Upvotes: 8
Views: 3213
Reputation: 45177
Visually select the lines then execute the following
:norm $bd0
:*j
Note: :norm
will show up as :'<,'>
For more help see:
:h :norm
:h :j
Upvotes: 1
Reputation: 195269
option 1:
%s/\v.*\s(\S+)$/\1/
option 2: using macro
qa$T d0jq
then x@a
x is how many lines you want to apply this macro
option 3
turn to external command:
:%!awk '$0=$NF'
option 4:
if you have Align or similar plugin, align those lines right, then use c-v
block select and remove, just leave the last column.
Upvotes: 10
Reputation: 35943
I would do:
:%s/\v(.*\s)(\w\+)/\2/
Which means to grab everything until the last space into capture group 1, everything afterward into capture group 2, and replace it with just capture group 2.
Replaces this:
select
column_a blah blah blah alias_a,
column_b foo foo foo alias_b,
column_c bar bar bar alias_c
from
To:
select
alias_a,
alias_b,
alias_c
from
You can then punch Shift-J
a few times to get the aliases into one comma separated line.
Upvotes: 3