In a console, is it possible to change into a commands "environment"? To explain, I take "git" as an example. While programming and using a git repository, I change to the repos path and then do many "git" commands like "git x ...", "git y ... " etc. So what I want is to somehow change "into" git so that I don't have to type "git" all the time, only "x ..." and "y ...". Or is this a feature you normally wouldn't want?
4 Answers
What you are asking for is a "git shell". There appears to be one out there by the name git-sh
.
There isn't a generalized solution that I know of, but git is not the only program with a custom shell just for running it's own commands. Similar examples might be mysql
, telnet
, ftp
, etc. There are actually quite a number of programs that do have their own built in shell environments and you have the choice of either feeding them commands as arguments or starting them as shells and running the commands internally.
Also useful along these lines is tab completion. A good tab completion library will have all the possible commands and arguments for a program like git at your finger tips.
Note: While "git shell" is the right terminology here, don't get confused by the "git-shell" project that is out there that is designed to replace the system login shell to give limited permissions for git actions only.
-
Ty! With your paragraph in the middle, do you mean that when I am typing "git ..." in the console and then do tab completion it is knowing the commands of git? I already wondered how this works because it does ;) So it is not actually checking the git program for its available commands? Aug 22, 2011 at 15:49
-
@user905686 No. It's possible to parse the usage documents and built tab completion routines, but generally there is a library of possible completions and it just works off of those. If you have the right packages installed this should work for a lot of popular programs, but certainly not all.– CalebAug 22, 2011 at 15:52
You can define git commands as aliases in the shell. You need to make a choice when a git command is also a shell command, e.g. git diff
, git mv
, … The function all_git_aliases_on
makes all git commands available without the git
prefix, while the function unambiguous_git_aliases_on
makes git commands available without the git
prefix only if there isn't a shell command by that name.
The function git_aliases_off
removes these aliases; if you had aliases by the same name as a git command (e.g. alias mv='mv -i'
), they won't be restored, you need to set them again (add their definitions to the git_aliases_off
function).
You may want to call unambiguous_git_aliases_on
from your ~/.bashrc
or ~/.zshrc
, or to give toggle_git_aliases
a short alias such as g
.
git_commands=$(git help -a | grep '^ *[a-z]')
git_aliases=
all_git_aliases_on () {
for x in $git_commands; do
alias $x="git $x"
git_aliases="$git_aliases $x"
done
}
unambiguous_git_aliases_on () {
for x in $git_commands; do
if ! type $x >/dev/null 2>/dev/null; then alias $x="git $x"; fi
git_aliases="$git_aliases $x"
done
}
git_aliases_off () {
unalias $git_aliases
git_aliases=
}
toggle_git_aliases () {
if [ -n "$git_aliases" ]; then
all_git_aliases_on
else
git_aliases_off
fi
}
-
Nice solution! It seems that this IS a generic one, the only thing you have to modify for other programs is maybe the first line, but with a little tweaking you could make a script that generates a respective script like above for a certain program, right? And, when you turn the aliases on like in your script, the original shell commands are still available, are they? Is it possible to turn them off then, too? Then it would be a git shell ;) Aug 23, 2011 at 9:13
You may be able to do this by creating aliases in your ~/.bashrc
file but be careful in the aliases you choose as this could cause you to override other command line programs/scripts.
Here is an example using ls
:
alias l = 'ls -Alh'
So when I type l
on the command line, bash runs ls -Alh
instead. So you could do something like:
alias gx = 'git x'
alias gy = 'git y'
in your ~/.bashrc
then running gx
would run git x
.
I don't know of a generic solution that would work for programs in general though.
I would make a shell emulator
. It's basically a small script that will give you a fake prompt and accept certain input values. For exmaple, it could look like this:
user@host ~$ gitshell
Welcome to git-shell. Type help for command info
>> help
help - display this message
x - do x
y - do y
exit - close the shell
>> exit
user@host ~$
It's not hard to code (use python or perl or something), and once you place it in your $PATH you're set to go. It could be used for git, hg, svn whatever you want. Just an idea, it's what I'd do.
-
I guess that would be better to code using a framework for shells so you just have to provide mappings from aliases to actual commands. The solution using bash aliases would be a simple one. Jun 18, 2017 at 10:39