9

I want to open another file in the same directory or any file with its path relative to the current directory in command line.

My path is /home/sibich/

 /home/sibich> vim a.pl

In vim, I want to open b.pl in same directory, so I use :

:vim b.pl

Bit I receive this message Invalid pattern or filename

So, I had to give it in shell.

  :!vim b.pl

I want to directly execute this in vim.

Example 2: sub is a folder under /home/sibich

  :vim sub/c.pl

Is there a way to set options such that command line accepts path relative to current directory and allows opening files through split, tabnew and vim commands?

4
  • 6
    :vim is short for :vimgrep. Is there a reason you aren't using :e?
    – Tommy A
    Commented May 6, 2016 at 13:13
  • 2
    This is the most basic usage of vim, have you used vimtutor or used google to find a solution?
    – statox
    Commented May 6, 2016 at 13:52
  • 2
    @statox: I have been using vim for over a year without knowing anything about it apart from i, dd and wq!. Only for past few weeks, I am learning it through various resources. I have read vimtutor too and didnt notice this. :/ Before asking a question here, I usually Google it and then search in stackoverflow too. I am a beginner :)
    – SibiCoder
    Commented May 6, 2016 at 17:26
  • 4
    @SibiCoder: I really don't consider myself as an expert and I do understand that as beginners we sometimes have questions which might be trivial. I was just pointing out that this particular question is answered is several different places. I would never want to discourage someone from asking for help: it's the purpose of this community. It's just that on this particular topic a Google research would have provided you an answer and avoided several downvotes :-)
    – statox
    Commented May 6, 2016 at 17:31

2 Answers 2

21

Have you tried the edit command?

:edit b.pl

Edit: Not sure if you edited in the last question, or I just missed it the first time. But the only reason you wouldn't be able to use relative paths on :split or :tabnew is if your current working directory isn't the same as the file you're currently editing. So I think what you're looking for is

:set autochdir

This option basically makes your current working directory "follow" you whenever you change buffers. With that options set, you should be able to use relative paths. See :h autochdir for more info.

2
  • 1
    Thanks. //current working directory isn't the same as the file editing.// I have mistaken the path of the first file to be its current directory. That is, whenever I give the vim command from shell, I believed that the file's directory is the current directory.
    – SibiCoder
    Commented May 6, 2016 at 17:30
  • 3
    Autochdir is the thing I wanted.
    – SibiCoder
    Commented May 6, 2016 at 17:30
20

Use :e %:h<filename>, or specifically for your question :e %:hb.pl. Individually these tokens mean

:e edit, but you could use :tabnew or :split etc

% the current file path

:h 'head', which in this usage is the directory of the currently open file

<filename> the relative path of the file you want to open

You can also hit tab after typing :h for a list of filenames at that path.

For further reading, execute :help expand() in vi(m) for more about token expansion.

2
  • 1
    Thanks, this :h - tab is very helpful. but how do I browse the current directory(after :set autochdir) without making another split vim window? I mean I want to browse the directory in the current wim window (of course the current file shouldn't have any change). ==> Ah, it's just :Explore or :Ex in short. Hope this helps to anyone else.
    – Chan Kim
    Commented Nov 28, 2019 at 12:20
  • gf but relative path of the file nnoremap <Leader>gf :hide :edit %:h/<cfile><CR>
    – CervEd
    Commented May 9, 2023 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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