Missing linux shell on W7? Getting friends with Msys2

Are you missing the power of (l)inux shell at you fingertips?

Of course, running Linux would be the best thing — but some of us are stuck on corporate machines, or don’t have the time to be our own sysadmin’s.

Installling Msys2 is fairly straightforward — but you have to type some pretty cryptic commands to get there. But if you’re longing for the linux-shell I guess this feeling should be somewhat familiar.

Understanding and tweaking Msys2

Msys2 makes a directory structure under where you choose to install it. Say you install it at c:\myprograms\msys2, then your home directory will be at c:\myprograms\msys2\home\<username>\

To navigate your file structure, you do cd /c or cd /c/data/whatever. cd with no arguments takes you back to your home directory.

Quirks and fixes

My biggest quirk was getting network connections right on my laptop. When connected to the corporate network I need to get the proxy settings rigth — but not when I’m at home, or at any of the other networks.

Adding a few lines to .bashrc did the trick:

if [[ "$(ping -n 1 internal.corporate.webservice | wc -l)" -gt 1 ]]; then
    export http_proxy=proxy.my.corporate.net:8080
    export https_proxy=$http_proxy
    export ftp_proxy=$http_proxy
    export rsync_proxy=$http_proxy
    export rsync_proxy=$http_proxy
    export no_proxy="localhost,,localaddress,.localdomain.com"

I first fire away a single ping request to a service which is accessible only within my corporate firewalls. If it fails, ping’s terse error message is one line only. So if the test fails I’m not at my corporate network and (usually) won’t have to do anything.

If I get more than one line from my single ping I am at our corporate network, and need to use our corporate proxies for internet connection.

Tweaking the bash shell

You should have a look at the file .bashc in your home folder. It already has a lot of the tweaking required to make bash suit your needs. Uncomment the options you love the most —  this is my pick:

# If not running interactively, don't do anything
[[ "$-" != *i* ]] && return

# show differences in colour
alias grep='grep --color'              
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'

# classify files in colour
alias ls='ls -hF --color=tty'
alias dir='ls --color=auto --format=vertical'
# long list
alias ll='ls -l'
# all but . and ..                              
alias la='ls -A'                              
alias l='ls -CF'

# Tab completion cycles through alternatives
bind TAB:menu-complete

Search history

Adding these changes to the the .inputrc file gives you the power to search history.  Most likely, you have soome default setting for show-all-if-ambiguous and completion-ignore-case, so you should review and edit and not just paste into the tail of the .inputrc file. 

"\e[A": history-search-backward
"\e[B": history-search-forward

ls and arrow up now will now iterate over any ls – command you’ve added previously. At least in w7 – can’t get it to work at w10 (yet). 

Thanks for this tip to http://markjacobsen.net/2016/04/improved-linux-command-history/

You may also consider adding these to the .inputrc (not sure if the same can or should be achieved through settings in .bashrc)

set show-all-if-ambiguous on 
set completion-ignore-case on

If there’s more than one possible completion when you autocomplete with tab, the first setting will list all of them.

The second will tell autocomplete to ignore case when looking for competions.  This is how windows treat file names anyhow.

The daunting Pacman

If you survived the installation instructions you’ll probably have noticed that Msys2 uses the package manager Pacman to administer which programs are installed as part of msys2. For novice users, finding your way through the pacman documentation is quite … daunting and intimidating.

The hardest part about pacman is actually  finding the packages you want to use. I found browsing for available packages and repositories really, really  tedious and painfull. Luckily, the pacman search function is good. So in the MSys-shell you do:

pacman -Ss <search phrase>

and then finally

pacman -S <package name>

Note that you typically don’t need to include version info in <package name> . To install grep I simply typed

pacman -S grep

and then accepted the proposition proceed to installation. 

Pin to start menu and desktop

Creating a shortcut to <your installation folder>\msys2\msys2_shell.bat will give you you one-click startup of this wonderful shell. However, different windows-version have different opinions about how you add stuff to the start menu, your most used programs and the taskbar.

Windows 10: I’ve only been able to create this shortcuts to the desktop, and NOT the taskbar or start meny.

Windows 7: Adding to menu bar and/or start menu is straightforward. (Pro-tip: manipulate the icon of the shortcut, there is a «change icon» button there, you may have to manually input the path to <your installation folder>\msyss\msys.ico ).

What doesn’t work

Working interactively with python sort of works for super simple stuff (use python -i), but is painfull: I cant use the arrow keys to navigate the python history. I think this is why.  Instead I highly recommend installing anaconda, which has the editor spyder built-in (also available separately). Another good editor is IeP. Or you can use the regular windows command line utility.

Diff: If pacman doesn’t have your favorite utility you may have to install that separately. I had to do that with diff (see below)

Manually installing windows command line utilities

Curiously, I could not find a pacman-package for diff. Instead, I installed GnuWin32 diffutils.  Download the binaries, which contains the setup program diffutils-2.8.7.exe.  Run the installation program, then add the destination folder/Gnuwin32/bin to the windows PATH environment variable. (Here’s just one of very many tutorials on how to do this.)

What works:

ssh including key generating and sharing (ssh-copy-id). Scp works just fine, and I assume sftp and the rest of ssh-related stuff will work as well.

well… most of the stuff I’ve tried works. Except the popular have-multiple-bash-sessions-in-one-terminal program screen.

Cutting and pasting magic

Any text that is selected in the command window is also copied to the windows clipboard, automagically. You must use the mouse to select text, but there is a multiple-click to select the whole word or the whole line. This GREATLY speeds up the selection, just click many times to iterate through the selections (none, word, line). If none of those rock your boat you can always do the click-drag thing.

Pasting is done with a  singleright-click on the mouse.

I thought it would be awkward to shift from the highly automated CTRL-C, CTRL-V combination to the click-rigthclick thing inside the shell, but that worked out pretty smooth pretty quick (for me). Now I want all other windows applications to behave like this!