Clink
Bash's powerful command line editing in cmd.exe
Install / Use
/learn @chrisant996/ClinkREADME
Overview
Clink combines the native Windows shell cmd.exe with the powerful command line editing features of the GNU Readline library, which provides rich completion, history, and line-editing capabilities. Readline is best known for its use in the Unix shell Bash, the standard shell for many Linux distributions.
For details, refer to the Clink documentation.
[!NOTE] Starting Clink injects it into a
cmd.exeprocess, where it intercepts a handful of Windows API functions so that it can replace the prompt and input line editing with its own Readline-powered enhancements.
Download
Downloads are available from the releases page.
See the issues page for known issues or to file new issues.
Feature Highlights
- Auto-Suggestions; Clink offers suggestions as you type based on history, files, and completions.
- Completions; Clink can complete words when you press <kbd>Tab</kbd> or <kbd>Ctrl</kbd>-<kbd>Space</kbd> (interactive completion list).
- Persistent History; Clink stores persistent history between sessions.
- Scriptable Prompt; You can customize the prompt dynamically with Lua scripts -- like in other shells -- but never before possible in cmd.exe!
- Colored Input Line; Your input is colored by context sensitive completion scripts.
- Command Line Editing Improvements; Clink supercharges the command line with new input editing commands and configurable key bindings.
- Auto-answering of the "Terminate batch job?" prompt.
- and much more!
Installation
You can install Clink by running the setup EXE file from the releases page.
Or by using winget and running winget install clink.
Or by using scoop and running scoop install clink.
Or by downloading the ZIP file from releases page, and extracting the files to a directory of your choosing.
Usage
Once installed, there are several ways to start Clink.
-
If Clink is configured for autorun, just start
cmd.exeand Clink is automatically injected and ready to use.The setup EXE has an option "Autorun when cmd.exe starts". If you didn't use the setup EXE, or if you want to enable or disable autorun later, you can run
clink autorun installorclink autorun uninstallto change the autorun configuration. Runclink autorun --helpfor more info. -
To manually start, run the Clink shortcut from the Start menu (or the clink.bat located in the install directory).
-
To establish Clink to an existing
cmd.exeprocess, useclink inject.If the Clink install directory isn't in the PATH, then use <code><em>install_dir</em>\clink</code> in place of
clinkto run Clink commands. Once Clink is injected into acmd.exeprocess, then it automatically sets an alias so that you can simply useclink.
You can use Clink right away without configuring anything:
- Searchable command history will be saved between sessions.
- <kbd>Tab</kbd> and <kbd>Ctrl</kbd>-<kbd>Space</kbd> will do match completion two different ways.
- Press <kbd>Alt</kbd>-<kbd>h</kbd> to see a list of the current key bindings.
- Press <kbd>Alt</kbd>-<kbd>Shift</kbd>-<kbd>/</kbd> followed by another key to see what command is bound to the key.
See Getting Started for information on how to get started with using Clink.
Upgrading from Clink v0.4.9
The new Clink tries to be as backward compatible with Clink v0.4.9 as possible. However, in some cases upgrading may require a little bit of configuration work. More details can be found in the Clink documentation.
Extending Clink
Clink can be extended through its Lua API which allows easy creation of context sensitive match generators, prompt filtering, and more. More details can be found in the Clink documentation.
Building Clink
Clink uses Premake to generate Visual Studio solutions or makefiles for MinGW. Note that Premake >= 5.0.0-beta4 is required.
- Cd to your clone of Clink.
- Run <code>premake5.exe <em>toolchain</em></code> (where <em>toolchain</em> is one of Premake's actions - see
premake5.exe --help) - Build scripts will be generated in <code>.build\<em>toolchain</em></code>. For example
.build\vs2022\clink.sln. - Call your toolchain of choice (VS, mingw32-make.exe, msbuild.exe, etc). GNU makefiles (Premake's gmake target) have a help target for more info.
[!NOTE] Only the VS toolset is supported. If you want to try using MinGW, then make sure to use the msvcrt version because
setjmpdoesn't link in the URT version of MinGW.
Building Documentation
- Make sure npm and python are installed.
- Run
npm install -g marked@2.0.1to install the marked markdown library (version 2.0.1). - Run
pip install fonttools brotli lxmlto install the pyftsubset font optimizer tool and the lxml parser. - Run
premake5.exe docs.
[!IMPORTANT] Clink documentation uses marked@2.0.1. Newer versions of marked have introduced breaking changes, and I haven't yet rewritten how Clink builds the documentation to accommodate the breaking changes. The security fixes in newer versions aren't relevant since marked is only used at build time with known inputs (the marked library is no longer embedded in the documentation as of commit d5b39ca in Oct 2020).
The python dependencies are needed in order to make a subset of the Caskaydia Cove nerd font (based on Cascadia Code) to display the prompts. This optimization reduces the size (in
woff2) from ~1220 kB to ~12 kB allowing it to be bundled with theclink.htmlfile.
Debugging Clink
- Start Clink using any of the normal ways.
- Launch a debugger such as Visual Studio.
- Attach the debugger to the CMD.exe process that Clink was injected into.
- If you break into the debugger now, it will be inside Clink code, waiting for keyboard input.
- Here are some breakpoints that might be useful:
host::edit_lineis the start of showing a prompt and accepting input.line_editor_impl::update_matchesis where the match pipeline uses.generate()to collect matches and.select()to filter the matches.rl_module::on_inputandreadline_internal_char(and the_rl_dispatchinside it) is where keys are translated through Readline's keymap to invoke commands.rl_completeorrl_menu_completeorrl_old_menu_completeare the Readline completion commands.alternative_matchesbuilds a Readline match array from the results collected by the match pipeline.
Debugging Clink startup
The easiest way to debug Clink startup is to use simulated injection rather than real injection: set a breakpoint on initialise_clink and start clink testbed --hook under the debugger. All of the usual Clink startup code is executed, but the cross-process injection is only simulated, so the resulting prompts of course are not actually executed by CMD.exe.
To debug Clink startup during real injection, you must attach the debugger to the target CMD.exe before injection, set a breakpoint on initialise_clink (it will be an unresolved breakpoint at first), and then use clink inject -p process_id. The debugger should resolve the breakpoint as soon as the Clink DLL is loaded, and should hit the breakpoint soon after.
Debugging Clink DLL injection
To debug the actual DLL injection procedure, you must debug both the clink_x64.exe (or clink_x86.exe) process and the target CMD.exe process.
- Set a breakpoint on
process::remote_call_internalin the Clink process.- The first time it's reached should be for injecting a
LoadLibrarycall into the target CMD.exe process. - The second time it's reached should be for injecting an
initialise_clinkcall into the target CMD.exe process.
- The first time it's reached should be for injecting a
- Set a breakpoint on
initialise_clinkin the target CMD.exe process. - Step through the
remote_call_internalfunction to inspect the local side of the operation.- The
stdcall_thunkfunction is the instruction payload that will be copied into the target CMD.exe process. - Observe the value of
region.basebefore theCreateRemoteThreadcall executes -- this is the address of the instruction payload that has been copied into the target CMD.exe process. - Set a breakpoint in the target CMD.exe process for that address -- when
CreateRemoteThreadexecutes, it will transfer execution to that address in the target CMD.exe process, and you can debug through the execution of the instruction payload itself.
- The
[!CAUTION] If the instruction payload references any functions or variables from the Clink process, it will crash during execution inside the target CMD.exe process. Compiler features like the "just my code", "edit and continue", "omit frame pointers", exception handling, inlining, and runtime checks must be configured appropriately to keep the
