How to Add an Icon to The Bash Prompt

How to add an icon to the bash prompt

Sorry, no. Terminals don't do graphics.

For a full description of what you can do, see the PROMPTING section of the bash(1) man page:

PROMPTING


When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

\a     an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the
prompt string; an empty format results in a locale-specific time
representation. The braces are required
\e an ASCII escape character (033)
\h the hostname up to the first ‘.’
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell’s terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final
slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde (uses the
value of the PROMPT_DIRTRIM variable)
\W the basename of the current working directory, with $HOME abbreviated with a
tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a
terminal control sequence into the prompt
\] end a sequence of non-printing characters

The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).

The \e, \[ and \] escape sequences deserve special attention. With these you can insert ANSI escape codes to command the terminal to change foreground color, background color, move the cursor, erase parts of the screen, and do other fancy tricks.

That is, for instance, how your prompt changes color. \[\e[0;31m\] sets the foreground color to red, and \[\e[0;0m\] resets it back to the default.

GitBash duplicate taskbar icon

Pinning the git-bash.exe fixed the problem for me. This also fixed the problem with broken icon graphic.

NOTE! This was on Windows 7 - haven't tested on Win10!

  1. Open Windows Explorer.
  2. Navigate to C:\Program Files\Git.
  3. Right click git-bash.exe, select Pin to Taskbar.
  4. Shift-right click the newly pinned icon, select Properties.
  5. Add to the end of the field Target: --cd-to-home
  6. Set the value of the field Start in: %HOMEDRIVE%%HOMEPATH%
  7. Press OK.

Git Bash Properties

How to change the icon of .bat file programmatically?

Assuming you're referring to MS-DOS batch files: as it is simply a text file with a special extension, a .bat file doesn't store an icon of its own.

You can, however, create a shortcut in the .lnk format that stores an icon.

How do I use Bash on Windows from the Visual Studio Code integrated terminal?

  1. Install Git from https://git-scm.com/download/win

  2. Open Visual Studio Code and press and hold Ctrl + ` to open the terminal.

    Sample Image

  3. Open the command palette using Ctrl + Shift + P.

  4. Type - Select Default Profile

  5. Select Git Bash from the options

  6. Click on the + icon in the terminal window

  7. The new terminal now will be a Git Bash terminal. Give it a few seconds to load Git Bash

    Sample Image

  8. You can now toggle between the different terminals as well from the dropdown in terminal.

    Sample Image

Adding Git-Bash to the new Windows Terminal

Overview

  1. Open settings with Ctrl+,
  2. You'll want to append one of the profiles options below (depending on what version of git you have installed) to the "list": portion of the settings.json file:

Open settings.json in Windows Terminal sidebar

{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{00000000-0000-0000-ba54-000000000001}",

"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles
},
"list":
[
<put one of the configuration below right here>
]
}
}

Profile options

Uncomment correct paths for commandline and icon if you are using:

  • Git for Windows in %PROGRAMFILES%
  • Git for Windows in %USERPROFILE%
  • If you're using scoop
{
"guid": "{00000000-0000-0000-ba54-000000000002}",
"commandline": "%PROGRAMFILES%/Git/usr/bin/bash.exe -i -l",
// "commandline": "%USERPROFILE%/AppData/Local/Programs/Git/bin/bash.exe -l -i",
// "commandline": "%USERPROFILE%/scoop/apps/git/current/usr/bin/bash.exe -l -i",
"icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
// "icon": "%USERPROFILE%/AppData/Local/Programs/Git/mingw64/share/git/git-for-windows.ico",
// "icon": "%USERPROFILE%/scoop/apps/git/current/usr/share/git/git-for-windows.ico",
"name" : "Bash",
"startingDirectory" : "%USERPROFILE%"
},

You can also add other options like:

{
"guid": "{00000000-0000-0000-ba54-000000000002}",
// ...
"acrylicOpacity" : 0.75,
"closeOnExit" : true,
"colorScheme" : "Campbell",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"historySize" : 9001,
"padding" : "0, 0, 0, 0",
"snapOnInput" : true,
"useAcrylic" : true
}

Notes

  • make your own guid as of https://github.com/microsoft/terminal/pull/2475 this is no longer generated.
  • the guid can be used in in the globals > defaultProfile so you can press you can press CtrlShiftT
    or start a Windows terminal and it will start up bash by default
"defaultProfile" : "{00000000-0000-0000-ba54-000000000001}",
  • -l -i to make sure that .bash_profile gets loaded
  • use environment variables so they can map to different systems correctly.
  • target git/bin/bash.exe to avoid spawning off additional processes which saves about 10MB per process according to Process Explorer compared to using bin/bash or git-bash

I have my configuration that uses Scoop in https://gist.github.com/trajano/24f4edccd9a997fad8b4de29ea252cc8

How do you copy and paste into Git Bash

Press Insert.

Also, to copy from the window, try clicking the console's window icon (topleft) and choosing Edit -> Mark, then drag a box on the text, then press Enter. (You can also paste via the window icon menu, but the key is faster.)

UPDATE

Starting from Windows 10 the CTRL + C, CTRL + V and a lot of other feature are implemented in conhost.exe so they should work with every console utility on Windows. (You have to enable Properties -> Option tab -> Quick Edit Mode)

Ref: http://blogs.windows.com/buildingapps/2014/10/07/console-improvements-in-the-windows-10-technical-preview/

How to add a open git-bash here... context menu to the windows explorer?

I had a similar issue and I did this.

Step 1 : Type "regedit" in start menu

Step 2 : Run the registry editor

Step 3 : Navigate to HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell. If you don't have the shell key, create one.

Step 4 : Right-click on "shell" and choose New > Key. name the Key "Bash"

Right click on "shell" and choose New > Key. name the Key "Bash"

Step 5 : Modify the value and set it to "open in Bash" This is the text that appears in the right click.

Sample Image

Sample Image

Step 6 : Create a new key under Bash and name it "command". Set the value of this key to your git-bash.exe path.

Sample Image

Sample Image

Sample Image

Close the registry editor.

You should now be able to see the option in right click menu in explorer

PS Git Bash by default picks up the current directory.

EDIT : If you want a one click approach, check Ozesh's solution below

Opening up Windows Terminal with elevated privileges, from within Windows Terminal

Currently you cannot open an elevated wt.exe session from the command line without workarounds. Workarounds include using gsudo, Using Task Scheduler (I tested this one and it works but you need to use the full path to wt.exe and you can skip the shortcut creation step) OR if you are ok with a keyboard shortcut, the simplest way; using a keyboard shortcut to run Windows Terminal as Admin from the taskbar.

For your use case:

For my specific instance, I simply want to make it simpler to pop open
an admin terminal, I don't need a way to elevate arbitrary commands,
then I will happily use the commands I have already shown here.

The simplest approach will work:

Pin Windows Terminal as the first item on the task bar. Then hit Win+Ctrl+Shift+1 to open it as admin.

If you really must launch Windows Terminal from the command line from within Windows Terminal then create a task in the Task Scheduler:

  1. Give the task a name, check 'Run with highest privileges'.
  2. Choose the 'Actions' tab, click 'New', select 'Start a program' as the action. Put the full path to wt.exe in the 'Program/script field'. Click OK. Click OK again.
  3. Click 'Conditions' tab, uncheck "Start the task only if the computer is on AC power".
  4. Click 'Settings' tab, make sure "Allow task to be run on demand" is checked and uncheck "Stop the task if running for longer than".
  5. Finally in your shell (Powershell), launch an elevated Windows Terminal session by running the command: schtasks /run /TN "TASK_NAME" where TASK_NAME is the name you gave the task in step 1.


Related Topics



Leave a reply



Submit