The thing is that sometimes I type cd by mistake and that take me to the home directory.
e.g. I'm in a directory that have a hidden directory and a visible directory, I quickly press cd+tab and that takes me to the home directory
The thing is that sometimes I type cd by mistake and that take me to the home directory.
e.g. I'm in a directory that have a hidden directory and a visible directory, I quickly press cd+tab and that takes me to the 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.
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 "$@"
}
ncd (for "New cd"), and leave real cd alone?
– waltinator
Oct 22 '18 at 23:21
cd (blank) works differently for people besides the home user of ~/.bashrc. Could you give an example or two what to watch out for? e.g. A developer uses cd (blank) in their installation script. (Hypothetical but I can't think of a case). I can roll up your examples into the answer and suggest the OP use ncd all the time instead of cd.
– WinEunuuchs2Unix
Oct 23 '18 at 23:23
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.
Here's how I put the current dir and user in my windows title - You can adapt it to your need, but cd -, equivalent to cd $OLDPWD is a better solution.
From my ~/.bashrc:
# from the "xttitle(1)" man page - put info in window title
update_title()
{
[[ $TERM = xterm ]] || [[ $TERM = xterm-color ]] && xttitle "[$$] ${USER}@${HOSTNAME}:$PWD"
}
cd()
{
[[ -z "$*" ]] && builtin cd $HOME
[[ -n "$*" ]] && builtin cd "$*"
update_title
}
The problem here is not cd, and it's not fixed by technology.
The problem is you, and it's fixed by patience!
If you frequently find yourself typing and submitting commands that you did not want, practice slowing down. Take a breath, read what you're typing, and double-check it before pressing enter. Think it through. Don't rush.
You'll find that this approach not only solves the problem at hand, but other far worse problems that you are going to encounter if you continue down your current path.
cdsomewhere by accident, usecd -to return to your previous location -- bash keeps the$OLDPWDvariable for this purpose. See https://www.gnu.org/software/bash/manual/bash.html#index-cd – glenn jackman Oct 22 '18 at 16:48cdto do nothing, you can write a function namedcdthat does nothing when no arguments are given, otherwise callbuiltin cd "$@"– glenn jackman Oct 22 '18 at 16:50cddoing whatever it does. If you keep the habit of executing commands you haven't double checked, you'll get into much bigger troubles later on. E.g. you want to move two files into a third directory:mv a b dir/and TAB completion doesn't producediras you expect, you'll end up executingmv a bwhich overwritesb. Learn to be careful, learn to take a look at the command before pressing Enter. – egmont Oct 22 '18 at 20:00pushdinstead ofcd, as it won't change directories when nothing's given. I'd avoid changing the behavior ofcd, as I worked in academic computing in college, and engineering students were always surprised to find thatrm *didn't prompt them for what to delete (because the engineering school had aliasedrmtorm -i) – Joe Oct 23 '18 at 01:08rmto prevent deleting of top level directories such as/,/etc,/usr,/home,/var, etc. without a password override. Sometimes we need protection from ourselves :) – WinEunuuchs2Unix Oct 23 '18 at 23:35