Live data from Hacker News

How to navigate directories faster with Bash (2015)

mhoffman.github.io

81–90 of 179 posts

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

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

escpae-dot (.) also works for this: "It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore."

Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-...

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

#83
post #67
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 have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }

Yep, first thing I add to my dotfiles when I don't have it haha

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

#84
post #7
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…

Typing Esc then . does the trick too.

Only in emacs editing mode.

Real programmers use set -o vi

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

#85
These sorts of things are really useful and it's one of the reasons I think everyone should keep their dotfiles on github or similar so they can share their little tricks. Mine are just a compilation of stuff like this that I've picked up from other people over the years. (I even try to put attributions in there if I got them from friends)

My favorite one is c to change to my ~/code directory with tab completion from anywhere. So I can type c pr to cd in to ~/code/project from anywhere. (Full disclosure This is zsh and I don't have a bash equivalent for that)

Another thing I can't live without and some people hate is ls -la anytime I cd in to a directory. In a gui, I get that visual feedback immediately and it's usually the first thing I type when I land anyway so I just have a function for it https://github.com/gpspake/dotfiles/blob/master/zsh/gpspake/...

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

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

mkdir -p foo && cd $_

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

#87
post #67
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 have an "mkcd" function in my startup files: mkcd() { mkdir -vp "$1" && cd "$1"; }

Wait, now I'm confused. I have:

mkcd() { mkdir -p "$@" && cd "$_"; }

Can someone please detail the differences between $! and $_? (Apologies if I'm missing something obvious)

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

#89
post #41
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…

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.

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

#90
post #58

> Maybe less well-known is that ~ is an alias for one’s home directory. And ~mylogin is another alias for ones home directory. And more generally, ~otherdev is an alias for otherdev's home directory.

This is accomplished with the zsh hash table I believe. `hash -d myproject=~/projects/myproject` will set up the same thing for ~/projects/myproject

$ cd ~myproject

$ pwd

~/projects/myproject

Post reply on HN