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

~ doesn't work in cmder? #1051

Closed
searene opened this issue Jul 28, 2016 · 14 comments
Closed

~ doesn't work in cmder? #1051

searene opened this issue Jul 28, 2016 · 14 comments
Assignees

Comments

@searene
Copy link

searene commented Jul 28, 2016

C:\Users\myusername
λ rm ~\Documents\Untitled.png
rm: `/c/Users/myusername' is a directory

I'm pretty sure Untitled.png exists, but I cannot remove it through the rm command.

@glucas
Copy link
Contributor

glucas commented Jul 28, 2016

Does it work if you don't use the ''? If this is a cmd.exe shell then I believe the shell handles the path and doesn't understand the ''.

I have Alt-~ mapped to tidle-expand for cases like this. (I believe that is a default setting -- use Alt-h from the shell to list key bindings).

@searene
Copy link
Author

searene commented Jul 29, 2016

I just tried rm C:\Users\myusername\Documents\Untitled.png, which worked.

So this is an issue of ~ instead of rm

@Stanzilla Stanzilla changed the title rm doesn't work in cmder? ~ doesn't work in cmder? Jul 29, 2016
@radavis
Copy link

radavis commented Jul 29, 2016

λ cd ~
The system cannot find the path specified.

@MartiUK
Copy link
Member

MartiUK commented Jul 29, 2016

Try using PowerShell.

@glucas
Copy link
Contributor

glucas commented Aug 18, 2016

So to clarify: Cmder does not interpret shell arguments (like '~').

If the underlying shell is cmd.exe then tilde is not supported in paths. If the underlying shell is PowerShell or Bash it will work.

If you want to use a cmd.exe shell, then one thing you can do is expand the tilde before executing the command. This is supported via tilde-expand, which is mapped to Alt-~ by default. For example:

c:\Temp
λ cd ~\bin

before hitting Enter here, you can hit Alt-~, which results in something like:

c:\Temp
λ cd c:\home\bin

Cmder provides a bunch of (configurable) key bindings like this. Try Alt-h to see what is available.

@jankatins
Copy link
Contributor

I use

local function tilde_match (text, f, l)
    if text == '~' then
        clink.add_match(clink.get_env('userprofile'))
        clink.matches_are_files()
        return true
    end
end

clink.register_match_generator(tilde_match, 1)

in <cmderroot>\config\whatever.lua to get ~<tab> to expand to my home dir.

@glucas
Copy link
Contributor

glucas commented Aug 24, 2016

@JanSchulz Ah, that's handy! Based on your example I'm using the following to handle any path that starts with tilde:

local function tilde_match (text, f, l)
    if text == '~' then
        clink.add_match(clink.get_env('home'))
        clink.matches_are_files()
        return true
    end
    if text:sub(1, 1) == '~' then
        clink.add_match(string.gsub(text, "~", clink.get_env('home'), 1))
        -- second match prevents adding a space so we can look for more matches
        clink.add_match(string.gsub(text, "~", clink.get_env('home'), 1) .. '+')
        clink.matches_are_files()
        return true
    end
end

The one funny thing is the second bogus match to prevent a space being added, since the path may not actually be complete just by replacing the ~. For example:

λ cd ~\b<TAB>
λ cd c:\Users\jdoe\b<TAB>
λ cd c:\Users\jdoe\bin\

@Beej126
Copy link

Beej126 commented Sep 17, 2017

i had to change the 3 get_env('home') to get_env('userprofile') on my basic clink install (not using cmder) but otherwise it's an age old dream come true!! thank you thank thank you

for other's coming here for clink only, just tack this (along with the register_match_generator call shown in previous comment) onto your clink.lua file in your clink install folder (mine was c:\program files (x86)\clink\0.4.8)

@elisim
Copy link

elisim commented Sep 10, 2018

I put "tilde_match" function in cmderroot\config\tilde.lua but it's not work for me.

I tried:

λ cd ~<TAB>
λ cd ~\Documents<TAB>

But nothing. I also tried to insert the function into cmderroot\vendor\clink.lua at the end, but it still not working.

@daxgames
Copy link
Member

daxgames commented Sep 15, 2018

Just tested this with latest cmder master branch build that re-enables loading user lua files and it works.

I added a %cmder_root%\config\tilde.lua with the below content:

local function tilde_match (text, f, l)
    if text == '~' then
        clink.add_match(clink.get_env('home'))
        clink.matches_are_files()
        return true
    end
    if text:sub(1, 1) == '~' then
        clink.add_match(string.gsub(text, "~", clink.get_env('home'), 1))
        -- second match prevents adding a space so we can look for more matches
        clink.add_match(string.gsub(text, "~", clink.get_env('home'), 1) .. '+')
        clink.matches_are_files()
        return true
    end
end

clink.register_match_generator(tilde_match, 1)

Started an new cmder session.

C:\Users\user\cmderdev(cexec)λ cd ~<tab>
C:\Users\user\cmderdev(cexec)λ cd C:\Users\user\<enter>

C:\Users\userλ ls ~
'3D Objects'                      dir2                   'Local Settings'                                                                                OneDrive
 acloudguru-dev-assoc-notes.txt   dir3                    MicrosoftEdgeBackups                                                                           Pictures                       Documents              'My Documents'                                                                                  Recent
 -aliases                         Downloads               NetHood                                                                                        Roaming
 AppData                          error.html             'New folder'                                                                                   'Saved Games'
'Application Data'                Favorites               NTUSER.DAT                                                                                     Searches
 cmder                           'Google Drive'           ntuser.dat.LOG1                                                                                SendTo
 cmderdev                         hellocloudgurus.py      ntuser.dat.LOG2                                                                                source
 Contacts                         index.html              NTUSER.DAT{45798cec-6166-11e8-9696-8a7b95415587}.TM.blf                                       'Start Menu'
 Cookies                          IntelGraphicsProfiles   NTUSER.DAT{45798cec-6166-11e8-9696-8a7b95415587}.TMContainer00000000000000000001.regtrans-ms   Templates
 Desktop                          Links                   NTUSER.DAT{45798cec-6166-11e8-9696-8a7b95415587}.TMContainer00000000000000000002.regtrans-ms   Videos
 dir1                             loadpage.html           ntuser.ini

C:\Users\userλ

Update: Using recent clink versions (1.1.8 and up, since 2020) this script is no longer necessary as clink now supports this natively. For more information, please refer to the link below:

chrisant996/clink#689

~DRSDavidSoft

@daxgames daxgames mentioned this issue Sep 27, 2019
3 tasks
@DRSDavidSoft
Copy link
Contributor

DRSDavidSoft commented Oct 2, 2020

Personally, I went the other way and wrote a small batch script to handle ~ in the cd command itself natively, instead of replacing it when pressing TAB.

@if "%~1"=="/?" (@cd %*)
@set excd=%*
@set excd=%excd:"=%
@if "%excd:~0,1%"=="~" (@set excd=%userprofile%\%excd:~1%)
@if not "%~1"=="/d" (@set excd_param="/d") else (@set excd_param=)
@cd %excd_param% "%excd%"

You can save this as %cmder_root%\config\excd.cmd, and then you can test it like this:
(Provided that %cmder_root%\config is in your PATH, or you call excd.cmd directly)

λ excd ~\Desktop

C:\Users\David\Desktop
λ

Now you can alias it to cd instead of using the batch script:

alias cd=excd $*

Or, if the %cmder_root\config is not in your path, use it like this:

alias cd=%cmder_root%\config\excd.cmd $*

Results:

tilde-cmder-cd

It's so satisfying to use!


Using this approach allows you to cd into a specific directory relative to the home directory with no hassle.

Note: As stated in the comment above, the tab completion using tilde has arrived in clink so you no longer need to use the provided lua scripts.

@daxgames
Copy link
Member

daxgames commented Oct 2, 2020

Very nice. Maybe it should be included in %cmder_root%\vendor\bin. :-)

@DRSDavidSoft
Copy link
Contributor

@daxgames Thanks, I was going to make a pull request, but I thought I'd ask people to try it first. If it's alright, I'll open a PR in the next days :)

@Beej126
Copy link

Beej126 commented Oct 4, 2020

Veally slick—Cmder is great! Just wanted to share in case you haven't tried it yet: the new Microsoft Terminal with PowerShell (Pwsh) is also worth checking out. It supports tilde paths, allows UNC paths as the current directory, and, you haven't lived until you've seen color emojis in your Windows' terminal. Definitely a game-changer for Windows users! 😄

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants