How do I prevent 'cd' command from going to home directory?
Use gedit ~/.bashrc
and insert these lines at the bottom:
cd() {
[[ $# -eq 0 ]] && return
builtin cd "$@"
}
Open a new terminal and now when you type cd
with no parameters you simply stay in the same directory.
TL;DR
If you want to be really elaborate you can put in a help screen when no parameters are passed:
$ cd
cd: missing operand
Usage:
cd ~ Change to home directory. Equivelent to 'cd /home/$USER'
cd - Change to previous directory before last 'cd' command
cd .. Move up one directory level
cd ../.. Move up two directory levels
cd ../sibling Move up one directory level and change to sibling directory
cd /path/to/ Change to specific directory '/path/to/' eg '/var/log'
The expanded code to accomplish this is:
cd() {
if [[ $# -eq 0 ]] ; then
cat << 'EOF'
cd: missing operand
Usage:
cd ~ Change to home directory. Equivelent to 'cd /home/$USER'
cd - Change to previous directory before last 'cd' command
cd .. Move up one directory level
cd ../.. Move up two directory levels
cd ../sibling Move up one directory level and change to sibling directory
cd /path/to/ Change to specific directory '/path/to/' eg '/var/log'
EOF
return
fi
builtin cd "$@"
}
If it's tab completion that's causing this, one option is to make the completion cycle through entries immediately. This can be done using readline's menu-comple
option instead of the default complete
:
bind 'tab: menu-completion'
Then, in my home directory, for example:
$ cd <tab> # becomes
$ cd .Trash
Of course, even then you'd have to read what you're executing.