Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

September 12, 2020

Git Bash Prompt Showing Date and Time on Windows

Sometimes I have Git Bash open for several days, and knowing when I executed a particular command is helpful. Here's a quick-and-dirty way of showing the date and time in the bash prompt, which came about from looking at C:\Program Files\Git\etc\profile.d\git-prompt.sh and doing echo $PS1.

  • Copy the following content into a text editor and save it as .profile in the %USERPROFILE% folder:
PS1="\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[94m\]\D{%m/%d %H:%M:%S} \[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]"'`__git_ps1`'"\[\033[0m\]\n$ "
MSYS2_PS1="$PS1"
  • Run source .profile to reload the prompt.

For the full list of configurable colors, refer to ANSI Color Escape Codes.

So how did I know to create the .profile file? There's a .bash_profile file in the %USERPROFILE% folder:

# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Windows Terminal

Lately, I've been trying out Windows Terminal with oh-my-posh for PowerShell (based on the post by Scott Hanselman). You can also host cmd, bash (via wsl), etc., in Windows Terminal... I fear my days of using plain cmd and Git Bash on Windows will come to an end soon, though I'll surely miss the start up time of cmd.

December 4, 2019

Setup Bash "ll" Alias in Windows

I'm used to ll more than ls. To get it working for bash in Windows (not WSL, but for bash that's usually installed in Windows, such as with git), follow the steps below:

  1. Start a bash shell prompt, e.g., via VS Code terminal or Git Bash prompt.
  2. Set the current directory to the your home directory (if not set already):
cd ~
  1. Add the alias to .bashrc:
echo "alias ll='ls --color=auto -alF'" >> .bashrc
  1. Restart the bash shell, and ll should now work. Here's an example:
$ ll
total 43775
drwxr-xr-x 1 dusklight 1049089        0 Nov 19 11:26  ./
drwxr-xr-x 1 dusklight 1049089        0 Dec  4 20:33  ../
drwxr-xr-x 1 dusklight 1049089        0 Nov  7 11:10  assembly/
-rwxr-xr-x 2 dusklight 1049089    67072 Jan  1  2018  bfsvc.exe*
drwxr-xr-x 1 dusklight 1049089        0 Jan  1  2018  Boot/
-rw-r--r-- 1 dusklight 1049089    67584 Dec  4 20:31  bootstat.dat
drwxr-xr-x 1 dusklight 1049089        0 Jan  1  2018  debug/
-rw-r--r-- 1 dusklight 1049089     8143 Nov  1  2018  setup.log
...

When you restart the shell, you might see a warning about .bash_profile not being found. If it was Git Bash, it will create it for you automatically.

To have colors for ls as well, create a separate alias for ls as ls --color=auto then set the ll alias after that with just -ilF parameters. For additional information, search for "default Ubuntu .bashrc".

For more ls options, check out the man page.