Suppose you have to write several commands all beginning with 'python manage.py' but you do not want to write those words each and every time. How do you create an alias where 'python manage.py' is shortened to two letters? So what I would like is to input 'pm'. And then I can input a command such as 'runserver'. This would then activate: 'python manage.py runserver'? Thanks in advance.
Asked
Active
Viewed 969 times
1
-
1Put alias pm='your commands' in your .bash_profile in thw user directory, then command source .bash_profile – Script Kitty Nov 05 '17 at 22:14
-
See eg https://stackoverflow.com/questions/8967843/how-do-i-create-a-bash-alias – Izaak van Dongen Nov 05 '17 at 22:16
-
Although I'm not entirely convinced you need an alias for this.. I usually find tab completion and cycling through previous commands suffices (especially if you have something like vim keybindings to make use of). I feel like aliases should be *generally* applicable.. – Izaak van Dongen Nov 05 '17 at 22:23
1 Answers
0
Create an alias with the name 'pm'
Open .bash_profile from your home folder. Create one if not present
Append
alias pm='python manage.py'
to the file.
Type . .bash_profile
to load the changes

anonymous
- 390
- 2
- 12
-
Aliases are quite limited compared to shell functions. If you use `pm() { python manage.py "$@"; }` instead, then you can export it to child processes with `export -f pm`, and you can extend it to have more complex logic. – Charles Duffy Nov 05 '17 at 23:19
-
1...for instance, if you want `runserver` to be the default command, you can't do that with an alias, but with a function, you could write `pm() { (( $# )) || set -- runserver; python manage.py "$@"; }`, such that `pm` will run `python manage.py runserver`, but `pm shell` will run `python manage.py shell`. – Charles Duffy Nov 05 '17 at 23:20