Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

141–150 of 179 posts

Re: How to navigate directories faster with Bash (2015)

#141
post #126

Another one: cdof () { cd "$(dirname $1)" } as in 'Change into Directory Of" Changes into the directory of a file. This is useful when you're doing complicated commands with long path names, but realize it would be easier if you were to cd into that directory. Use in combination with . Especially useful if the filename component is long: $ pwd => ~/ $ ls -l path/to/whatever/deep/somewhere/2021-04-22-filewithverylongn…

bit longer, but if given a file, change to its directory

    function cd() {

        if [[ ${#} -eq 0 ]]; then
            builtin cd
            return
        fi

        if [[ -f ${1} ]]; then
            builtin cd $(dirname ${1})
        else
            builtin cd ${1}
        fi

    }

Re: How to navigate directories faster with Bash (2015)

#142

Not a word about command editing. Learn keyboard shortcuts to edit the current command line and you'll save years of your life. ESC-. (dot) will get the last argument of the last command. This one alone will save you months. CTRL-R will search backwards. This other will save more months.

I remember being advised that it was more efficient to use the command line editing shortcuts instead of changing directory.

That was over 10 years ago and I still use the shell with this in mind.

I’m not saying I never change directory, but once I do, I tend to stay there.

I had to scroll a fair way down to find a comment validating this opinion.

Perhaps we’re wrong when saying to change directory efficiently, you don’t.

Re: How to navigate directories faster with Bash (2015)

#144
post #2

I upvoted for this alone: Another neat short hand is !$ which is an alias for the last argument of the previous command. This can be handy if one creates a new directory and wants to change into it without typing the the directory again. So commands would be mkdir -p make/new/directory cd !$ You've no idea how many years I've been thinking "This is Linux fer gawd's sake. Surely there must be some easy way to make a n…

I know this is a bash thread but zsh/oh-my-zsh will replace !$ with the actual argument when you type a space so you can see and edit it before executing the command.

bash supports the same, also named magic-space. For bash you can chuck something like "Space: magic-space" in your $INPUTRC, although you'd probably want a guard(see Readline Conditional Constructs in the manpage).

FWIW, zsh without oh-my-zsh can be configured to do too this with "bindkey ' ' magic-space".

Re: How to navigate directories faster with Bash (2015)

#145

Here is one additional bookmark solution [1]: export CDPATH=.:~/.marks/ function mark { ln -sr "$(pwd)" ~/.marks/"$1" } mark @name # create a bookmark cd @name # jump to bookmark cd @ # list bookmarks cd @n # auto-complete cd @name/ # can access sub-directories within bookmarks Works best when bookmarks are prefixed by "@" or other special symbol. Additional advantage is that you can use the same function `cd` to nav…

I have seen many solutions like this but this one is by far the most lightweighted. Thank you for sharing.

Re: How to navigate directories faster with Bash (2015)

#148
post #137

The most important tool I found for switching directories is called "z". You just partially type a previously visited directory's name and it takes you there. It also keeps track of your history to rank candidates for popularity. https://github.com/rupa/z/ It's basically CDPATH on stereoids and doesn't require preconfiguration like CDPATH.

I largely despised the commandline until I discovered z. Then I realized there are ways to make the commandline actually usable and it became an incredible, useful and user-friendly tool for me.

Re: How to navigate directories faster with Bash (2015)

#150
post #41

Earlier quoted context omitted.

For this exact use case I defined the following many years ago in my bash profile: # create a new directory and enter it function mkd() { mkdir -p "$@" && cd "$_"; } And use it like: mkd foo

Beware that using "$@" there might not be what you want, as it passes space-separated arguments to mkdir. What does: "mkd foo bar" do? It creates both directories, and cd's into "bar". Might be worth checking that only one argument has been passed, or use "$*" which instead would treat all arguments as one parameter, and "mkd foo bar" would create a "foo bar" directory instead (and cd to it) YMMV.

Thanks a lot for pointing this out. I will happily fix this now!
Post reply on HN