4

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?

  • 1
    You can give full path.it should work :) – SibiCoder Jun 23 '16 at 4:01
  • If someone is looking for a convenient way to open files in vim's '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. – Hotschke Jun 27 '16 at 9:23
5

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.


Workaround 1: use :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.


Workaround 2: script it

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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