隐藏

Windows命令行神器Cmder

发布:2024/11/1 12:00:31作者:管理员 来源:本站 浏览次数:5

Windows命令行神器Cmder

一、Cmder介绍

1.1 Cmder概述

1.2 Cmder的功能与优势

二、Cmder安装&环境配置

2.1 下载与安装

2.2 配置环境变量

2.3 添加到右键菜单

三、Cmder常用设置

3.1 设置中文

3.2 关闭自动检查更新

3.3 设置控制台输出最大行数

3.4 修改CMder默认命令行提示符(λ 替换为 $)

3.5 解决中文乱码问题

3.6 解决中文字体重叠问题

3.7 设置透明度

四、快捷键&实用功能

4.1 快捷键

4.2 实用功能

一、Cmder介绍

1.1 Cmder概述

定义: Cmder是一个软件包,被创建出来以替代Windows上缺乏美观的控制台模拟器。它不仅是一个命令行工具,更是一个集成了多种功能的终端模拟器。


特点: Cmder支持大部分Linux命令,如grep、curl(没有wget)、vim、tar、unzip、ssh、ls、bash、perl等,同时还集成了Windows batch、PowerShell、Git Bash等多种命令行环境于一体。


1.2 Cmder的功能与优势

跨平台命令支持: Cmder不仅可以使用Windows下的所有命令,还支持Linux的命令和shell命令,使得Windows用户能够轻松执行复杂的命令行操作。


美观的界面: 相较于传统的命令提示符(CMD)和PowerShell,Cmder提供了更加美观的界面和可自定义的窗口环境,提升了用户的使用体验。


强大的集成功能: Cmder集成了Git Bash、ssh客户端等多种工具,使得用户可以在不离开熟悉的操作系统环境下享受到更强大的功能。


便捷的操作方式: Cmder支持在不同的标签页中同时连接不同的底层Shell,包括cmd、PowerShell、Bash和WSL,并提供了复制粘贴、超链接点击等便捷操作方式。


二、Cmder安装&环境配置

2.1 下载与安装

Cmder Github


下载cmder.zip并解压到电脑上,建议不要放到C盘,文件路径不要包含中文。


2.2 配置环境变量

1)新建系统变量CMDER_HOME



2)编辑Path,添加%CMDER_HOME%




3)验证是否安装成功(按下 Win + R,输入cmder,回车)




这里部分电脑可能会出现以下两个问题:

1)"'git --version' returned an improper version string"

Win10、Win11可能会出现两次这个警告,这是个历史bug,可以暂时忽略。

参见:https://github.com/cmderdev/cmder/issues/2938


2)'"D:\Develop\Tools\cmder\config\profile.d\Active"' is not recognized as an internal or external command, operable program or batch file.

参见:https://github.com/cmderdev/cmder/issues/1102

这个可能是因为电脑修改过默认编码导致的,可以采用以下方式解决:

按下Win + R,输入regedit,回车,打开注册表;

找到 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor


修改autorun项的值为:chcp 65001>nul

这里说明一下:


chcp 65001:代表每次启动cmd,显示Active code page: 65001

chcp 65001>nul:代表每次启动cmd,不显示Active code page: 65001


修改注册表之后,再启动Cmder,就不会有上面的警告信息了。



2.3 添加到右键菜单

以管理员方式身份运行cmd,运行cmder.exe /REGISTER ALL,然后我们在桌面右键就可以看到Cmder了



三、Cmder常用设置

3.1 设置中文

在Cmder终端,使用快捷键Win + Alt + P打开设置面板,如下选择简体中文,保存设置,重启Cmder。



3.2 关闭自动检查更新



3.3 设置控制台输出最大行数



3.4 修改CMder默认命令行提示符(λ 替换为 $)

找到Cmder安装目录下,config文件夹下的cmder_prompt_config.lua文件

将prompt_lambSymbol的值,修改为 $


再次打开Cmder,就可以看到变化啦。



3.5 解决中文乱码问题

设置-启动-环境,添加以下内容:

set LANG=zh_CN.UTF-8

set LC_ALL=zh_CN.utf8



3.6 解决中文字体重叠问题

设置-字体,取消以下勾选。



3.7 设置透明度



四、快捷键&实用功能

4.1 快捷键

Tab:路径的自动补全


Ctrl+T:建立新页签


Ctrl+W:关闭页签


Ctrl+Tab:切换页签


Ctrl+1:快速切换到第 1 个页签,Ctrl+n 快速切换到第 n 个页签


Ctr+R:历史命令搜索


Ctrl+鼠标滚轮:字体的快速放大/缩小


