How to get directory of current file.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get directory of current file.

DwigtArmyOfChampions
I am using gVim on Windows. I open a file in C:\tmp called file1.cpp. After looking around in file1.cpp I now want to open file2.cpp which is also in C:\tmp. So I run the command

:e .\file2.cpp

But this doesn’t work because the “.” is C:\Windows\system32. Is there a different character or function or variable I can use in command mode to designate “the directory that the current file is in”?

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/3d20816b-22ec-4437-9a29-de72d9484674%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

RE: How to get directory of current file.

Wood, Geoff (Refinitiv)
> -----Original Message-----
> From: [hidden email] <[hidden email]> On Behalf Of DwigtArmyOfChampions
> Sent: 17 April 2020 15:47
> To: vim_use <[hidden email]>
> Subject: How to get directory of current file.
>
> I am using gVim on Windows. I open a file in C:\tmp called file1.cpp. After looking around in file1.cpp I now want to open file2.cpp which is also in C:\tmp. So I run the command
>
> :e .\file2.cpp
>
> But this doesn’t work because the “.” is C:\Windows\system32. Is there a different character or function or variable I can use in command mode to designate “the directory that the current file is in”?

This seems to cover what you want:

https://vim.fandom.com/wiki/Easy_edit_of_files_in_the_same_directory

Personally I have a key mapped to change to the current file's directory:

map <F7> :cd %:p:h<CR>:pwd<CR>

based on this:

https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file

regards,
Geoff

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/DM6PR06MB514675DDB5BE12A391DF8968FFD90%40DM6PR06MB5146.namprd06.prod.outlook.com.
Reply | Threaded
Open this post in threaded view
|

Re: How to get directory of current file.

Gary Johnson-4
In reply to this post by DwigtArmyOfChampions
On 2020-04-17, DwigtArmyOfChampions wrote:

> I am using gVim on Windows. I open a file in C:\tmp called
> file1.cpp. After looking around in file1.cpp I now want to open
> file2.cpp which is also in C:\tmp. So I run the command
>
> :e .\file2.cpp
>
> But this doesn’t work because the “.” is C:\Windows\system32. Is
> there a different character or function or variable I can use in
> command mode to designate “the directory that the current file is
> in”?

One way:

    :e %:h/file2.cpp

%:h will expand to the parent directory of the current file.

Another way:

    :e ^R%

where ^R is Ctrl-R, will put the current file name on the command
line where you can edit it to the file name you want.

See

    :help filename-modifiers
    :help c_CTRL-R

Regards,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200417152247.GA22179%40phoenix.
Reply | Threaded
Open this post in threaded view
|

Re: How to get directory of current file.

Tim Chase
In reply to this post by DwigtArmyOfChampions
On 2020-04-17 07:47, DwigtArmyOfChampions wrote:

> I am using gVim on Windows. I open a file in C:\tmp called
> file1.cpp. After looking around in file1.cpp I now want to open
> file2.cpp which is also in C:\tmp. So I run the command
>
> :e .\file2.cpp
>
> But this doesn’t work because the “.” is C:\Windows\system32. Is
> there a different character or function or variable I can use in
> command mode to designate “the directory that the current file is
> in”?

There are two main ways to do this

1) set 'autochdir'.  This will automatically change directory to the
directory of the containing file so that "./" will always refer to
the directory of the current file.

2) use expand() or fnamemodify() with the various available modifiers
(":help filename-modifiers").  In this case you want either :p for the
absolute path or :~ for the path relative to your home directory.

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200417104100.55816e0d%40bigbox.attlocal.net.