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

Question: How to start cmder with a command #457

Closed
bapti opened this issue Apr 13, 2015 · 17 comments
Closed

Question: How to start cmder with a command #457

bapti opened this issue Apr 13, 2015 · 17 comments

Comments

@bapti
Copy link

bapti commented Apr 13, 2015

Hi,

I'd like to be able to write a batch file and run a command through cmder. I've got this so far

START c:\tools\cmder\cmder.exe /START my-command

However this doesn't seem to run the command. I've been searching around to try and figure out how to do this but I can't find anything. Is it possible? If so I'll submit a pull request and add it to the documentation.

@keil0
Copy link

keil0 commented Apr 19, 2015

Hi,
For start cmder with specified command on windows, create one file .bat with the following content:

@echo off
set CMDER_ROOT=%~dp0
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title Cmder /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"

then call this .bat with your command:

"path/to/my/file.bat" "<yourcommand>"

I hope this helps you :)

@bapti
Copy link
Author

bapti commented Apr 23, 2015

Thanks a million, I'll test this out over the next week!

@bapti
Copy link
Author

bapti commented Jun 24, 2015

Hi, sorry for taking so long to reply - this worked fine. Thanks!

@bapti bapti closed this as completed Jun 24, 2015
@cascornelissen
Copy link

I'm trying to do the same, I have a file called cmder.bat with the following contents:

@echo off
set CMDER_ROOT=C:\Cmder
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title "Homestead VM" /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"

And am running test.bat (in the same directory):

"cmder.bat" "w: && cd \tools\homestead"

This causes both a ConEmu prompt and a Cmder prompt to pop up. The commands get executed in the Cmder window but the ConEmu windows stays open with the Press Enter or Esc to close the console... message.

Is there any way to close the ConEmu window?

And another thing I noticed: when creating a new console (via de title bar of the Cmder prompt > New Console...) it starts the same .bat file. Would there be any way to 'reset' this to the defaults?

@apudiu
Copy link

apudiu commented Nov 12, 2016

I like to make an alias to 'cd' into a deep directory, I created the alias on .minttyrc on my home folder.. but the problem is cmder is not loading that .minttyrc file when startup. How can I make cmder to load that file on (cmder)startup ?

@schlichtanders
Copy link

I still get this weird second ConEmu window...
any update on this after 2 years?

@AWBuchanan7
Copy link

AWBuchanan7 commented Mar 12, 2018

I think if you're doing this from within cmder you can use something like this:

cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1"

Whereas if you need the command to run from outside of cmder, you can then do something like:

set CMDER_ROOT= <PATH_TO_CMDER>
start %CMDER_ROOT%..\Cmder.exe /START "cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %~1""

Also this can be done if you want to launch Powershell and not cmd (from within cmder):

start PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command "Invoke-Expression '. ''%ConEmuDir%\..\profile.ps1'''; Invoke-Expression %~1"

And for Powershell external from cmder:

set CMDER_ROOT= <PATH_TO_CMDER>
start %CMDER_ROOT%..\Cmder.exe /START "cmd /k "%CMDER_ROOT%\vendor\init.bat && PowerShell -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command Invoke-Expression '. ''%CMDER_ROOT%\vendor\profile.ps1'''; Invoke-Expression %~1""

@wanidulf
Copy link

wanidulf commented May 29, 2020

This worked for me:

`@echo off

set CMDER_ROOT=**insert_path_to_cmder_folder**

start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title Cmder /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "**name_of_a_second_bat_file_on_the_this_same_folder_this**"`

@iki
Copy link

iki commented Jun 2, 2022

This batch opens a new cmder console, and optionally runs a command in it. If the command is one of defined shells, then it skips command processor too.

@echo off

if "%~1"==":run" goto :run

setlocal

:: Use default path from chocolatey cmder package
if not defined CMDER_ROOT set CMDER_ROOT=%SystemDrive%\tools\Cmder
if not exist "%CMDER_ROOT%\cmder.exe" echo Cmder not found in "%CMDER_ROOT%" >&2 && exit /b 1