Alt+F4:关闭所有页签


Alt+enter:切换到全屏状态


Win+Alt+P:进入cmder设置菜单


4.2 实用功能

选中内容,自动复制

集成了git、ssh、ls等终端命令

多开分屏


集成到IDE中,作为默认终端使用

Why use it

The main advantage of Cmder is portability. It is designed to be totally self-contained with no external dependencies, which makes it great for USB Sticks or cloud storage. So you can carry your console, aliases and binaries (like wget, curl and git) with you anywhere.


The Cmder's user interface is also designed to be more eye pleasing, and you can compare the main differences between Cmder and ConEmu here.


Installation

Single User Portable Config

Download the latest release

Extract the archive. Note: This path should not be C:\Program Files or anywhere else that would require Administrator access for modifying configuration files

(optional) Place your own executable files into the %cmder_root%\bin folder to be injected into your PATH.

Run Cmder.exe

Shared Cmder install with Non-Portable Individual User Config

Download the latest release

Extract the archive to a shared location.

(optional) Place your own executable files and custom app folders into the %cmder_root%\bin. See: bin/README.md

This folder to be injected into your PATH by default.

See /max_depth [1-5] in 'Command Line Arguments for init.bat' table to add subdirectories recursively.

(optional) Place your own custom app folders into the %cmder_root%\opt. See: opt/README.md

This folder will NOT be injected into your PATH so you have total control of what gets added.

Run Cmder.exe with /C command line argument. Example: cmder.exe /C %userprofile%\cmder_config

This will create the following directory structure if it is missing.


c:\users\[username]\cmder_config

├───bin

├───config

│   └───profile.d

└───opt

(optional) Place your own executable files and custom app folders into %userprofile%\cmder_config\bin.

This folder to be injected into your PATH by default.

See /max_depth [1-5] in 'Command Line Arguments for init.bat' table to add subdirectories recursively.

(optional) Place your own custom app folders into the %user_profile%\cmder_config\opt.

This folder will NOT be injected into your PATH so you have total control of what gets added.

Both the shared install and the individual user config locations can contain a full set of init and profile.d scripts enabling shared config with user overrides. See below.

Cmder.exe Command Line Arguments

Argument Description

/C [user_root_path] Individual user Cmder root folder. Example: %userprofile%\cmder_config

/M Use conemu-%computername%.xml for ConEmu settings storage instead of user_conemu.xml

/REGISTER [ALL, USER] Register a Windows Shell Menu shortcut.

/UNREGISTER [ALL, USER] Un-register a Windows Shell Menu shortcut.

/SINGLE Start Cmder in single mode.

/START [start_path] Folder path to start in.

/TASK [task_name] Task to start after launch.

/X [ConEmu extras pars] Forwards parameters to ConEmu

Context Menu Integration

So you've experimented with Cmder a little and want to give it a shot in a more permanent home;


Shortcut to open Cmder in a chosen folder

Open a terminal as an Administrator

Navigate to the directory you have placed Cmder

Execute .\cmder.exe /REGISTER ALL If you get an "Access Denied" message, make sure you are executing the command in an Administrator prompt.

In a file explorer window right click in or on a directory to see "Cmder Here" in the context menu.


Keyboard shortcuts

Tab manipulation

Ctrl + T : New tab dialog (maybe you want to open cmd as admin?)

Ctrl + W : Close tab

Ctrl + D : Close tab (if pressed on empty command)

Shift + Alt + #Number : Fast new tab: 1 - CMD, 2 - PowerShell

Ctrl + Tab : Switch to next tab

Ctrl + Shift + Tab : Switch to previous tab

Ctrl + #Number : Switch to tab #Number

Alt + Enter: Fullscreen

Shell

Ctrl + Alt + U : Traverse up in directory structure (lovely feature!)

End, Home, Ctrl : Traversing text with as usual on Windows

Ctrl + R : History search

Shift + Mouse : Select and copy text from buffer

(Some shortcuts are not yet documented, though they exist - please document them here)


Features

Access to multiple shells in one window using tabs

You can open multiple tabs each containing one of the following shells:


Task Shell Description

Cmder cmd.exe Windows cmd.exe shell enhanced with Git, Git aware prompt, Clink (GNU Readline), and Aliases.

Cmder as Admin cmd.exe Administrative Windows cmd.exe Cmder shell.

PowerShell powershell.exe Windows PowerShell enhanced with Git and Git aware prompt .

PowerShell as Admin powershell.exe Administrative Windows powershell.exe Cmder shell.

