Vim provides super timesavers through its abillity to read output from external commands into the current buffer. The syntax is :r !command options and together with some clever little key mappings this can save a lot of keystrokes.
A few simple examples for starters, before we go on to the more involved stuff.
:r !ls will pull in a listing of your current working directory
:r !date will pull in a date string formatted according to your locale
:r !pwd will print a line with the path to your current working directory
Or how about the ouput from a pipe?
:r !ls | grep "\.html" will pull in a list of all html docs from your working directory
etc. etc. you caught the drift, I assume.
Now for the next level
Say, for instance, that you are a nut like me and maintain RSS feeds by hand. Writing the <pubDate> element complete with RFC-2822 compliant date format is tedious to say the least.
Time for some ex-command magic combined with a key mapping. Open your .vimrc in Vim and add this mapping to it:
map <F4> i<pubDate><Esc>:r !date --rfc-2822<CR>0i<Backspace><Esc>$a</pubDate>
Of course you may already have F4 mapped to something else - take your pick of all the other F-keys.
So what happened here?
i tells Vim to enter "insert" mode and write the opening <pubDate> tag. Then <Esc> tells Vim to enter "normal" mode again, enter ex-mode with :r and with !date --rfc-2822<CR> call up the external command.
Since :r adds a new line below the cursor position, we need to pull the output of date up by one row - this we achieve with 0 (go to beginning of line) i (enter insert mode) and <Backspace>. Next we enter "normal" mode again with <Esc>, jump to end of line with $ and enter "append" mode with a to write the closing </pubDate> tag.
Save your .vimrc and relaunch Vim. From now on pressing <F4> in "normal" mode will give you a valid <pubDate> element like this: <pubDate>Mon, 25 Aug 2008 19:19:02 +0200</pubDate>.
But wait - there's more
Vim can of course also pull in output from your own shell scripts.
In my never ending quest to save keystrokes I have amassed a collection of fragments of particularily tedious pieces of typing such as Public Identifiers for HTML docs, or XML snippets for RSS. These now sit in a directory named "fragments" and wait for their call.
Whenever I need one of those fragments I could of course type :r ~/fragments/somefrag.txt and already achieve considerable savings on keystrokes. But how about reducing all this to <F2> plus a single word and a carriage return?
Step 1 - a bash script
#!/bin/bash
TYP=$1
case "$TYP" in
"xstrict" ) cat ~/fragments/xhtmlstrict.txt;;
"xloose" ) cat ~/fragments/xhtmlloose.txt;;
"strict" ) cat ~/fragments/html4strict.txt;;
"loose" ) cat ~/fragements/html4loose.txt;;
"item" ) cat ~/fragments/rss-item.txt;;
# and so on to somewhere in Movieland
esac
This script sits in /usr/local/bin as frags.
Step 2 - the mapping
In my .vimrc I added map <F2> :r !frags.
Now when I press <F2> (in "normal" mode) my command line will read :r !frags. All I need to enter now is one of the "cases" from my script, press <CR> and the fragment will be pulled into my current buffer. Neat, ain't it?
The possibillities are only limited by your imagination. You can create your own system of templates for all your projects and pull them into a buffer with a few simple keystrokes.
Bookmark/Search this post with:
Re: Using the :r command in Vim for fun and profit
Saving keystrokes is always a good thing! Good post!