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

  • 30
    If you cd somewhere by accident, use cd - to return to your previous location -- bash keeps the $OLDPWD variable for this purpose. See gnu.org/software/bash/manual/bash.html#index-cd – glenn jackman Oct 22 at 16:48
  • 4
    If you truly want cd to do nothing, you can write a function named cd that does nothing when no arguments are given, otherwise call builtin cd "$@" – glenn jackman Oct 22 at 16:50
  • 2
    Really bad idea: HOME=. – Joshua Oct 22 at 19:48
  • 26
    My sincere best recommendation is to slow down and learn to to check the command before you're executing it. In other words: get used to cd doing 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 produce dir as you expect, you'll end up executing mv a b which overwrites b. Learn to be careful, learn to take a look at the command before pressing Enter. – egmont Oct 22 at 20:00
  • 1
    @Joe I wrote a wrapper script for rm to 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 at 23:35

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 "$@"
}
  • 6
    But then you've made your environment work like no other environment. Why not call the function ncd (for "New cd"), and leave real cd alone? – waltinator Oct 22 at 23:21
  • @waltinator I can improve the answer with a warning about how 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 at 23:23
  • I love customizing things. It's what Linux is about. But I have a strict personal rule never to change the function (no pun intended) of any common commands. This opens up problems when you work on another system or when someone else works on or advises you on yours. I agree with @waltinator and that other "good" Joe – Joe Oct 25 at 7:59

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.

  • 2
    also, after some months/years of usage you will forget about that you changed the behavior and will have problems to work in other environments. – RoVo Oct 23 at 14:35
  • @RoVo That too, good point! – Lightness Races in Orbit Oct 23 at 14:44
  • @RoVo That can happen in a few days. Right away, if you dual boot, switch to a VM ... – Joe Oct 25 at 8:21
  • @Joe Lately I've been switching between OSX in the daytime and Windows in the evening and the keyboard layout change is really confusing! – Lightness Races in Orbit Oct 25 at 9:34
  • @LightnessRacesinOrbit I'm sure! I have two different sized notebook keyboards with almost the same layout and that's bad enough. That's also why I've never tried a Dvorak keyboard. Just not sure how this relates to the question. – Joe Oct 25 at 10:59

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.