Open a file given by the result of a command in vim
Solution 1
You can use command substitution:
vim $(find -name somefile.txt)
or
find -name somefile.txt -exec vim {} \;
Solution 2
Try this:
- start vim
- 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
Related videos on Youtube
anjum
Updated on September 17, 2022Comments
-
anjum over 1 year
I find myself doing the following almost every day
- Run a find (
find -name somefile.txt
) - Open the result in
vim
The problem is I have to copy and paste the result of the
find
into thevim
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
- Run a find (
-
Chris Perkins over 13 yearsThis will try to open all the 'somefile.txt'
-
CurtainDog over 13 yearsthat's what is required. We need to open the file found for editing.
-
Chris Perkins over 13 yearsok. If that is what is needed, then find -name somefile.txt | xargs vim
-
CurtainDog over 13 yearsNo, 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 over 13 yearsVim emits an alert, but it still works in the end.
-
Michael Mrozek over 12 yearsAn anonymous user notes that this will fail if the filename has spaces
-
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 over 12 yearsAnother alternative would be
vim `find -name somefile.txt`
. @StuartPowers Alternatively, you can usevim `!-1`
if you already used this find was you previous command -
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 over 6 years@dogbane $ find /etc/update-motd.d -type f -exec vim {} < /dev/null \; stackoverflow.com/a/13555918
-
durum over 6 yearsNote 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 almost 4 yearsthank you! it works!
-
FantomX1 about 3 yearsTo 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/…