/everything-cmder

My notes, findings, resources, setups, alises, scripts, etc. for the Windows console emulator Cmder (https://cmder.net/) and *NIX tools.

Primary LanguageBatchfileMIT LicenseMIT

Why Cmder?

Rather than list my opinion of all the pros and cons of Cmder and trying to sell you on using it. I will let the software speak for itself if you decide to use it. Simply put, I prefer using the full version of Cmder bundled with git-for-windows because I find it to be robust, fast, and have easy access to the Windows filesystem. Cmder runs nativley, unlike WSL (Windows Subsystem for Linux) which runs in a sandbox and makes access to the Windows file system convoluted. Allthough WSL is more robust for using *NIX style tools, I find that being confined to a Linux sandbox in the Windows environment for all my daily development tasks to be slower on the I\O side of things and somewhat defeats the purpose of using Windows 10 in the first place.

Why not Powershell?

If you are working in the Windows environment then why use Unix style tools at all? Personally I find the Powershell syntax, learning curve and encoding caveats a bit of a hurdle and just not as versatile as *NIX tools. At times I do find myself working with Powershell in Cmder and I will say that the more I learn the better off I am because of it. I will be sharing what I have learned about Powershell here on this page and you will find some powershell scripts in the repository. TLDR; Powershell is awesome and can be integrated with Cmder. A well rounded scripter will be fluent in as many tools as possible.

Getting True Colors (24-bit color)

Not that anyone will ever really need all 16 million colors in their terminal at once, however being able to choose from a wider pallette of colors has its advantages and it is just plain more fun. Cmder does NOT support true color. The bash shell that comes with Cmder also does NOT support true color. If you try to use powershell with Cmder as its terminal you will NOT get true color. If you want true color in your terminal on Windows or WSL there many ways to do it and there can be many caveats as well. Powershell is a shell and scripting language with true color support so you can use 24-bit color escape sequences and get any color you want but typically not when running powershell in the Cmder standalone console/terminal/terminal emulator. There are bash shells running in WSL for windows such as Ubuntu's bash that you can use but they have a decent amount of weight to them such as installing an entire Linux distro.

Long story short true color support is pretty much dictacted by your console/terminal emulator and shell combination. I have found the following combinations to work easily for true color without dealing with WSL bash.

  • Straight powershell
  • VSCode terminal with powershell
  • VSCode terminal running Cmder with powershell
  • Windows Terminal with powershell
  • Windows Terminal running Cmder with powershell

How to Use This Repository

It suggested that you perouse this document however you see fit. You may also download or copy and paste any portion of any of the files in this repository to fit your needs. Much of this document may be crafted for beginner and intermediate developers in the form of linear step lists, however I am sure that expert developers can probably gleam a few gems from this repository.

This repository is meant to be a relatively random compilation, a somewhat organized mishmosh if you will, of scripts, resources, learning materials, personal findings and revelations that may or may not be 'correct' or optimized but could easily help one learn alot and hopefully help ones workflow to solve some daily challenges that one may encounter.

Anyone that may have an opinion on how to do things better (optimize) or to add any resources to this repository please feel free to open pull requests anywhere you like.

Resources

  • General
  • Cmder
    • Link Windows executables such as find to the console commands in Cmder.
      • Add the following to your \Cmder\config\user_profile.cmd file:
        • set "PATH=%GIT_INSTALL_ROOT%\usr\bin;%PATH%". Reference.
  • Bash Scripting
  • *NIX Tools
    • General
    • sed: Stands for Stream Editor. sed works with streams of characters for searching, filtering and text processing and despite its power, sed is considered a 'simpleton' when compared to awk. It is recommended to use sed for simple regex type operations and one-liners as sed syntax is more terse and compact than awk syntax. Complex multiline sed scripts can look noisy and be harder to read than awk programs.
    • awk: Is a text pattern scanning and processing language, which was created by Aho, Weinberger & Kernighan. awk is mostly used for data extraction and reporting, was built around processing .csv files. awk is considered more robust than sed. awk can do everything that sed can do and much more since awk is essentially a full fledged programming language complete with system calls, and sophisticated constructs such as if/else, while, do/while, etc.
      • Reference
        • urls TBD
      • One-liners
        • Count the number of newlines
          • In a file: awk 'END {print NR}' file.txt
          • From a pipe: awk | 'END {print NR}'
      • Practice awk in real-time using an awk REPL (sandbox) online editor
    • grep: Stands for Global Regular Expression Print and is used to search for specific terms in a file. Different from awk and sed, grep cannot add/modify/remove the text in a specific file. But it’s useful when you just want to search and filter out matches.
    • tr: A command line utility for translating or deleting characters. Great for newline substitutions since sed is not designed for this and the sed syntax for translation involving the \n character is convoluted because it has a harder ro read escape sequence for the newline character. For example, if you wanted to output all the windows paths, each other their own line by replacing all instances of a semicolon with a \n, in sed it would look like much less readable: path | sed -e $'s/,/\\\n/g' than using tr to do the same thing: path | tr ; '\n'.
  • Powershell (check the version in Cmder by typing: powershell $PSVersionTable)
    • Tricks
      • In Cmder, to run powershell in utf8 mode with a custom colorized prompt showing the powershell version number and current directory, just add the following one-liner (alias) to your user_aliases.cmd file:
        • ps=powershell -NoExit -Command "function prompt {$ps = """"Powershell UTF8 v$((Get-Host).Version.Major).$((Get-Host).Version.Minor) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1))""""; Write-Host $ps -NoNewline -ForegroundColor Cyan; return """" """"}; $OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8;"
      • Open any control panel item from this list
        • control appwiz.cpl will open the add/remove programs dialog.
    • IDE
    • Encoding
    • Error Handling
    • Run a program with elevated permissions from a Powershell script. This assumes that the user running the script has administrator privileges. A new cmd terminal window will be opened. We use cmd /k to keep the new process window open. Pass all the arguments at once in a quoted string after the /k . The following example shows the currently installed language packs on the Windows system:
      • Start-Process -FilePath cmd -ArgumentList '/k DISM.exe /Online /Get-Intl /English' -Verb RunAs