How can I set up autocompletion for Git commands? - linux

I have Git (version 1.7.2.5) bash compeletion working on my Debian squeeze (6.0). Git was installed with aptitude and I am using standard debian's bash, which supports command line autocompletion.
Now, I just installed Git (1.5.6.5) on an other machine (Lenny/Debian 5.0) and the there is no autocompletion.
Why is Git autocomplete not working on the second machine? How do I diagnose this?
What is making completion work on my machine? I have looked for the file git-completion.bash but it doesn't seem to be on my machine. How does Git complete ever work?
How can I bring git complete to the other machine?

You need to source /etc/bash_completion.d/git to enable git auto-completion.
In my .bashrc it's done with:
for file in /etc/bash_completion.d/* ; do
source "$file"
done

For Debian Squeeze (6.x):
Put the following lines in your ~/.bashrc
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
The script/program /etc/bash_completion already includes the scripts in /etc/bash_completion.d and also defines some functions needed by the included scripts.
For Ubuntu Bionic and up (^18.04):
Put the following lines in your ~/.bashrc
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
For Fedora 20 & macOS High Sierra
See comments

You need to install this package if missing. And then logout and login.
apt-get install bash-completion

The shortest way to activate the bash auto-completion for Git on Debian is to add
source /etc/bash_completion.d/git
to the ~/.bashrc (and restart the terminal).
See also here: "Pro Git" -> 2.7 Git Basics - Tips and Tricks -> Auto-Completion.

Get the git autocompletion script:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
Add to your .bash_profile in home directory:
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
Source your .bash_profile after this like:
. ~/.bash_profile

For Manjaro and other Arch-based distros. I know it's about debian, but most things are the same but sometimes not. Whatever OS you use you'll end up here.
In your ~/.bashrc add:
source /usr/share/git/completion/git-completion.bash
And then in terminal
$ source ~/.bashrc

For Ubuntu/Debian
Install Git and bash-completion by the following command:
sudo apt-get install git bash-completion
I don't think you need to do anything else.

Use Notepad++ to edit your ~/.bashrc file.
Put the line at the bottom of the script with a # at the beginning of the line. Save the file. For example:
# source C:\cygwin64/etc/bash_completion.d/git
Don't forget to put the entire file path after 'source' and in front of '/etc/' For example, my cygwin64 folder which contains the 'etc' folder is in my c drive so my file path is c:\cygwin64/etc therefore the line I included in my bashrc file is:
# source c:\cygwin64/etc/bash_completion.d/git
Save bashrc file. Open Cygwin Terminal ... Boom! It's go time.
I then entered the following command and it worked.
git clone git:\/\/github.com/magnumripper/JohnTheRipper -b bleeding-jumbo JtR-Bleeding

Recent versions of Ubuntu (observed on 20.04) seem to have split completions into multiple paths. For Ubuntu 20.04, I had to add the following to my .bashrc (taken from the default bashrc found in /etc/bash.bashrc):
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi

At times git auto-complete disappears because you accidentally deleted your ~/.bashrc file. Check if the bashrc file is there in your home directory. If not, you can always copy it from:
/etc/skel/.bashrc

Related

Alias Multiple Commands in Linux

I am managing a website using git. One of the requirements for the git repository is that bare = true. It uses a post-receive hook to manage pushes from my local computer. The problem is that sometimes I would like to make changes to a WordPress directory on my website using the wp-admin view online. So then I would just ssh into the directory and run git --work-tree="BLAH" add . and git --work-tree="BLAH" commit -m "BLAH". Is there a way to set up an alias, like alias git="git --work-tree=\"BLAH\"" and have that work for all git commands?
There are times when alias are a great tool. Then there are times when things start getting too complicated where a shell script is better.
To create a single command that executes other commands just create a file (maybe call it git-add-all) then type the following:
#! /bin/bash
git --work-tree="BLAH" add .
git --work-tree="BLAH" commit -m "BLAH"
Then you can run the script by simply doing:
bash git-add-all
Even better, make the script executable:
chmod +x git-add-all
Then you can use it like any command:
./git-add-all
Advanced tips:
To be able to run the script from any git directory you can copy/move the file to one of the directories in your $PATH. For example /usr/loca/bin. Then you can simply run git-add-all instead of ./git-add-all.
Even better is to create your own personal scripts directory and include it in $PATH. I personally use ~/bin. To add the directory to $PATH you just need to add the following to .bashrc or .profile:
export PATH=/home/username/bin:$PATH
or if you're doing this for the root user:
export PATH=/root/bin:$PATH
In case anyone is curious how I solved it (thanks to shellter's comment), I wrote a bash script then prompted the user for input like so:
#!/bin/bash
function fix {
git --work-tree="PATH_TO_WORKING_TREE" $1
}
echo -n "git "
read -e INPUT
until [ "$INPUT" = "quit" ]; do
fix $INPUT
echo -n "git "
read -e INPUT
done
Running it:
user#server [repo.git] $ git-fix
git status
# On branch master
nothing to commit (working directory clean)
git quit
There is a .bashrc file in Linux. You can edit it for creating alias for your favorite and frequently used commands.
To create an alias permanently add the alias to your .bashrc file
gedit ~/.bashrc
The alias should look like:
alias al='cmd'
You can read more about it over here.

linux: Changing default programs

I'm on a RedHat server as a --user
my github default is out of date (1.7.1)
>>which git
/usr/bin/git
However there's another git on the server
/appl/git/2.6.1/git
How do I change my default "which git" to refer to the one in the /appl folder every time I login?
I have tried(DOESNT WORK):
>> cd ~
>> vim .profile
export PATH=/appl/git/2.6.1:$PATH
Then reopen my terminal
Add the folder /appl/git/2.6.1 to your PATH variable, before /usr/bin.
Another alternative would be to prepend your ~/bin folder to the path and add a symlink to the git executable there:
ln -s /appl/git/2.6.1/git ~/bin/git

How can I use a dot file in a bash script unix

I am trying to install git's autocomplete (and I do not want to use Homebrew to do this), so I placed the following in my bash_profile
if [ -f ~/.git-completion.bash ]; then
source ~/.git-completion.bash
fi
The .git-completion.bash is an install script I donwload from github, specifically:
https://github.com/git/git/raw/master/contrib/completion/git-completion.bash
The .git-completion.bash file exists, I can see it when I run ls -la however, every time I open a terminal window, I see
-bash: /Users/Username/git-completion.bash: No such file or directory
(I do not know why it does not have a "." in that error)
I would appreciate any guidance on how to fix this without homebrew. Thanks

Virtualenv: workon command not found

I have installed virtualenv and the virtualwrapper via apt-get, I got to a point where I created a virtual enviroment but however later on during that same day when I used the workon command it was not found. I further on went and inspected my home directory and .virtualenvs dir and the virtualenv I created earlier were still there.
Solving this problem took two steps:
Add this to your .bashrc / .bash_profile / .zshrc:
# load virtualenvwrapper for python (after custom PATHs)
venvwrap="virtualenvwrapper.sh"
/usr/bin/which -s $venvwrap
if [ $? -eq 0 ]; then
venvwrap=`/usr/bin/which $venvwrap`
source $venvwrap
fi
Then use:
source .bash_profile
# or .bashrc / .zshrc
to reflect the changes.
Additionally, if the terminal still sometimes cant find workon, use source .bash_profile to reset and find it again.
type source .profile in home directory from terminal.
Read the readme in the top of which virtualenvwrapper.sh
You need to source it inside bashrc
open ~/.profile
cd ~
nano .profile
add at the end
#virtualenvwrapper setup
export WORKON_HOME=$HOME/envs
export PROJECT_HOME=$HOME/dev
source /usr/local/bin/virtualenvwrapper.sh
to load your .profile file you just edited:
$ . .profile
I ran in to this problem too and I simply needed to logout and log back in.
This read in the changes which the debian package manager made to my system at /etc/bash_completion.d/virtualenvwrapper

How to change rvm install location?

Currently I have rvm installed under /home/john/.rvm
How do move rvm to the location /opt/local/rvm ?
I tried to use the rvmrc file to take care of this (according to the rvm document here: https://rvm.io/workflow/rvmrc/ ) but no luck, I found a rvmrc file under /etc/
Is there any step by step instruction I could follow?
Figure out myself by doing some research online, hope this can help someone out:
Suppose you want to move from home/username/.rvm to /opt/local/rvm
Update you .bash_profile or .bashrc to:
if [ -s "$HOME/.rvmrc" ]; then
source "$HOME/.rvmrc"
fi # to have $rvm_path defined if set
if [ -s "${rvm_path-$HOME/.rvm}/scripts/rvm" ]; then
source "${rvm_path-$HOME/.rvm}/scripts/rvm"
fi
find your .rvmrc under $HOME/.rvmrc, if not found, create one and put this line in it:
export rvm_path=/opt/local/rvm
move your file to the new location : mv $HOME/.rvm/* /opt/local/rvm
execute rvm repair all and rvm reload
And you are good to go!
You can do
curl -sSL https://get.rvm.io | bash -s -- --path /path/you/want/rvm/installed/to

Resources