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

4. Visual Block Select from Start to foo

Pressing down Shift or Ctrl, followed by v (so that two key are depressed at the same time) will visually select a row or a cell, respectively.

Now, press / and type foo to select from this current selection until the first instance of foo.

Hit Return or Enter to accept. Press n to advance to the next instance, or hold Shift and press n to go back to a previous instance.