Tiny Code

Tiny Code

Monday, November 19, 2007

I/O redirection into an editor

Sometimes it's handy to redirect the output of a Linux/Unix command into an editor. One reason to do this might be to try to weed out unwanted search pattern matches when running grep. Fortunately the Linux (and before that Unix) developers have anticipated this need. Editors like vi, vim, gvim, and gedit (and possibly others I'm not aware of) can all accept the output of a command as the "file" being edited.

For example, you can direct the output of a grep command to gvim with the following command:

grep searchPattern | gvim -

If you really want to get fancy, you can pass a command to gvim to cause it to search for the same pattern you used grep to find by using the -c argument.

grep searchPattern | gvim -c/searchPattern -

Since searchPattern appears twice on the command line and engineers never want to type any extra characters, I'd define a command alias to perform this action. Since there are so many shells out there, I'll leave that as an exercise for the reader.

No comments: