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

19,869

Solution 1

You can use command substitution:

vim $(find -name somefile.txt)

or

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

Solution 2

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 

Solution 3

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

Solution 4

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 `!!`

Solution 5

As @davidmh pointed in the comments, you can use xargs with the flag -o if you want to use an interactive application.

find . -name somefile.txt | xargs -o vim

If you do not use -o, you will probably mess up the terminal state after leaving vim. If you have done it, you should be able to reset your terminal with the following command:

reset
Share:
19,869

Related videos on Youtube

anjum
Author by

anjum

Updated on September 17, 2022

Comments

  • anjum
    anjum over 1 year

    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

  • Chris Perkins
    Chris Perkins over 13 years
    This will try to open all the 'somefile.txt'
  • CurtainDog
    CurtainDog over 13 years
    that's what is required. We need to open the file found for editing.
  • Chris Perkins
    Chris Perkins over 13 years
    ok. If that is what is needed, then find -name somefile.txt | xargs vim
  • CurtainDog
    CurtainDog over 13 years
    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.
  • Luc Hermitte
    Luc Hermitte over 13 years
    Vim emits an alert, but it still works in the end.
  • Michael Mrozek
    Michael Mrozek over 12 years
    An anonymous user notes that this will fail if the filename has spaces
  • sente
    sente over 12 years
    vim $(find -name somefile.txt) works but I'm always annoyed to have to do this, especially when the command inside the $(...) is usually the last command you entered and I just want to pipe it to vim. pvim does that - unix.stackexchange.com/a/30208/14810
  • Bernhard
    Bernhard over 12 years
    Another alternative would be vim `find -name somefile.txt`. @StuartPowers Alternatively, you can use vim `!-1` if you already used this find was you previous command
  • davidmh
    davidmh over 10 years
    @balki to use xargs in this scenario, you have to use the flag -o, it's in the man: -o Reopen stdin as /dev/tty in the child process before executing the command. This is useful if you want xargs to run an interactive application.
  • user197292
    user197292 over 6 years
    @dogbane $ find /etc/update-motd.d -type f -exec vim {} < /dev/null \; stackoverflow.com/a/13555918
  • durum
    durum over 6 years
    Note that the second option opens one vim instance for every single file. This causes can cause plenty of inconveniences, for example, you may ending up closing vim several dozens of times.
  • Player1
    Player1 almost 4 years
    thank you! it works!
  • FantomX1
    FantomX1 about 3 years
    To adjust the result supplied, you might wish to adjust it with cut ofc echo 'someletters_12345_moreleters.ext' | cut -d'_' -f 2 - stackoverflow.com/questions/428109/extract-substring-in-bash‌​/…