Building a basic command palette for Bash

I’ve been using tmux for a while at work, and in particular nested tmux sessions, one for each server I use regularly handled via a main session on a jump host. There are around 20 different servers I connect to for various tasks and navigating between them all via standard methods was a little tricky. I thought of the idea (which, of course, wasn’t a new thought) of having a fuzzy find so to get to box4, I could type x4<Enter>.

I had a look for anything that existed already and found marker and fzf which seemed really promising. Unfortunately, the servers I’m using didn’t meet the requirements so I wondered how tricky making something like this would be.

Continue reading “Building a basic command palette for Bash”

bash-todo: A TODO list in your terminal window

I recently saw this question posted in /r/bash and thought “that’s something I’d also use”. After seeing /u/whetu‘s comment and thinking about it, I realised it should be fairly easy to build a script that does exactly what’s required.

After a bit of tinkering and adding some basic styles I wrapped it up into a small Bash/Perl script (a polyglot, just for fun…) and placed it on GitHub. I’ve added a few basic switches to toggle/delete items, or slurp items from STDIN.

As part of this I also repurposed parts of my bash-ps1 tool so that you can easily create style strings including the colours and styling to help make your TODO list look just the way you want it. It’s available here and the source code is also on GitHub.


terminal-preview is a collection of JavaScript and CSS that will enable parsing and styling of terminal ANSI escape codes for simple preview. It’s been developed as a result of my experimenting with bash-ps1 and image-to-ansi.

I’d wanted to extract the code I used in the those projects into a stand-alone project/component that could be added to an existing page where ANSI escape sequences need to be interpreted. I’ve toyed with the idea of making it a Web Component or something like that but haven’t just yet.

Continue reading “terminal-preview”

Poor man’s __git_ps1

Recently one of the remotely maintained servers I worked on regularly has started to exhibit slowdowns in bash. It started only occasionally, perhaps as a result of a high number of users or long-running script, but become a lot worse and resulted in a few seconds delay every time a new prompt was drawn. A few seconds may not sound a lot, but can be incredibly frustrating!

Upon a bit of an investigation I found that the culprit was the call to __git_ps1 in my $PS1 variable. Removing this made the bash prompt speedy again! Hooray! However, having the current branch on my screen immediately is infinitely useful and I can’t really live without it any more, so I carried on digging.

On this particular set of servers git has been replaced with a set of custom script that perform a lot of tasks. This means that calls to simple git commands run a LOT of other commands, logging and getting repo information among other things. I thought about replacing all the references to git in the file with the direct path to the binary, but thought it shouldn’t be too tough to just get the information myself using the information in the .git folder.

The result is this bash function.

It’s fairly primitive, it checks the current path for a .git directory, if it doesn’t find one, it goes up one level, until either finding one, or hitting the root. It printfs the result in the same way as the real __git_ps1 and handles the basic states like |MERGING although doesn’t detect a detached HEAD any other similar states, but serve the main purpose, now I can correctly see my current branch and avoid committing the wrong thing to the wrong place!

Feel free to share if this is useful and let me know if there are any minor improvements (short of using the full version!).

Dissecting your bash prompt

I’ve always noticed really elaborate examples of what can be achieved with a highly customised PS1 variable for your bash prompt and recently have spent some time playing with my settings to make my terminal experience better with a few updates. I also felt that I would have benefitted in the past from being able to analyse what my prompt currently does, breaking it down into easier to digest blocks, so I can make more sense of what each part does.

As a result of that curiosity, I’ve created a small page that allows you to paste in an existing PS1 from your terminal which will then be parsed and previewed with some basic dummy details (which can be changed by clicking on the identifier in the preview area).

I’d like to make it into something like EzPrompt with support for 256, or even true colour additions and collate some useful scripts for displaying additional information, but at the moment it’s useful for seeing what each block does at least.

It seems to cover most of the examples I’ve tested in various searches, but the more inline-code is included, the less it can display.

Check it out here and the source is available on github.

Convert an image to ANSI escape codes

I’ve been working on cleaning up my dotfiles recently and have been playing with my PS1 and the available colours. In doing so I thought it might be interesting to extract image data from an uploaded image and turn the result into a nearest-match ANSI image. This is the result! It supports both 256 colour and true colour terminals and can utilise unicode code points to present a clear image in slightly fewer pixels as well as just using background colours and spaces if preferred.

It includes a live preview which I’ve lifted from the bash PS1 parser.

Check it out here!

Breaking down large CSV files

Today I received a 45Mb CSV file for importing into a database… Needless to say the application we were importing to didn’t seem to like the size of the file, for what ever reason… So I knocked up a quite bash script to create smaller ‘chunks’ defined as a number of lines, to make importing simpler.

I’m sure there’s many way in which is can be simplified, so if you know any I’d like the contributions!

It’s run like this:

$ ./ large-data.csv 5000

The first argument being the filename and the second argument the maximum number of lines for each ‘chunk’. From that 45Mb megalith, 38 files of around 1.2Mb were produced which didn’t seem to break the other end!

Continue reading “Breaking down large CSV files”

MySQL backup script

I’ve recently been learning more and more about bash scripting and the cool functions and features that are included, that i’ll probably never use…

I recently had to set up a cron to backup all MySQL databases but I didn’t want them in one huge file.

So I’ve written a little script, that is probably not entirely efficient, but serves it’s purpose for us:


# get todays date
DATE=`date +"%F"`;

# go to the backup directory
cd /var/backup/mysql

# export the databases, each to it's own file
# the first echo is sent to mysql, so we're basically echoing SHOW DATABASES; to mysql
# then we're chopping off the first line (sed 1d) and passing the remaining input to
# xargs which accepts \n or space delimited arguments as a list and echo's out the
# string mysqldump -u  -p % > %.sql (replacing % with the name) to
# /bin/bash
echo "SHOW DATABASES;" | mysql -u  -p | sed 1d | xargs --replace=% echo "mysqldump -u  -p % > %.sql" | /bin/bash

# compress the backups
tar czf mysql_backup_$DATE.tar.gz *.sql

# remove the large .sql files
rm *.sql

This was tested on a SuSE 11 server, might need some tweaking on other dists.