How to open a new file in the current directory with gvim on Windows?

3

2

If I'm working on a file located on e.g. ~\myprojects\testproject\hello.txt and then want to open a new file in the same directory e.g. todo.txt (located on ~\myprojects\testproject\todo.txt), how can I do that efficiently with gvim on Windows?

Today I have to type this command, maybe using tab-completion for the paths:

:e ~\myprojects\testproject\todo.txt

Is there any shorter command e.g:

:e .\todo.txt

in gvim on Windows?

If I use .\todo.txt, that file will be located on C:\Windows\system32, that's not even my home directory. Is there any setting to specify my home directory as default instead? or any other diectory?

Jonas

Posted 2011-12-08T11:52:27.827

Reputation: 21 007

Answers

6

The following should work, provided your current dir is where the file is:

:e todo.txt

For the second question I don't know a good answer. You can of course do

:cd ~

every time you start up, or you could edit your vimrc to do that. It's a hack and not a good solution.

There's also 'autochdir' if you want to cd to the file every time. I found it here: http://vim.wikia.com/wiki/Set_working_directory_to_the_current_file

In your vimrc you would put in this line:

set autochdir

Edit2: add answer to second question

ReyCharles

Posted 2011-12-08T11:52:27.827

Reputation: 891

No, that file is also place in C:\Windows\system32 and not in ~\myprojects\testproject\todo.txt. – Jonas – 2011-12-08T11:58:27.830

Sorry, I misread your question. I will come back with a proper answer soon. – ReyCharles – 2011-12-08T12:01:33.317

How about now? Does this answer your question? – ReyCharles – 2011-12-08T12:10:20.237

3

I have the following in my vimrc:

map <Leader>e :e <C-R>=expand("%:p:h") . "/" <CR>

This opens the command line, enters the 'e' command and pre-populates the file argument with the path to the file in the currently active buffer. The key is mapped to the backslash key by default.

tonyk

Posted 2011-12-08T11:52:27.827

Reputation: 111

0

For completeness, if you are editing a file in that directory at the moment, set autochdir in vim.rc should be enough.

If you are however in the vim file explorer (netrw)

:e .
%filename

you will have to precede the creation of a new file with %.

For more information: :help netrw-intro-browse.

Anne van Rossum

Posted 2011-12-08T11:52:27.827

Reputation: 201