Bash bash.exe Unix/Linux like bash shell running on Windows.

Bash as Admin bash.exe Administrative Unix/Linux like bash shell running on Windows.

Mintty bash.exe Unix/Linux like bash shell running on Windows. See below for Mintty configuration differences

Mintty as Admin bash.exe Administrative Unix/Linux like bash shell running on Windows. See below for Mintty configuration differences

Cmder, PowerShell, and Bash tabs all run on top of the Windows Console API and work as you might expect in Cmder with access to use ConEmu's color schemes, key bindings and other settings defined in the ConEmu Settings dialog.


⚠ Note: Only the full edition of Cmder comes with a pre-installed bash, using a vendored git-for-windows installation. The pre-configured Bash tabs may not work on Cmder mini edition without additional configuration.


You may however, choose to use an external installation of bash, such as Microsoft's Subsystem for Linux (called WSL) or the Cygwin project which provides POSIX support on windows.


⚠ Note: Mintty tabs use a program called 'mintty' as the terminal emulator that is not based on the Windows Console API, rather it's rendered graphically by ConEmu. Mintty differs from the other tabs in that it supports xterm/xterm-256color TERM types, and does not work with ConEmu settings like color schemes and key bindings. As such, some differences in functionality are to be expected, such as Cmder not being able to apply a system-wide configuration to it.


As a result mintty specific config is done via the [%USERPROFILE%|$HOME]/.minttyrc file. You may read more about Mintty and its config file here.


An example of setting Cmder portable terminal colors for mintty:


From a bash/mintty shell:


cd $CMDER_ROOT/vendor

git clone https://github.com/karlin/mintty-colors-solarized.git

cd mintty-colors-solarized/

echo source \$CMDER_ROOT/vendor/mintty-colors-solarized/mintty-solarized-dark.sh>>$CMDER_ROOT/config/user_profile.sh

You may find some Monokai color schemes for mintty to match Cmder here.


Changing Cmder Default cmd.exe Prompt Config File

The default Cmder shell cmd::Cmder prompt is customized using Clink and is configured by editing a config file that exists in one of two locations:


Single User Portable Config %CMDER_ROOT%\config\cmder_prompt_config.lua

Shared Cmder install with Non-Portable Individual User Config %CMDER_USER_CONFIG%\cmder_prompt_config.lua

If your Cmder setup does not have this file create it from %CMDER_ROOT%\vendor\cmder_prompt_config.lua.default


Customizations include:


Colors.

Single/Multi-line.

Full path/Folder only.

[user]@[host] to the beginning of the prompt.

~ for home directory.

λ symbol

Documentation is in the file for each setting.


Changing Cmder Default cmd.exe Shell Startup Behaviour Using Task Arguments

Press Win + Alt + T

Click either:

1. {cmd::Cmder as Admin}

2. {cmd::Cmder}

Add command line arguments where specified below:

Note: Pay attention to the quotes!


cmd /s /k ""%ConEmuDir%\..\init.bat" [ADD ARGS HERE]"

Command Line Arguments for init.bat

Argument Description Default

/c [user cmder root] Enables user bin and config folders for 'Cmder as admin' sessions due to non-shared environment. not set

/d Enables debug output. not set

/f Enables Cmder Fast Init Mode. This disables some features, see pull request #1492 for more details. not set

/t Enables Cmder Timed Init Mode. This displays the time taken run init scripts not set

/git_install_root [file path] User specified Git installation root path. %CMDER_ROOT%\vendor\Git-for-Windows

/home [home folder] User specified folder path to set %HOME% environment variable. %userprofile%

/max_depth [1-5] Define max recurse depth when adding to the path for %cmder_root%\bin and %cmder_user_bin% 1

/nix_tools [0-2] Define how *nix tools are added to the path. Prefer Windows Tools: 1, Prefer *nix Tools: 2, No /usr/bin in %PATH%: 0 1

/svn_ssh [path to ssh.exe] Define %SVN_SSH% so we can use git svn with ssh svn repositories. %GIT_INSTALL_ROOT%\bin\ssh.exe

/user_aliases [file path] File path pointing to user aliases. %CMDER_ROOT%\config\user_aliases.cmd

/v Enables verbose output. not set

(custom arguments) User defined arguments processed by cexec. Type cexec /? for more usage. not set

Cmder Shell User Config

Single user portable configuration is possible using the Cmder specific shell config files. Edit the below files to add your own configuration:


Shell Cmder Portable User Config

Cmder %CMDER_ROOT%\config\user_profile.cmd

PowerShell $ENV:CMDER_ROOT\config\user_profile.ps1