if "%~1"=="" (
  set CC_RUN=cmd /k "%CMDER_ROOT%\vendor\init.bat"
) else if "%~1"=="sh" (
  set CC_RUN=%*
) else if "%~1"=="bash" (
  set CC_RUN=%*
) else if "%~1"=="pwsh" (
  set CC_RUN=%*
) else if "%~1"=="powershell" (
  set CC_RUN=%*
) else (
  set CC_RUN=cmd /k %0 :run %*
)

if exist "%CMDER_ROOT%\config\user-ConEmu.xml" (
  set CC_CONFIG=%CMDER_ROOT%\config\user-ConEmu.xml
) else (
  set CC_CONFIG=%CMDER_ROOT%\config\ConEmu.xml
)
                                                                                          
start "Console" "%CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe" -Single -NoCloseConfirm -Icon "%CMDER_ROOT%\cmder.exe" -Title "Console" -LoadCfgFile "%CC_CONFIG%" -Run %CC_RUN%

exit /b %errorlevel%

:run
call "%CMDER_ROOT%\vendor\init.bat"
if "%~2"=="" exit /b 0
for /f "tokens=1,* delims= " %%a in ("%*") do set CC_CMD=%%b
echo === %CC_CMD% >&2
call %CC_CMD%
exit /b %errorlevel%

@am3000
Copy link

am3000 commented Mar 11, 2023

SOLVED IT:

set CMDER_ROOT=C:/Tools/Cmder
set MY_COMMAND=tail -f php-error.log
start %CMDER_ROOT%\vendor\conemu-maximus5\ConEmu.exe /icon "%CMDER_ROOT%\cmder.exe" /title Cmder /loadcfgfile "%CMDER_ROOT%\config\ConEmu.xml" /cmd cmd /k "%CMDER_ROOT%\vendor\init.bat cd %CD% && %MY_COMMAND%"

Hey guys, i don´t get it.
I want a .bat file that on double click opens cmder and automatically executes "tail -f mylogfile.txt"
Is that possible?
Thanks!

@daxgames
Copy link
Member

daxgames commented Mar 11, 2023

Why do you have to use Cmder to do this.

What is the benefit of doing it with Cmder/ConEmu over just doing:

Cmd.exe /c "tail -f "mylogfile.txt""

Do you just want it in a tab?

@am3000
Copy link

am3000 commented Mar 11, 2023

Cmd.exe /c "tail -f "mylogfile.txt""

this doesnt work, it does not know tail

But that brings me to an idea.
How would I start another tail in another tab in the same .bat file?

@daxgames
Copy link
Member

Then either add the folder tail is in to the path or add the fully qualified path to tail in the bat file.

To find it inside cmder type where tail

@daxgames
Copy link
Member

You could also do the following:

  1. Download this build

  2. Create a batch file:

    @echo off
    
    call C:\Users\vagrant\cmderdev\vendor\init.bat
    C:\Users\vagrant\cmderdev\cmder /task Tail::File
    
  3. Create a task:

    image

  4. Run the batch file.

    image

@virginviolet
Copy link

virginviolet commented Apr 29, 2024

This batch opens a new cmder console, and optionally runs a command in it. If the command is one of defined shells, then it skips command processor too.

Thank you so much for this!

If you replace this line

echo === %CC_CMD% >&2

with the following two lines, it will echo the command with a lambda at the beginning, instead of ===.

chcp 65001 >nul 2>&1
echo λ %CC_CMD% >&2

@virginviolet
Copy link

By the way, you can also make Cmder/ConEmu the default terminal on Windows, so that whenever you run a batch file, it will be opened in Cmder automatically.

@DRSDavidSoft
Copy link
Contributor

@virginviolet Also on Windows 10/11, you can use Cmder in Windows Terminal which has the same set as default terminal feature.

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