Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

71–80 of 179 posts

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

#71

I'm surprised autocd was not mentioned. I saw alias ..='cd ..' # etc ... But not. shopt -s autocd zsh also has this. This seems like the fastest way to navigate since you no longer need to type, or retype, cd.

Hell yeah, I've been missing this functionality since the Amiga shell days! I should have known it was in there somewhere.

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

#74

The author missed to mention one of my favorite tricks to change back in to the last visited directory: cd - Hint: Change into directory called '-' can be done by cd ./- I also use the mkcd alias / function: mkcd() { mkdir -p "$@" && cd !$; } This allows creating multiple dirs and cd into the first one. mkcd test1 test2 test3

cd - is there, it’s one of my favs too

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

#75
post #36
post #14

Earlier quoted context omitted.

I'm an old-timer so was introduced to Unix before bash was ubiquitous. Actually it was csh/tcsh which later introduced me to "interactive" command history/editing. With interactive command history/editing, the old tricks to avoid excessive typing became less essential. But I still use some of the old techniques as they continue to save time/typing - particular "$!". And even though I've reduced my reliance on aliases…

Is there a way to tell CTRL-w to break words at / as well as space? I often use it to remove a parameter from a command I've copy-pasted or got from the history, but for the cd use case it removes too much. Edit: ESC followed by backspace seems to work.

Alt-Backspace

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

#76
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…

Yes, that's a good one! My way of remembering is "bang for my buck" -> !$

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

#77
I have a few functions in my bashrc for fast directory navigation.

up - cd .. # times

upto - find dir in the current path above you and cd to it

down - find dir in the current tree below you including inside any number of subdirectories and cd to it

https://github.com/robmccoll/dotfiles/blob/master/bashrc#L15...

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

#78

Earlier quoted context omitted.

May favorite bash "trick" is: !-1:gs/hello/world which runs the last command with all instances of hello replaced by world. !-1 can be any so called event designator, for instance !n to refer to any specific line number from history or !string for last command containing a particular string. Seriously people, read the man page for bash. Your shell has so many cool features.

And if you only have a single instance to replace, you can use ^old^new^ $ echo "hello hello hello world" hello hello hello world $ ^hello^bonjour^ echo "bonjour hello hello world" bonjour hello hello world

This is great for removing an option from a prior command such as unzip -t ...:

^-t

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

#79
post #61

I independently also have a stack of '...', '....' etc aliases almost exactly the same as this! Extremely useful I reckon! Another thing that I've found surprisingly invaluable is an alias (i went for 'ccd') that figures out (via python, so a bit slower on the 1st run) what part of the argument is a valid directory and then cd's you there. It's very handy indeed when working with file paths all the time (a film-vfx-o…

No need for stacks of `..`. Use this instead: function up() { for i in $(seq 1 $1); do cd ..; done } # usage: up # goes up 1 directory up 2 # goes up 2 directories up 42 # goes up 42 directories

Nice little helper function, thank you. What do you think about renaming it to '..'? For me this feels a bit more natural:

  function ..() { for i in $(seq 1 $1); do cd ..; done }

  ..    # goes up 1 dir
  .. 2  # goes up 2 dirs 
  .. 42 # goes up 42 dirs
Post reply on HN