Bash/Mintty $CMDER_ROOT/config/user_profile.sh

Note: Bash and Mintty sessions will also source the $HOME/.bashrc file if it exists after it sources $CMDER_ROOT/config/user_profile.sh.


You can write *.cmd|*.bat, *.ps1, and *.sh scripts and just drop them in the %CMDER_ROOT%\config\profile.d folder to add startup config to Cmder.


Shell Cmder Profile.d Scripts

Cmder %CMDER_ROOT%\config\profile.d\*.bat and *.cmd

PowerShell $ENV:CMDER_ROOT\config\profile.d\*.ps1

Bash/Mintty $CMDER_ROOT/config/profile.d/*.sh

Git Status Opt-Out

To disable Cmder prompt git status globally add the following to ~/.gitconfig or locally for a single repo [repo]/.git/config and start a new session.


Note: This configuration is not portable


[cmder]

 status = false      # Opt out of Git status for 'ALL' Cmder supported shells.

 cmdstatus = false   # Opt out of Git status for 'Cmd.exe' shells.

 psstatus = false    # Opt out of Git status for 'Powershell.exe and 'Pwsh.exe' shells.

 shstatus = false    # Opt out of Git status for 'bash.exe' shells.

Aliases

Cmder(Cmd.exe) Aliases

You can define simple aliases for cmd.exe sessions with a command like alias name=command. Cmd.exe aliases support optional parameters through the $1-9 or the $* special characters so the alias vi=vim.exe $* typed as vi [filename] will open [filename] in vim.exe.


Cmd.exe aliases can also be more complex. See: DOSKEY.EXE documentation for additional details on complex aliases/macros for cmd.exe


Aliases defined using the alias.bat command will automatically be saved in the %CMDER_ROOT%\config\user_aliases.cmd file


To make an alias and/or any other profile settings permanent add it to one of the following:


Note: These are loaded in this order by $CMDER_ROOT/vendor/init.bat. Anything stored in %CMDER_ROOT% will be a portable setting and will follow Cmder to another machine.


%CMDER_ROOT%\config\profile.d\*.cmd and \*.bat

%CMDER_ROOT%\config\user_aliases.cmd

%CMDER_ROOT%\config\user_profile.cmd

Bash.exe|Mintty.exe Aliases

Bash shells support simple and complex aliases with optional parameters natively so they work a little different. Typing alias name=command will create an alias only for the current running session.


To make an alias and/or any other profile settings permanent add it to one of the following:


Note: These are loaded in this order by $CMDER_ROOT/vendor/git-for-windows/etc/profile.d/cmder.sh. Anything stored in $CMDER_ROOT will be a portable setting and will follow Cmder to another machine.


$CMDER_ROOT/config/profile.d/*.sh

$CMDER_ROOT/config/user_profile.sh

$HOME/.bashrc

If you add bash aliases to $CMDER_ROOT/config/user_profile.sh they will be portable and follow your Cmder folder if you copy it to another machine. $HOME/.bashrc defined aliases are not portable.


PowerShell.exe Aliases

PowerShell has native simple alias support, for example [new-alias | set-alias] alias command, so complex aliases with optional parameters are not supported in PowerShell sessions. Type get-help [new-alias|set-alias] -full for help on PowerShell aliases.


To make an alias and/or any other profile settings permanent add it to one of the following:


Note: These are loaded in this order by $ENV:CMDER_ROOT\vendor\user_profile.ps1. Anything stored in $ENV:CMDER_ROOT will be a portable setting and will follow Cmder to another machine.


$ENV:CMDER_ROOT\config\profile.d\*.ps1

$ENV:CMDER_ROOT\config\user_profile.ps1

SSH Agent

To start the vendored SSH agent simply call start-ssh-agent, which is in the vendor/git-for-windows/cmd folder.


If you want to run SSH agent on startup, include the line @call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd" in %CMDER_ROOT%/config/user_profile.cmd (usually just uncomment it).


Vendored Git

Cmder is by default shipped with a vendored Git installation. On each instance of launching Cmder, an attempt is made to locate any other user provided Git binaries. Upon finding a git.exe binary, Cmder further compares its version against the vendored one by executing it. The vendored git.exe binary is only used when it is more recent than the user-installed one.


You may use your favorite version of Git by including its path in the %PATH% environment variable. Moreover, the Mini edition of Cmder (found on the downloads page) excludes any vendored Git binaries.


Using external Cygwin/Babun, MSys2, WSL, or Git for Windows SDK with Cmder.

You may run bash (the default shell used on Linux, macOS and GNU/Hurd) externally on Cmder, using the following instructions:


Setup a new task by pressing Win +Alt + T.

Click the + button to add a task.

Name the new task in the top text box.

Provide task parameters, this is optional.

Add cmd /c "[path_to_external_env]\bin\bash --login -i" -new_console to the Commands text box.

Recommended Optional Steps:


Copy the vendor/cmder_exinit file to the Cygwin/Babun, MSys2, or Git for Windows SDK environments /etc/profile.d/ folder to use portable settings in the $CMDER_ROOT/config folder.


Note: MinGW could work if the init scripts include profile.d but this has not been tested.


The destination file extension depends on the shell you use in that environment. For example:


bash - Copy to /etc/profile.d/cmder_exinit.sh

zsh - Copy to /etc/profile.d/cmder_exinit.zsh

Uncomment and edit the line below in the script to use Cmder config even when launched from outside Cmder.


# CMDER_ROOT=${USERPROFILE}/cmder  # This is not required if launched from Cmder.

Customizing user sessions using init.bat custom arguments.

You can pass custom arguments to init.bat and use cexec.cmd in your user_profile.cmd to evaluate these arguments then execute commands based on a particular flag being detected or not.


init.bat creates two shortcuts for using cexec.cmd in your profile scripts.


%ccall% - Evaluates flags, runs commands if found, and returns to the calling script and continues.

ccall=call C:\Users\user\cmderdev\vendor\bin\cexec.cmd

Example: %ccall% /startnotepad start notepad.exe


%cexec% - Evaluates flags, runs commands if found, and does not return to the calling script.

cexec=C:\Users\user\cmderdev\vendor\bin\cexec.cmd

Example: %cexec% /startnotepad start notepad.exe


It is useful when you have multiple tasks to execute cmder and need it to initialize the session differently depending on the task chosen.


To conditionally start notepad.exe when you start a specific cmder task:


Press win+alt+t


Click + to add a new task.


Add the below to the Commands block:


cmd.exe /k ""%ConEmuDir%\..\init.bat" /startnotepad"

Add the below to your %cmder_root%\config\user_profile.cmd


%ccall% "/startNotepad" "start" "notepad.exe"`

To see detailed usage of cexec, type cexec /? in Cmder.


Integrating Cmder with Windows Terminal, VS Code, and your favorite IDEs

Cmder by default comes with a vendored ConEmu installation as the underlying terminal emulator, as stated here.


However, Cmder can in fact run in a variety of other terminal emulators, and even integrated IDEs. Assuming you have the latest version of Cmder, follow the following instructions to get Cmder working with your own terminal emulator.


For instructions on how to integrate Cmder with your IDE, please read our Wiki section.


Upgrading

The process of upgrading Cmder depends on the version/build you are currently running.


If you have a [cmder_root]/config/user[-|_]conemu.xml, you are running a newer version of Cmder, follow the below process:


Exit all Cmder sessions and relaunch [cmder_root]/cmder.exe, this backs up your existing [cmder_root]/vendor/conemu-maximus5/conemu.xml to [cmder_root]/config/user[-|_]conemu.xml.


The [cmder_root]/config/user[-|_]conemu.xml contains any custom settings you have made using the 'Setup Tasks' settings dialog.

Exit all Cmder sessions and backup any files you have manually edited under [cmder_root]/vendor.


Editing files under [cmder_root]/vendor is not recommended since you will need to re-apply these changes after any upgrade. All user customizations should go in [cmder_root]/config folder.

Delete the [cmder_root]/vendor folder.


Extract the new cmder.zip or cmder_mini.zip into [cmder_root]/ overwriting all files when prompted.


If you do not have a [cmder_root]/config/user[-|_]conemu.xml, you are running an older version of cmder, follow the below process:


Exit all Cmder sessions and backup [cmder_root]/vendor/conemu-maximus5/conemu.xml to [cmder_root]/config/user[-|_]conemu.xml.


Backup any files you have manually edited under [cmder_root]/vendor.


Editing files under [cmder_root]/vendor is not recommended since you will need to re-apply these changes after any upgrade. All user customizations should go in [cmder_root]/config folder.

Delete the [cmder_root]/vendor folder.


Extract the new cmder.zip or cmder_mini.zip into [cmder_root]/ overwriting all files when prompted.


Current development builds

You can download builds of the current development branch by going to AppVeyor via the following link:


AppVeyor


The latest download builds by GitHub Actions can be downloaded from the link below:


Build Status


License

All software included is bundled with own license


The MIT License (MIT)


Copyright (c) 2016 Samuel Vasko


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.