When I use :read <filename>
in Vim, does it only look in the current working directory?
Is there a way to set up vim to have the :read
read reference multiple directories when attempting to open a particular file?
You cannot configure the path that :read
will use. It will always start from the current directory. :read
is not affected by the path
variable in vim (see :h path
). Although, see the workaround sections below.
Note that once you input :read
into the prompt you can use tab for file and directory completion, including going up (with ..
) and down the directory tree.
:find
:find
will open the file if it is present in any directory present in the path
variable. If you set the path
as:
:set path=.,/home/me/myfiles
Then when you perform:
:find myfile
And myfile
is not present in the working directory, vim will look for /home/me/myfiles/myfile
and open it instead (if it exists). You can have as many paths, separated by commas (,
), in the path
variable as needed.
:h path
is very useful.
If you need the full functionality of :read
(e.g. to add the contents of a file to another file), you can use the fact that :read
can use the standard output of a command:
:read !<command>
Therefore you can write a script such as (I'm using bash
here for brevity):
#!/bin/sh
cd /home/me/myfiles
cat $1
Let's say that the script is in the executable path (PATH
on *nix, %PATH%
on windows) and is called myread.sh
, then you can perform:
:r !myread.sh myfile
To open /home/me/myfiles/myfile
from any working directory.
'runtimepath'
directories, there is the plugin github.com/tpope/vim-scriptease which provides:Vedit
for that. E.g.:Vedit plugin/scriptease.vim
to open the actual plugins file. Otherwise this question sounds like the use case of:find
.