aboutsummaryrefslogtreecommitdiff
path: root/README
blob: 5ad7225bcdc0e92507328a4a3e1f5cbf0f4919f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
vp is a powerful UNIX tool for interactively building shell commands.
For more information, read the vp.1 manual.

Below are some examples of how I've successfully used vp to perform
advanced ad-hoc text processing in my everyday life.

= Sort paragraphs alphabetically =

I once found myself wanting to sort function definitions alphabetically.
Luckily, the solution was simplified by the fact that functions
didn't contain any blank lines.  Here's how I did it.

 1. Open the file in vi.

 2. Cut the function definitions to the clipboard:

	!xsel -i

 3. Launch vp:

	:!vp

 4. Write something along the following lines:

	xsel -o | perl -00 -ne '
	push @d, $_;
	END { print for sort @d }' 

 5. When you've found a command that works, press q to keep
    the output.

 6. Copy the buffer:

	:%!xsel -i

 7. Quit vp, return to the original vi session and paste the
    sorted function definitions.

For a more general solution, which supports blank lines within
definitions, I came up with the following command:

	xsel -o | perl -ne '
	@d[$i] .= $_;
	$i++ if /^}/;
	END { print for sort @d }
	'