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 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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use command substitution:
vim $(find -name somefile.txt)
or
find -name somefile.txt -exec vim {} ;
Method 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
Method 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).
Method 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 `!!`
Method 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
Method 6
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 🙂
Method 7
To open all files found in individual tabs, you can:
vim -p $(find -name somefile.txt)
or
find -name somefile.txt -exec vim -p {} +;
Method 8
The solution is to use xargs -o vim
I added the following to my .bash_aliases
# .bash_aliases ### Example Usage: "ls | sort -r | pipe-vim" alias pipe-vim="xargs -o vim"
Method 9
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.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a2a5a491a2b4bfa5b4">[email protected]</a> ~ $ function pvim() { read -r somefile; exec < /dev/fd/1; vim "$somefile"; }
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcafa8a99cafb9b2a8b9">[email protected]</a> ~ $ type pvim
pvim is a function
pvim ()
{
read -r somefile;
exec < /dev/fd/1;
vi "$somefile"
}
Use it like:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="146760615467717a6071">[email protected]</a> ~ $ 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")
Method 10
Here is a simpler xargs solution than those already mentioned:
find -name somefile.txt | xargs vim -p
The -p option tells vim to open each file in a separate tab. I find this more convenient than using buffers.
Method 11
I found that opening a file with the help of locate in vim is extremely convenient and much faster than find.
The following command searches a file called app.py in the path containing the directory MyPythonProject.
:args `locate '*MyPythonProject*app.py'`
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0