Last active
December 15, 2015 17:29
-
-
Save MercuryRising/5296375 to your computer and use it in GitHub Desktop.
Easy aliaser for bash. If you keep navigating to directories to run files, easyalias them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
Bash RC aliaser | |
Usage: easyalias aliasName aliasPath | |
Outputs: run source ~/.bashrc for commands to take effect | |
Configuration: If you have a different shell or your bashrc is somewhere other than ~/.bashrc, you'll need to change it. | |
It wouldn't be a bad idea to use ~/.bash_aliases instead of your bash file directly (changing config will do it). | |
Make sure you have the following in your bash config | |
if [ -f ~/.bash_aliases ]; then | |
. ~/.bash_aliases | |
To use: Add this file to somewhere on your bash path. Make executable (chmod +x easyalias.py). | |
Use similar to below - it will resolve ~/ and ./ in paths (~/ is automatically expanded in linux to the user directory), whereas ./ is handled by Python. | |
$ cd ~/Downloads/Sublime\ Text\ 2/ | |
$ easyalias my_time_is_sublime ./sublime_text | |
$ source ~/.bashrc | |
$ my_time_is_sublime | |
**sublime text opens** | |
''' | |
import sys | |
import os | |
# If your bash path is something different, change to absolute path or modify from home | |
bashPath = os.path.expanduser("~/.bashrc") | |
def alias_command(name, destination): | |
aliasLine = '\nalias %s="%s"\n'%(name, destination) | |
with open(bashPath, "ab") as f: | |
f.write(aliasLine) | |
print "Added line: %s to %s.\nRun 'source %s' to enact the changes." %(aliasLine.strip(), bashPath, bashPath) | |
def check_conflicts(wordList): | |
# open up the file in read mode, grab the data | |
with open(bashPath, "rb") as f: | |
lines = f.readlines() | |
conflicts = [] | |
for line in lines: | |
for word in wordList: | |
if word in line: | |
conflicts.append((word, line)) | |
return conflicts | |
if len(sys.argv) == 3: | |
# We're good - we have script name, aliasName, aliasPath | |
aliasName = sys.argv[1] | |
aliasPath = sys.argv[2] | |
print aliasPath | |
# Expand the path if it isn't already | |
if "./" in aliasPath: | |
aliasPath = os.path.abspath(aliasPath) | |
# Unix specific? Paths with spaces are not aliased correctly by bash, must be escaped | |
if " " in aliasPath: | |
aliasPath = aliasPath.replace(" ", r"\ ") | |
# Check for conflicts in the file we're going to append to, just to make sure we don't map over something that's already there | |
conflicts = check_conflicts([aliasName]) | |
print "Mapping {aliasName} to {aliasPath}".format(aliasName=aliasName, aliasPath=aliasPath) | |
if conflicts: | |
conflictsFormatted = "\n".join( (": ".join(pair) for pair in conflicts) ) | |
verify = raw_input("Possible Conflicts:\n{conflicts}\nIs this what you want? > ".format(conflicts=conflictsFormatted)) | |
if "y" in verify.lower(): | |
alias_command(aliasName, aliasPath) | |
else: | |
print "Not what you wanted. Run again with desired aliases." | |
else: | |
alias_command(aliasName, aliasPath) | |
else: | |
print "Usage: scriptName aliasName aliasPath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment