Vim: delete all words in a string except the last

I'm having trouble getting vim find / replace to remove all words in the string except the last one, i.e. in the SQL bit, it would be nice to create a list of all the aliases from the code group

select column_a alias_a, column_b alias_b, column_c alias_c from ... 

I would just generate a list

 alias_a, alias_b, alias_c 

Therefore, I think that I want to delete all words that are not immediately followed by a comma and a line ending with

+6
source share
4 answers

option 1 :

 %s/\v.*\s(\S+)$/\1/ 

option 2 : using a macro

 qa$T d0jq 

then x@a x - how many lines do you want to apply to this macro

option 3

refer to an external command:

 :%!awk '$0=$NF' 

option 4 : if you have Align or a similar plugin, align these lines to the right, then use cv select and remove blocks, just leave the last column.

+7
source

This should do it:

 :%s/.* \(.*\)\n/\1 / 
+4
source

I would do:

 :%s/\v(.*\s)(\w\+)/\2/ 

This means that you need to capture everything to the last place in capture group 1, everything after that into capture group 2 and replace it only with 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 skip Shift-J several times to get aliases in one comma.

+3
source

Visually select the rows, then do the following

 :norm $bd0 :*j 

Note:: :norm will display as :'<,'>

For more help see:

 :h :norm :h :j 
0
source

All Articles