12

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

muru
  • 193,181
  • 53
  • 473
  • 722
  • 30
    If you `cd` somewhere by accident, use `cd -` to return to your previous location -- bash keeps the `$OLDPWD` variable for this purpose. See https://www.gnu.org/software/bash/manual/bash.html#index-cd – glenn jackman Oct 22 '18 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 '18 at 16:50
  • 2
    Really bad idea: HOME=. – Joshua Oct 22 '18 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 '18 at 20:00
  • You could start using `pushd` instead of `cd`, as it won't change directories when nothing's given. I'd avoid changing the behavior of `cd`, as I worked in academic computing in college, and engineering students were always surprised to find that `rm *` didn't prompt them for what to delete (because the engineering school had aliased `rm` to `rm -i`) – Joe Oct 23 '18 at 01:08
  • @Joe, pushd with no parameters switches to the second directory on the stack, like pushd +1. – prl Oct 23 '18 at 09:37
  • 2
    @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 '18 at 23:35

4 Answers4

14

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 "$@"
}
WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401
  • 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 '18 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 '18 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 '18 at 07:59
6

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.

muru
  • 193,181
  • 53
  • 473
  • 722
4

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
}
waltinator
  • 35,099
  • 19
  • 57
  • 93
4

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.