Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I find myself doing the following almost every day

  1. Run a find (find -name somefile.txt)
  2. Open the result in vim

The problem is I have to copy and paste the result of the find into the vim command. Is there any way to avoid having to do this? I have experimented a bit (find -name somefile.txt | vim) but haven't found anything that works.

Thanks in advance

share|improve this question

7 Answers 7

up vote 8 down vote accepted

You can use command substitution:

vim $(find -name somefile.txt)

or

find -name somefile.txt -exec vim {} \;
share|improve this answer
    
This will try to open all the 'somefile.txt' –  balki Jan 14 '11 at 17:31
    
that's what is required. We need to open the file found for editing. –  dogbane Jan 14 '11 at 17:31
1  
ok. If that is what is needed, then find -name somefile.txt | xargs vim –  balki Jan 14 '11 at 17:37
    
No, that doesn't work properly and might even mess up your terminal! You can't pipe to xargs vim, because vim expects input to come from an interactive terminal. –  dogbane Jan 14 '11 at 17:43
    
Vim emits an alert, but it still works in the end. –  Luc Hermitte Jan 17 '11 at 14:39

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).

share|improve this answer

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 
share|improve this answer
    
My solution does exactly the same but why is it down voted? –  balki Jan 15 '11 at 12:51

I like to to use the back ticks ` (Its on the same key as the ~)

> vim `find . -name somefile.txt`

The back ticks executes the command inside the ticks and the output can then be used by the command. The above will find all files somefile.txt thus allowing you to use :next to move through all the files.

Its very usefull if you spend a couple of tries refining the command, because you can then use history substitution to repeat the command for the editor.

> find . -name somefile.txt
./debug/somefile.txt
./somefile.txt
> vi `!!`
share|improve this answer

I must have missed the point here, because I would use:

:find **/somefile.txt

(if I knew there was only one in my :echo &path ) or, if I wanted them all:

:args **/somefile.txt

Edit: Whoa! Ok, so I did miss the point - you want the find list, not the actual files opened? Try (again, from within Vim):

!!find . -name somefile.txt

-- my bad :)

share|improve this answer

I wrote a function pvim which does what you want. If multiple lines are piped to pvim it ignores all but the first, it could be extended though to open multiple files in vim.

stu@sente ~ $ function pvim() { read -r somefile; exec < /dev/fd/1; vim "$somefile"; }
stu@sente ~ $ type pvim
pvim is a function
pvim ()
{
    read -r somefile;
    exec < /dev/fd/1;
    vi "$somefile"
}

Use it like:

stu@sente ~ $ find . -name "foo.txt" | pvim

which will load the first "foo.txt" into vim just as if you had typed:

vim $(find . -name "foo.txt | head -n1")

share|improve this answer
find -name somefile.txt | vim -

Most of the commands can take -

as a file name which means stdin.

share|improve this answer
1  
-1 This doesn't work. It should open up the file for editing, not open up stdin. –  dogbane Jan 14 '11 at 17:31
    
You can't save the file (unless you know the original file name). –  Loki Astari Jan 14 '11 at 20:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.