Vim


Useful commands and macros that you'll probably forget.


1. Concatenate lines of text with yanked lines

:5,8del | let x=split(@") | 1,4s/$/\=remove(x,0)/

Example:

1| work it    1| work it harder
2| make it    2| make it better
3| do it      3| do it faster
4| makes us   4| makes us stronger
5| harder
6| better
7| faster
8| stronger

2. Remove characters after match on every line

:%s/[^total:]*$//g

Example:

Before...

|product_id:1151|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:10
|product_id:1151|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:50

After...

|product_id:1151|sku:|quantity:1|total:
|product_id:1301|sku:|quantity:1|total:
|product_id:1301|sku:|quantity:1|total:
|product_id:1151|sku:|quantity:1|total:
|product_id:1301|sku:|quantity:1|total:

3. Remove characters before match on every line

:%s/.*[total]/total/g

Example:

Before...

|product_id:1151|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:10
|product_id:1151|sku:|quantity:1|total:10
|product_id:1301|sku:|quantity:1|total:50

After...

total:10
total:10
total:10
total:10
total:50