from within vi, pipe a chunk of lines to a command line and replace the chunk with the result

!}sort
The vi key sequence !}command will send the file contents from the cursor to the next blank line as STDOUT to the command specified and replace that sequence of file lines with the output of the command. For example: sorting a block of data - !}sort The sequence !{command will do the same but "upwards" (from the current position towards the start of the file.
Sample Output
a
b
c

12
By: mpb
2009-03-28 00:18:39

These Might Interest You

  • It's common to want to split up large files and the usual method is to use split(1). If you have a 10GiB file, you'll need 10GiB of free space. Then the OS has to read 10GiB and write 10GiB (usually on the same filesystem). This takes AGES. . The command uses a set of loop block devices to create fake chunks, but without making any changes to the file. This means the file splitting is nearly instantaneous. The example creates a 1GiB file, then splits it into 16 x 64MiB chunks (/dev/loop0 .. loop15). . Note: This isn't a drop-in replacement for using split. The results are block devices. tar and zip won't do what you expect when given block devices. . These commands will work: hexdump /dev/loop4 . gzip -9 < /dev/loop6 > part6.gz . cat /dev/loop10 > /media/usb/part10.bin Show Sample Output


    5
    FILE=file_name; CHUNK=$((64*1024*1024)); SIZE=$(stat -c "%s" $FILE); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done
    flatcap · 2014-10-03 13:18:19 2

  • 0
    head filename.txt | column -t -s $'\t'
    NLKNguyen · 2017-02-10 23:26:10 0
  • Useful to push strings in the stack Show Sample Output


    0
    python -c "s = '////bin/bash'; print [s[::-1].encode('hex')[i:i+8] for i in range(0, len(s[::-1].encode('hex')), 8)]"
    sinkmanu · 2017-01-30 22:05:37 0
  • While copying a large file that may take up a good chunk of your hard drive, start the copy and run this command concurrently. It will print out the disk information every second. It's pretty handy when you have a large copy with nothing to monitor the progress.


    5
    watch -n 1 df
    joem86 · 2009-02-18 21:34:06 1

What Others Think

this works in normal mode, not command mode as I first thought.
bwoodacre · 481 weeks and 2 days ago
I think you mean the block will be passed as STDIN to the command... A more useful(for me) variant is to highlight the text/lines in visual mode then type ":!sort"(the colon seems to be optional), leaving out the curly brace. You can also specify line ranges "10,20!sort" to sort lines 10 through 20. You can also pass parameters to the command (":!sort -f" to ignore case).
leper421 · 470 weeks ago
@leper421 No, I mean as STDOUT from using "!}sort" inside vi which is the piped to the sort command. It is, of course, STDIN for the sort command.
mpb · 335 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: