Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: ~ symbol not treated as home when starting from CLI/terminal #23901

Closed
devindang opened this issue Jun 4, 2023 · 16 comments · Fixed by #28515
Closed

Windows: ~ symbol not treated as home when starting from CLI/terminal #23901

devindang opened this issue Jun 4, 2023 · 16 comments · Fixed by #28515
Labels
bug issues reporting wrong behavior platform:windows

Comments

@devindang
Copy link

Problem

any path including "~" cannot be operated correctly.

nvim ~/.vimrc

This doesn't work, and it will create a new file.

cd ~
nvim .vimrc

This get worked.

Steps to reproduce

just install neovim
and execute nvim ~/.vimrc

Expected behavior

It should edit the .vimrc file instead of creating a new one.

Neovim version (nvim -v)

0.9.1

Vim (not Nvim) behaves the same?

no, vim 9.0

Operating system/version

Windows 11

Terminal name/version

windows terminal 1.16.10262

$TERM environment variable

powershell

Installation

scoop

@devindang devindang added the bug issues reporting wrong behavior label Jun 4, 2023
@lewis6991
Copy link
Member

Your shell should be expanding ~, I don't think neovim should expand internally.

@devindang
Copy link
Author

Your shell should be expanding ~, I don't think neovim should expand internally.

Is that expanded by the shell? But vim could process with it.

@lewis6991
Copy link
Member

I'm pretty sure neovim should process the argument names verbatim on the command line.

@devindang
Copy link
Author

I'm pretty sure neovim should process the argument names verbatim on the command line.

Yes, it didn't happen on WSL.

@justinmk
Copy link
Member

justinmk commented Jun 4, 2023

vim special-cases this on Windows. That was removed in Nvim. You can force the special-casing by using the :next command:

nvim +"n ~/.vimrc"

@justinmk justinmk changed the title My neovim for windows cannot regard ~ symbol as home Windows: ~ symbol not treated as home when starting from CLI/terminal Jun 4, 2023
@devindang
Copy link
Author

vim special-cases this on Windows. That was removed in Nvim. You can force the special-casing by using the :next command:

nvim +"n ~/.vimrc"

Yes, it does get worked, but if nvim could treat this special case in windows, it will be better :)

@justinmk
Copy link
Member

justinmk commented Jun 4, 2023

Does any other tool work like that on Windows?

@pjschinske
Copy link

Does any other tool work like that on Windows?

Powershell supports tilde expansion for built-in commands, and for native applications if you enable an experimental feature. But third party applications presumably have to implement it on their own.
See https://github.com/orgs/PowerShell/discussions/15263

@devindang
Copy link
Author

Does any other tool work like that on Windows?

I didn't find yet. A similar situation occurs with wildcard symbol * in powershell :)

@3N4N
Copy link
Contributor

3N4N commented Jun 5, 2023

I think "it would be better" is unarguable. But as lewis said, the responsibility of expanding symbols falls on the shell. And ~ is not a valid symbol in Windows lexicon anyway. Try nvim %userprofile%\.vimrc from command prompt. It will expand correctly. That's on (the responsibility, I mean) command prompt. If we wish to use sane things like ~ in Windows, then instead of patching all apps individually, we should patch the shell. Isn't PowerShell open source now? We should tell them to expand ~ before passing them to app arguments, like normal shells.

@daviewales
Copy link

In PowerShell <= 7.3, the problem of tilde expansion is somewhat mitigated by tab expansion which automatically replaces the tilde with the absolute path to your home directory when you press Tab.

nvim ~/Des<TAB> -->
nvim C:\Users\example\Desktop\

In PowerShell Core (7.4 in my case), Tab expansion autocompletes directories, but retains the tilde, and passes it unchanged in arguments.

nvim ~/Des<TAB> -->
nvim ~\Desktop\

See related PowerShell issue: PowerShell/PowerShell#20750.

There is an experimental feature in the pre-release version of PowerShell 7.5 which performs ~ expansion at the shell level if enabled, but it's not enabled by default, and 7.5 is not yet released.

It would be nice for Neovim to special-case ~ expansion on Windows at least until ~ expansion lands as a default feature in PowerShell.

@justinmk
Copy link
Member

justinmk commented Apr 3, 2024

Thanks for that info. If powershell is gaining this feature then we definitely don't need to special case it.

@jadczak
Copy link

jadczak commented Apr 16, 2024

Does any other tool work like that on Windows?

For what it's worth (and apologies for being late to the party)

sublime handles the tilde expansion.
So I can type the following to open my init.lua in powershell 7.4.2
subl ~/AppData/Local/nvim/init.lua

However python does not handle the tilde expansion, so trying to run the following:
python ~/foo.py
will result in No such file or directory as python will interpret this as a relative path.

And the direction of the slash (\ vs /) doesn't impact the results. I guess powershell already manages that.

@justinmk
Copy link
Member

And the direction of the slash (\ vs /) doesn't impact the results.

It matters almost nowhere, except in a few specific, old console (cmd.exe) commands.

rkitover added a commit to rkitover/neovim that referenced this issue Apr 26, 2024
In fix_fname() for MS-Windows, expand "~\" or "~/" prefixed paths to the
USERPROFILE environment variable for the user's profile directory
instead of a relative path to a literal "~" directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
@rkitover
Copy link
Contributor

I started working on a PR for this.

I will put my notes from it here as well.

PowerShell on Windows recently removed the feature where arguments such as ~/.ssh/config or ~/.vimrc are expanded to ${env:USERPROFILE}/.ssh/config etc..

What this means is, in PowerShell, when you run a command such as:

nvim ~/.ssh/config

, what happens is that nvim tries to open ${PWD}/~/.ssh/config because that looks like a relative path to a literal ~ directory to it.

In these cases the user is very unlikely to want to open a file in literal ~ relative directory. It's a huge annoyance for me in PowerShell because I run these commands frequently and now I have to run a command such as:

nvim (gi ~/.ssh/config)

, instead to get the file I want.

There used to be an experimental feature for this in get-experimentalfeature that you could enable, I don't remember which exactly, but it has been removed.

In the unreleased development version of PowerShell, which may not be released for quite a while yet, there is an experimental feature for this, PSNativeWindowsTildeExpansion, see here.

I believe it would be better if we address this in neovim, because this PowerShell experimental feature will not be available for a while yet, and when it is it would require enabling it, and if this is done in neovim it would also work in WinPS (powershell.exe) and cmd.exe.

I should also mention that the PowerShell completion behavior for tilde paths has also recently changed. Before, when you would type:

nvim ~/.ssh/config<TAB>

, it would expand to the absolute path, but PowerShell no longer expands tilde paths to absolute paths, it expands them to canonical tilde paths, leading to the behavior in neovim that I described.

I tried to do this in os_open() in os/fs.c first, but it is too late to do it there because fix_fname() has already converted the path to an absolute path to a relative ~ directory, this is why I put it there.

@T-727
Copy link
Contributor

T-727 commented Apr 26, 2024

Note that as of 2 days ago, ~ is expanded for native commands PowerShell/PowerShell#21529.
And that is also considered for backport to the current release of powershell.

rkitover added a commit to rkitover/neovim that referenced this issue May 20, 2024
In fix_fname() for MS-Windows, expand "~\" or "~/" prefixed paths to the
USERPROFILE environment variable for the user's profile directory
instead of a relative path to a literal "~" directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 23, 2024
In command_line_scan() for WIN32, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 23, 2024
In command_line_scan() for WIN32, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 23, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 24, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 25, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 25, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
rkitover added a commit to rkitover/neovim that referenced this issue May 25, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
zeertzjq pushed a commit that referenced this issue May 25, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix #23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
huangyingw pushed a commit to huangyingw/neovim that referenced this issue May 31, 2024
In command_line_scan() for MSWIN, expand "~\" or "~/" prefixed paths to
the USERPROFILE environment variable for the user's profile directory.

Fix neovim#23901

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior platform:windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants