0

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?

  • 5
    :vim is short for :vimgrep. Is there a reason you aren't using :e? – Tommy A May 6 '16 at 13:13
  • 2
    This is the most basic usage of vim, have you used vimtutor or used google to find a solution? – statox May 6 '16 at 13:52
  • 1
    @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 May 6 '16 at 17:26
  • 3
    @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 May 6 '16 at 17:31
8

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.

  • 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 May 6 '16 at 17:30
  • 1
    Autochdir is the thing I wanted. – SibiCoder May 6 '16 at 17:30
8

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.

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.