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
Press d. Begin a search / Type the pattern matching the end of the deletion (here purpose for example) Press Enter.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With