Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: delete all words on line except last

Tags:

replace

vim

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

like image 443
justin cress Avatar asked Apr 05 '13 20:04

justin cress


People also ask

How do I delete multiple words in Vim?

Press d. Begin a search / Type the pattern matching the end of the deletion (here purpose for example) Press Enter.

How do I move the last word of a line in Vim?

In short press the Esc key and then press Shift + G to move cursor to end of file in vi or vim text editor under Linux and Unix-like systems. However, you can use the following keyboard shortcuts too.

Can you highlight and delete in Vim?

Once the text is highlighted, press the d key to delete the text. If you deleted too much or not enough, press u to undo and start again. Move your cursor to the new location and press p to paste the text.


1 Answers

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.

like image 51
Kent Avatar answered Oct 25 '22 23:10

Kent