Overview
Today, I'm going to be sharing a little trick I use to teleport around my terminal with ease.
Do you change to your project d...
For further actions, you may consider blocking this person and/or reporting abuse
pushd
andpopd
are super handy for jumping to several locations, then trying to backtrack to where you were a few jumps ago; I've known about them for a while and still haven't gotten into the habit of using them.But for simple one-off cases, just jumping to one directory and the jumping back to the last one, there's another option that I use more often: if you call the
cd
command with the-
character, it jumps back one directory!This only works for a single level; whereas
pushd/popd
will store as many moves as you want, if you issuecd -
a bunch of times, you'll just jump back and forth between the same two directories. But for most of my own day to day uses, it gets the job done. This is especially handy when you weren't planning to jump back, like when you misremember where something is and youcd
to the wrong place, then want to jump back to where you were.Another related protip: the
git checkout
command adopted the same convention, so thatgit checkout -
will checkout out the previous branch!~- is the value of the shell variable
OLDPWD
, so you can list out your previous directory with ls ~- . Saves a little time from having to type all the extra characters in cd - && ls && cd - .But since it holds the actual directory path, it's even more useful because you can quickly list out any subdirs with ls ~-/oldPwdSubDir/anotherSubdir
Another useful feature of
cd
, calling it with no arguments takes you to~
. This can save you doingcd ../../../..
orcd ~
!A side effect of this that sometimes catches me off guard is that if you try to
cd
to an environment variable that doesn't exist, or if you misspell a variable (e.g.cd $PORJ_HOME
instead of$PROJ_HOME
), you end up running plaincd
by accident and going home instead! Always takes me a second to realize what happenedMaybe worthwile to note that
zsh
gives you multiplecd -
entries and you can even iterate and inspect the list when hitting tab. Quite nice. And thenwd
plugin for "warp points" (basicallycd X
aliases, but with convention).I ❤️ aliases
You gotta check out rupa/z. It's not quite as reliable as an alias, it is magical. It's based on your frecently used directories.
rupa / z
z - jump around
I was hopping to see this kind of tools at this post
I use a combination of z and aliases. E.g.:
So now, if I just type
projects
I'll always go to the~/Documents/projects
folder, but if I typeprojects img
I'll have a good chance of going to the~/Documents/projects/static/img
folder.The alias is using z, but only to change to a folder under
~/Documents/projects
.Otherwise, you could use a better z :)
github.com/ajeetdsouza/zoxide
One extra of using &&, if the first command fails it stops, if you need to run all commands even when any of them returns an error you can use ;
As && you also can use ||, to execute something if the previous command returns an error, very handy on scripts, something like:
command > /dev/null 2>&1 || exit 1
To suppress stdou and stderr of command but stop the script if returns an error
That's actually really useful to know! Thanks for sharing!
I've never gotten into the habit of using aliases for
cd
-ing into directories, but I use thepushd
set of commands so much, I have them shortened topu
,po
, andds
(fordirs -v
). I don't personally have a huge use for location aliases, as I just use soft links to create shortcuts to them from my home directory. Typing~/
isn't a huge deal to me.That said, the "proper" or POSIX-y way to do this is to use the
CDPATH
variable. Much like you can set your default binary paths with thePATH
variable, you can givecd
a list of places to look for directories you've asked for. You should usually make.
the first directory inCDPATH
, but you can add as many additional directories as you like. So in your examples, if you add~/Documents
to yourCDPATH
, you could get to your projects folder by just typingcd projects
from wherever you were. As long as the current directory didn't contain a "projects" folder, you'd be taken to your folder at~/Documents/projects
. Thepushd
command should obey theCDPATH
as well, but I haven't tested it in a while.Hello Jimmy,
thanks for article.
Please check the code in the CD Aliases section. There are missing the
&&
.The correct code should be:
Thank you very much! Good catch. The changes are saved now. :)
Supercharge your cd command with something like this:
Then in
~/bin/goto
you can put extended logic that will find where you want to go, for exampleFinally, if you prefer to use
cd
rather thangoto
add an extra alias:Example Usage:
For entering frequently visited directories I really enjoy combination of
Then
CTRL-G dev
will show me the list of all most used directories with dev in their name sorted by the usage frequency. Hitting enter with enter the directory.Great idea, Jimmy! I’ve used aliases with
pushd/popd
for decades, actually, but never thought to include an automaticls
on the end. For example:This uses one other nice feature of pushd — by itself it swaps the top two items on the directory stack! So you can quickly go back and forth between two directories with a single comma.
Including
&& ls
at the end of this, though, prevents applying it in general, as any trailing file/directory arguments are picked up by thels
. But if you instead use a shell function:it works both with and without an argument.
Also, if you don’t mind occasionally typing out
source
, you can repurpose.
:I’m using
.
here because it’s next to,
, it signals finality, and there really aren’t any other punctuation keys available without shift that aren’t interpreted by the shell for something else 😀I really like the
..
idea, though I doubt I would use more than one level very often. One other suggestion I would make is to perhaps provide a variant,to add the parent directory to the directory stack.
Finally, sometimes I want to remind myself of the current directory stack, which is displayed with
dirs
. Again, being short on punctuation keys but given this is not that common, I can alias it to,.
, which is in keeping with the rest of these options:A small typo in:
alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias ....="cd ../../../.. && clear && ls"
the 4th alias is identical to the 3rd one, so will over-write it; I imagine you meant the 4th alias to be five dots, not just four like the 3rd one.
Fixed! Thank you!
Good post :)
While I recognize the value in setting up custom aliases, IMHO, fzf (github.com/junegunn/fzf) is all I need. I can type like a raging lunatic monkey and most of the time it still guesses what I actually wanted to type. fzf changed my life!
Make aliases more easily:
This is leet af! I'm stealing this for sure.
These are my aliases and adding yours... Very cool recommendations. Thanks
Hey, instead of typing e or exit, you can do Ctrl-d, it will do the same as exit in almost all cli apps!
Basically Ctrl-d sends a special control character to the application, saying "there is nothing left to read!" which will usually close the cli app.
Greaaaaaaat!!!! pushd is just amazing! I will miss typing
cd
on my terminal from now on. Thanks!I love aliases, laziness converted to productivity hacks.
Here is my implementation of common git shortcuts, along with some common patterns using bash functions.
Enforce git messages format without throwing everyone off?
Piyush Kumar Baliyan ・ Jul 2 '20 ・ 3 min read
I didn't know you could do git aliases like that. Really cool stuff. Thanks for sharing! 🔥
I've been using autojump for mac/unix for a while now. Just using
j <part of the dir>
and it "teleports" you automatically to your last recent places. The only thing needed is to cd at least once to the repository.I have this little function in my
.zshrc
which is pretty convenient:My favourite is the humble *
instead of:
cd /var/www/this-is-a-test-to-see-if-it-works.tk
pwd
$ /var/www/this-is-a-test-to-see-if-it-works.tk
try this instead:
cd /var/www/this-*
pwd
$ /var/www/this-is-a-test-to-see-if-it-works.tk
This shell script could be handy if you want to jump to a specified directory to execute only a single command: github.com/bimlas/bash-mosh
If you are using ohmyszsh: github.com/ohmyzsh/ohmyzsh/tree/ma...
pj my-project
for openingmy-project
pjo my-project
for open the project with yout$EDITOR
Had no idea about &&! Great tip. Thanks!
Lovely tips, thanks a lot!
More than 10 years ago I wrote github.com/bulletmark/cdhist which I use probably 100 times per day. I don't know how the rest of you get by without it!
Great article!
But I still prefer using Autojump github.com/wting/autojump
Few of the aliases i always use navigate between sub dir...
.1= ..
.2=../..
.3=../../..
.4=../../../..
.5=../../../../..
I use github.com/b4b4r07/enhancd and never looked back..
I use a combo of ..... aliases and $CDPATH together with bash TAB completion. Put common project roots into CDPATH
What if I don't want to write & maintain the aliases?
That's where autojump comes in.
(same niche as rupa/z?)