importing aliases from bashrc etc

25 views
Skip to first unread message

Hamish

unread,
Apr 3, 2008, 4:18:19 PM4/3/08
to Hotwire Shell
Started writing some code to import my aliases from my .bash_aliases
file. Not sure where it should be hooked into the code, and there are
a few issues to think about. In particular this first cut of code
assumes that sys is the correct thing to prepend to the command. But
if it is a builtin (such as ls) then which should we use? I'd also
need to check for the commands that need to be run in a term (is there
some way to share that info with the current set?). Then there are
aliases that use bash variables (eg $EDITOR) ...

Do folk reckon this is worth persuing? I think it would be pretty
cool to try out hotwire and have your usual aliases working as if by
magic :)

All feedback appreciated.

--- CODE STARTS ---
import re
import os.path
from hotwire.cmdalias import AliasRegistry

# this regex works for
# alias ls='ls -l'
# style aliases (good for bash and zsh)
bashalias_re = re.compile ("""
^\s* alias \s+ # find the alias at the start
([-a-zA-Z0-9_.]+) # the name of the alias
\s*=\s* # separator from command
(?: # which is in one of these formats:
'([^']+)' # the command in single quotes
| # or ...
\"([^"])\" # the command in double quotes
| # or ...
(\w+) # the command as a single word
)
\s*$
""",re.VERBOSE)

# this regex works for
# alias tcsh '/usr/bin/tcsh -l'
# style aliases (good for tcsh and csh)
cshalias_re = re.compile ("""
^\s* alias \s+ # find the alias at the start
([-a-zA-Z0-9_.]+) # the name of the alias
\s+ # separator from command
(?: # which is in one of these formats:
'([^']+)' # the command in single quotes
| # or ...
\"([^"])\" # the command in double quotes
| # or ...
(\w+) # the command as a single word
)
\s*$
""",re.VERBOSE)

# need to add extra fro tcsh ...
filelist = [ "~/.bashrc", "~/.bash_aliases", "~/.zshrc" ]

for filename in filelist:
filename = os.path.expanduser(filename)
if not os.path.isfile(filename):
continue

for line in open(filename):
# TODO: need to decide which regex to use here
m = bashalias_re.match(line)
if m:
# make into a hotwire alias
name = m.group(1)
target = 'sys ' + m.group(m.lastindex)
# print "name: " + name + " target: " + target
# TODO: do we want to check the alias for possible
conflict with
# builtin command? For commands that need term rather
than sys ...
AliasRegistry.getInstance().insert(name, target)

Hamish

unread,
Apr 4, 2008, 12:42:06 PM4/4/08
to Hotwire Shell
Done a little more thinking about this and I'm not sure it would be
good to have this automatically happening every time hotwire starts.
And there seems too much ambiguity to do a good job without user
interaction.

So how about having a menu option (or somewhere in preferences) to
"Import Shell Aliases ..." ? This could

1. bring up a dialog box, with a drop down list for where to import
the preferences from. It would have options
for .bashrc .bash_aliases .zshrc .tcshrc ... and "Other" (which would
allow you to browse for a file).

2. On hitting OK, the chosen file would be parsed, and an aliases file
would be created (lets say ~/.hotwire/plugins/alias.py for now). If
it already exists then the generated aliases would be appended to the
file. If not, some comments about how to manage aliases would be
added to the top of the file.

2a. Ambiguous cases (where there is a shell version and a hotwire
builtin) would default to the shell version (ie prepend "sys ", but
have a comment above the alias to explain this.

2b. Aliases that don't translate well (eg the alias uses an
environment variable such as $EDITOR ) would be commented out by
default, with a comment above the alias to explain why.

2c. Duplicates (for the case where the alias.py file already exists)
would be commented out.

3. The alias.py file would be opened in the users preferred text
editor. They can then sort out the mess we've left them ...

---

We could also have a menu (or preferences) item to manage aliases -
which would be opening the alias.py file in the preferred text editor.

Hamish

unread,
May 21, 2008, 6:28:36 PM5/21/08
to Hotwire Shell
Hello all

Been playing around and have expanded my original code to be a plugin
you can stick in ~/.hotwire/plugins/ and run it to grab your aliases
from whatever file you use, translates them into hotwire speak and
saves them into ~/.hotwire/plugins/alias.py (or wherever it is meant
to go on windows). I've uploaded get_alias.py to the files section of
this group.

Usage is

get_alias [flags] filename

where the filename is the name of the file that contains your aliases
( ~/.bashrc ~/.bash_aliases ~/.zshrc ~/.tcshrc ...) The options are

-b - use bash syntax ( expect an = between the alias text and the
command) - works for bash and zsh
-c - use csh syntax ( expect an = between the alias text and the
command) - works for csh and tcsh
-e - open the alias.py file in your preferred editor so you can
review it
-p - print in the output window and don't write to a file

Don't know how useful this will be, but all feedback appreciated ...
Reply all
Reply to author
Forward
0 new messages