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

10,089

Solution 1

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

Solution 2

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.

Share:
10,089

Related videos on Youtube

Jonas
Author by

Jonas

I'm a Computer Science student.

Updated on September 18, 2022

Comments

  • Jonas
    Jonas over 1 year

    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
    Jonas over 12 years
    No, that file is also place in C:\Windows\system32 and not in ~\myprojects\testproject\todo.txt.
  • ReyCharles
    ReyCharles over 12 years
    Sorry, I misread your question. I will come back with a proper answer soon.
  • ReyCharles
    ReyCharles over 12 years
    How about now? Does this answer your question?