Skip to content

Instantly share code, notes, and snippets.

@alanxoc3
Created April 10, 2018 08:56
Show Gist options
  • Save alanxoc3/c242b895959f740c9c707ec067f95fcd to your computer and use it in GitHub Desktop.
Save alanxoc3/c242b895959f740c9c707ec067f95fcd to your computer and use it in GitHub Desktop.
Dmenu alias integration with xterm!
#!/usr/bin/bash
# By: Alan Morgan
# Don't you love my obfuscated perl code?
perl -e '
sub uniq { my %seen; grep !$seen{$_}++, @_; }
$dmenu=`dmenu_path`;
@dmenu=split "\n", $dmenu;
$aliases=`bash -i -c alias`;
@aliases = $aliases =~ /^alias ([^\=]+)\=.*$/mg;
%aliases = map { $_ => 1 } @aliases;
@all_cmds = sort (uniq( @aliases, @dmenu ));
$all_cmds = join "\n", @all_cmds;
print $all_cmds' | /usr/bin/dmenu "$@" | perl -e '
sub trim { return $_[0] =~ s/^\s+|\s+$//rg; }
$aliases=`bash -i -c alias`;
@aliases = $aliases =~ /^alias ([^\=]+)\=.*$/mg;
%aliases = map { $_ => 1 } @aliases;
$cmd = "";
while (<>) { $cmd=$_; }
$cmd = trim($cmd);
if (exists $aliases{$cmd}) { $cmd="xtermcmd $cmd" }
print"$cmd";' | ${SHELL:-"/bin/sh"}
#!/bin/bash
# For running commands on xterm startup.
# The newline is needed, because bash does weird things with aliases. Bash
# needs a line before the shopt command takes effect. That was a wild goose
# chase!
# http://www.delorie.com/gnu/docs/bash/bashref_72.html
xtermcmd() {
xterm -e "bash --rcfile <(echo -e \"shopt -s expand_aliases; . ~/.bashrc;\n$*\")"
}
xtermcmd "$*"
@alanxoc3
Copy link
Author

So, I really wanted aliases to work with dmenu and for my aliases to pull up xterm when you run it through dmenu. I got it working after quite a few hours. There were some little quirks with xterm that I had to get by. I would have used this script, but it is way outdated and didn't work 🙁.

You can modify this gist to work with any terminal emulator. To do so, just replace the xtermcmd near the bottom of the dmenu_alias file with your command to open up a terminal emulator (with alias support from opening it up).

And to install, just make sure you put both these scripts in your path (if you aren't using xterm, then you only need the dmenu_alias file. Reply if you got questions/feed back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment