Open a file given by the result of a command in vim

You can use command substitution:

vim $(find -name somefile.txt)

or

find -name somefile.txt -exec vim {} \;

Try this:

  1. start vim
  2. in vim, use the following:

:r!find /<path> -name 'expression'

The results should appear in vim when the search is complete.

Or

Try:

find /<path> -name > results.txt
vim results.txt 

If you don't mind running the command again: press Up and append an xargs command. Or use history substitution and run

!! | xargs vim          # won't work with file names containing \'" or whitespace
!! | xargs -d \\n vim   # GNU only (Linux, Cygwin)

There's a lightweight way of saving the output of a command that works in ksh and zsh but not in bash (it requires the output side of a pipeline to be executed in the parent shell). Pipe the command into the function K (zsh definition below), which keeps its output in the variable $K.

function K {
    K=("${(@f)$(tee /dev/fd/3)}") 3>&1;
}
find … |K
vim $K

Automatically saving the output of each command is not really possible with the shell alone, you need to run the command in an emulated terminal. You can do it by running inside script (a BSD utility, but available on most unices including Linux and Solaris), which saves all output of your session through a file (there's still a bit of effort needed to reliably detect the last prompt in the typescript).

Tags:

Linux

Vim

Bash

Find