Vscode: Option to specify separator for "Copy Relative Path" action

Created on 14 Aug 2018  路  48Comments  路  Source: microsoft/vscode

Workaround (from @bpasero)
You can use this extension: https://marketplace.visualstudio.com/items?itemName=rssowl.copy-relative-path-posix

Original Issue:

Looked for other mentions but haven't found related requests or discussion.


I'm often using Copy Relative Path feature to compose import strings or share with other developers on Mac/Linux. Therefore, new Copy relative path feature that has native path separators on Windows doesn't help in these cases (in comparison with Copy path).

Please add option to change path separators used in Copy Relative Path action, so I will be able to set / as path separator and have same behavior across all platforms.

feature-request file-explorer

Most helpful comment

@isidorn proposed option will open more use cases for Windows users (other have them "for free").
JS/TS imports expect path with forward slashes independently from OS, as well as paths for images/resources used in HTML/CSS. Ability to copy path with expected separator will remove pain from manually changing them (which should be done with Copy Path).
_UDP._ Also users who prefers CLI to work with git and uses git-bash instead of CMD will benefit from this too.

I've proposed to make it as option, because it is already released in this way and separator change might be considered breaking change. Moreover this will provide same behavior as in WebStorm: relative path - always forward slashes, (full) path - according to OS.

All 48 comments

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

Both copy path and copy relative path respect the platform they are on
I can leave this open as a feature request. Add a setting just for this sounds like a bad idea to me

fyi @bpasero

@isidorn proposed option will open more use cases for Windows users (other have them "for free").
JS/TS imports expect path with forward slashes independently from OS, as well as paths for images/resources used in HTML/CSS. Ability to copy path with expected separator will remove pain from manually changing them (which should be done with Copy Path).
_UDP._ Also users who prefers CLI to work with git and uses git-bash instead of CMD will benefit from this too.

I've proposed to make it as option, because it is already released in this way and separator change might be considered breaking change. Moreover this will provide same behavior as in WebStorm: relative path - always forward slashes, (full) path - according to OS.

6308

There was a closed issue with similar request, but was rejected by the arrogant developer.

This extension had problems earlier, but copies now, however doesn't play nicely with workspaces and 2 "Copy relative path" options in dropdown aren't good.

Still unified behavior across OSes and IDE would be great for built-in functionality would be great.

@IllusionMH agree, it's not perfect. I would suggest a new setting "preferedPathSepartor" with possible values "Native", "Forward", "Backward".

I'd so love an option to set POSIX (forward slash) separators everywhere in vscode on windows. I hate manually replacing\escaping slashes everywhere. I'm not using cmd.exe, windows explorer accepts POSIX paths, so why do we need backslashes at all?

Please add an option to display POSIX all the way.

I often use the vscode Windows Subsystem for Linux (WSL) terminal to navigate to a file by right clicking a file in VS code and hitting the Copy path options. This will deliver me a windows path ( C:\Users. ).
As i'm using WSL I want the possibility to copy an Unix path. The path would then be: /c/Users. ( https://github.com/Microsoft/vscode/issues/63633 )

+1 for making the slash direction optional to the user. I do a lot of work with Markdown file on Windows in VSCode for Azure Devops Wiki and relative path file links need to use forward slashes and not backslashes, so I am constantly using the "Copy Relative Path" and replacing the backslashes

This extension does something like that: https://github.com/CoenraadS/Copy-WSL-Path

@victor-ruben unfortunately this extension doesn't help. It will produce full path like /mnt/c/long/path/to/project/src/file/project.js (see) but in this issue we are asking for path relative for workspace. So it will be simple src/file/project.js.

What's the status on this ? It would be really nice to have options for relative file path copy, same way as you do for the window title for example. Given this is already implemented there, it should be quite trivial

I would like to contribute it if possible, just not sure where to get started

+1 for this. Makes total sense for me.

I would like to contribute it if possible, just not sure where to get started

Perhaps a PR to this extension https://github.com/CoenraadS/Copy-WSL-Path

+1 for this

I would suggest a new setting "preferedPathSepartor" with possible values "Native", "Forward", "Backward".

esmirnov proposes the perfect solution in my eyes. I recently started using VS code for angularJS development and the copy relative path function would be fantastic for pasting template links into directives and js links into index.html, but backslash is an escape character in JS. It's super annoying to have this great and useful feature have such a debilitating limitation, which is how I would describe the situation as pertains to use in JS.

Can there just be an added context menu option that one can turn on, such as "Copy Relative Path with /"? I would find this very useful as even being a Windows user, I use the forward slash path more often than the backslash one.

I use _vscode_ _because_ it is portable.

  • Native Windows APIs accept forward slashes or back slashes.
  • Relative paths in all my code therefore use forward slashes. Some languages mandate this.
  • When on Windows I use _bash_ as my shell (_vscode_ lets you do this).
  • Just about the only place where you must use backslashes is in _cmd.exe_ as far as I can tell.

I _never_ use _cmd_.

Please _please_ can Windows _vscode_ support some way of getting paths with forward slashes?

An obvious feature, should definitely exist for WSL users who want to work with Linux users and not look like idiots using Windows slashes :+1:

I'd like to chime in and say I use Cygwin through Mintty and the backslashes are really killing my productivity due to them being forward slashes, and not being able to configure a full path output. =|

