Cmder: ~ doesn't work in cmder?

Created on 28 Jul 2016  路  14Comments  路  Source: cmderdev/cmder

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.

Most helpful comment

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位

All 14 comments

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).

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

So this is an issue of ~ instead of rm

位 cd ~
The system cannot find the path specified.

ConEmu 160710 [64] {Stable}

Try using PowerShell.

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.

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.

@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\

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)

I put "tilde_match" function in cmderrootconfigtilde.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.

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位

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!

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

@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 :)

very slick... cmder is great... just to overshare, in case y'all haven't tried yet... the new Msft Terminal + Pwsh is pretty good too... tilde works... allowing unc path for current directory is nice... and you haven't lived till you've seen color emojis in your Windows terminal =)

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zsedcbm picture zsedcbm  路  3Comments

brunowego picture brunowego  路  3Comments

bmeverett picture bmeverett  路  3Comments

luisrudge picture luisrudge  路  3Comments

edgariscoding picture edgariscoding  路  3Comments