I'm not sure why there's no way of overriding this, the times I need backslashes are really rare.

This is a necessary feature and huge productivity booster for those of us who use Linux shells in Windows. Could we please add this as a configuration option with the default set to the native format but at least have the ability to override it?

This is a functionality that is very much needed, I work on windows but everything is mounted on Linux servers, it is a nuisance to be changing the bars.

I think it's OK to leave the option to the user. It's really needed.

The ability to choose a separator is something that is so lacking.

Or don't specify an option and always return a path with "/"

Especially now when WSL is a thing this is important. Even without WSL there are shells that use the Linux format - git bash for example. Switching to stock cmd just because I need to paste a VS Code path is a pain.

I just released an extension that should take care of this (as I still think this should be something an extension provides and not be a setting): https://marketplace.visualstudio.com/items?itemName=rssowl.copy-relative-path-posix

image

Features:

  • supports multi-select in explorer
  • supports editor title context menu
  • will not add the name of the workspace folder if in a multi-folder setup
  • has only minimal dependencies (e.g. uses the official API to copy to clipboard)

@IllusionMH If there are no objections and people think the extension is OK enough, I would suggest to close this issue.

Thanks for the extension, but I think there's quite enough argument above that this should be built-in. Whether it's a setting, or a separate menu entry like in the extension, or simply always copy the posix slash (which works everywhere except cmd as was pointed out earlier), I don't know, but it's not something to be left to an extension IMO.

I think it should be reversed, the default should be standard POSIX and the cmd compatibility should be an extension.

I pushed an update for the extension so that it can also be used via keybinding or from the command palette. In that case of usage it will copy the relative path of the active editor.

@bpasero Thanks for this, I'm going to use it until there is an alternative.

This really should be a standard feature, guess the developers don't actually develop on Windows platforms much.

Few developers are going to use cmd.exe as a terminal, it'll usually be bash, often as not people will be developing for non-windows environments. Having the copy path options forcibly use a type of slash that's wrong in the majority of usage cases is daft and makes the feature next to useless. It really does need to be settings-based or have two completely separate options. May well want to be per language, as desktop applications in an MS flavour might still want the backslash.

Extensions are nice but please add it as a native setting so it can be set on a per-folder or per-workspace basis in settings.json. This is especially needed for developing Angular apps. It's a reasonable request with good support and no real downsides. It can default to false to keep current behavior but something like the following would be very helpful. Thank you.
"editor.copyRelativePathForwardSlashes": true

It can default to false to keep current behavior but something like the following would be very helpful. Thank you.
"editor.copyRelativePathForwardSlashes": true

I'm thinking that "editor.copyRelativePathSlashType" would be a better option that could have "forward", "back", or "os" (default). That way if someone wanted backslashes over forward for whatever reason they have the option.

Another upvote. Especially with Windows Subsystem for Linux 2 incoming, more and more people are using Windows combined with linux workflows. I personally use git bash or an Ubuntu terminal for day-to-day work.

In addition, many development projects use forward slashes for path definitions that are passed to configs, command lines or API's. Not having this as a VSCode option feels very backwards and is a common pain point.

Even windows explorer accepts forward slashes in the address bar.

Please build this in as a setting into VS Code. I am unable to _Copy Relative Path_ properly into my Bash terminal, since it reads "\" as the escape character

Quick poll: what if there was simply a setting that puts any path on Windows with slash instead of backslash when shown in the UI or copied? Changing this just for the "Copy Path" action seems a bit inconsistent. Would that be OK for people?

For me that's definitely desirable (more than OK). Again, the only thing I'm aware of which still requires backslash is cmd, everything else may accept it but also accepts slash, so as I see it there's no real reason to be using backslash.

(And if you're a developer and you're using cmd, please do yourself a favor and take a couple of days to learn PowerShell, it's 2020 already.)

@bpasero I'm pretty sure that's actually what everyone wants, but they settled for "path". Bring it on! :)

@bpasero overall it will be even more preferable especially if implemented for Compact folders in Explorer and search results and open editors description text (relative path).

Not so optimal for full paths in hovers (tabs, open editors, explorer), but not a problem.

But problem will be reversed to "Copy path" which copies full path and OS separators make more sense here.
(Personally I rarely use it, so won't be a big problem for me)

TL;DR; I'm all in for this option, but proper OS path for Copy path makes more sense

Maybe "Copy Path" could be the only special case where we would use backslash even if this new setting is configured to use slashes. I agree that an absolute path when copied should use backslash on Windows.

I need 2 options for "Copy relative path":
1) for Windows, where I work with IDE
2) for Unix, which is the target for my PHP files
The solution with native function with OS separator + plugin suites for me.
If there will be an option for separator, it doesn't solve the problem dealing with 2 OS file systems :)

@sergey-beloglazov if you want to retain platform-specific conventions then simply don't change the (future) setting. Unix is already POSIX, windows will keep backslashes.

This issue is about support of overriding the convention for all platforms.

Also would be great if you fix class discovery. That way we wouldn't need to go copy (type in this case) the path to the file that we wanna import in our .ts

yes also need it.
Maybe the alt + click on the command can be a good idea!, or shift + rightClick to make appear secret commands like in Window explorer?
A option to toggle between path mode can also a great idea.
We lost precious time to convert path slash \ / and a plugin to just do this ! no thanks

Was this page helpful?
0 / 5 - 0 ratings