DEV Community

These are a few of my favorite: Terminal Shortcuts

Sal Hernandez on July 28, 2017

Terminal: an interface where the user types and executes text based commands. I’ll admit that when I first opened the terminal in 2014, I dislik...
Collapse
 
bensheldon profile image
Ben Sheldon

CTRL-r to search through your bash command history:

unix.stackexchange.com/questions/7...

Collapse
 
ennor profile image
Enno Rehling (恩諾)

Sometimes even faster, !ls repeats the last command that started with ls (or any other sequence of characters).

Collapse
 
silwing profile image
Silwing

This is easily my favorite as well.

Collapse
 
clickclickonsal profile image
Sal Hernandez

Oh, this is really cool! I'm gonna keep this in mind next time I run into this!!!!

Tanks for the tip!!!

Collapse
 
igormp profile image
Igor Moura

Just a heads up, touch isn't actually meant to create files (although it does it if the specified string doesn't exist), it is meant to update a file's timestamp (in case you point it to an already existing file).

You could also add the < operator - which does the same as cat, but is actually meant to redirect stuff into the stdout of your terminal.

Collapse
 
clickclickonsal profile image
Sal Hernandez

Oh wow, I didn't know that about touch. Thank you for clarifying this here.

I tried the < & that didn't seem to work on mac or linux.

Collapse
 
dennisfen profile image
Denis Borisevich

Try this:

echo "some text" > new_file.txt

or this (which means basically "write data to the file until you meet EOF label"):

cat > new_file.txt << EOF
new line
another line
EOF

> will rewrite contents of the file
>> will append to the end of the file

Collapse
 
igormp profile image
Igor Moura • Edited

Weird, I've tried it in both systems and it works flawlessly.

Just to be sure, did you type in < filetoread? A lack of example might've been the problem in my post.

Collapse
 
poison_dv profile image
David Virtser

Try sudo !! when you forgot to put sudo in front of last command.

Collapse
 
clickclickonsal profile image
Sal Hernandez

True! This is a good & powerful command. I'd like to mention that always understand what it is you're trying to do when you're using sudo. :-)

Collapse
 
the_scott_davis profile image
Scott Davis ⭐
  • "open ." to open your current directory path in a Finder window in Mac

  • drag drop a file or directory into terminal from any application to automatically copy the text into the terminal window (handy for changing into directories within terminal)

Collapse
 
clickclickonsal profile image
Sal Hernandez

Thanks for sharing your tips! These are awesome!
I use the first one all the time! I forgot to mention that one! :-)
The second one I didn't know about! That could come in handy in the future!
I learned something new! :-) Thank you, Scott!

Collapse
 
the_scott_davis profile image
Scott Davis ⭐

My pleasure! Enjoy!

Collapse
 
denvercoder profile image
Tim Myers

I am the only dev at my job that uses command line Git...I am the only dev at my job that doesn’t think that Git sucks. I think people confuse the GUI with the underlying tech, ie, “I can’t find my stash in SourceTree, I really hate Git”.

I come from a strange time. Started on a PC using DOS a bunch to add Extended Memory and Shadow Memory to play games.

I “locked” down my windows install by renaming win.exe to tim.exe. I did a lot in the command prompt.

Then I became a .NET developer. Visual Sudio has always been either a click-me interface or use keyboard shortcuts interface. It rarely required command line. Even now I use it more with the Nugget Package Manager Console but still, for the last 10 years or so I haven’t used the command line much.

Then I got a Mac and I’m back to mostly terminal, (iTerm). So in my career I have gone from command line to GUI and back to command line.

Collapse
 
rhymes profile image
rhymes • Edited

Good intro!

A couple of things:

  • checkout iterm2.com/ when you have some spare time. I think it's better than the default OSX Terminal

  • if you work on computer you own you don't really need your name and computer's name in the prompt, this way you can save some room in the window

Collapse
 
clickclickonsal profile image
Sal Hernandez

Thank you. Actually that terminal was iterm2! I'm a huge fan of it! :-)

Bullet point 2 is a really good tip. I think I'll do that now because you're right! Freeing up real estate on the terminal is a plus!!!

Collapse
 
kalinchernev profile image
Kalin Chernev • Edited

Ctrl + u is the first nature of Ctrl + l :)
I personally do both mechanically.
echo 'something' somewhere and echo 'something' >> somewhere is also a beginner one that helps quite often, say adding items in a .gitignore file.

Collapse
 
clickclickonsal profile image
Sal Hernandez

Whoa! I love everyone who's been sharing their tips as well! I've always used Ctrl + C to jump to a fresh new line. Now I know that I can do Ctrl + u to clear out my terminal! :-)

The echo command is awesome as well! I forgot to mention a lot of good ones. 😅

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Using rm cannot be your favorite shortcut. I would suggest using trash-cli.

Collapse
 
clickclickonsal profile image
Sal Hernandez

Whoa! It Cannot be my favorite shortcut? That's a very aggressive statement.
Everyone is entitled to their own opinions and they will determine what they consider their favorites.

Granted, rm is a very powerful command & I recommend everyone who's using it to fully understand what it does which I mention in my blog post.
It's one of my favorite shortcuts because when I delete stuff with it I know that it has been completely removed from my computer.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

It's a bad practice to use rm — you're def entitled to your own opinion but be open to suggestions as well. You cannot always be right. Using rm is a bad practice. Things can go wrong and you can end up deleting your OS.

Thread Thread
 
clickclickonsal profile image
Sal Hernandez

It is not bad practice to use rm. It is bad practice to use rm if you don't know what it does, but the same goes for running any command in the terminal.
The terminal is a very powerful tool & anything you run in it should be ran with a good understanding of what it is doing.

Thread Thread
 
ahmadawais profile image
Ahmad Awais ⚡️

Let's agree to disagree here.

Collapse
 
denvercoder profile image
Tim Myers

I am one of the only people at my job that uses command line Git...I am also one of the only people at my job that doesn’t think that Git sucks.

I think people blame GUI errors on the platform, ie, “I cant find my stash in SourceTree, Git really sucks”.

Collapse
 
clickclickonsal profile image
Sal Hernandez

Aw, that sucks to hear :-/ Git is awesome & I couldn't imagine developing without it!
I think you should try & give a tech talk at your job and show them the tips and tricks to using git on the command line. :-)

Collapse
 
itscoderslife profile image
Coder

==>(Cmd + k (Mac) `Ctrl + L (Linux/Mac) Clears the Terminal Screen)

In macOS Sierra, its Ctrl + L. Ctrl + k does not work.

Collapse
 
clickclickonsal profile image
Sal Hernandez • Edited

That's because It's not Ctrl + K, it's Cmd + K :-)

Collapse
 
itscoderslife profile image
Coder

Yup, works fine! Both are working Cmd + K and Ctrl + L

Collapse
 
palle profile image
Palle

The z command is one of my favorites. You just type z dir and it searches all directories you visited for a directory name containing dir and jumps to the best match.

Collapse
 
clickclickonsal profile image
Sal Hernandez

I kept the post tailored to beginners to make it easier to get familiar with the terminal.

The z command looks pretty cool but it doesn't have documentation on how to set it up, and this is one of the things I found hard for me in the beginning. I can figure out how to it up but not everyone can.

Maybe it would be cool for you to write a blog post on it ? Or maybe even submit a pull request on how to set it up.

Collapse
 
palle profile image
Palle • Edited
brew install z

Then add

. `brew --prefix`/etc/profile.d/z.sh

To your .zshrc file.
That's it.

I used this blog post to turbocharge my terminal. (The post recommends iTerm over the classic Terminal but everything works as expected in Terminal)

Collapse
 
anarchyrucks profile image
Ashish Acharya

it is good to alias rm to trash command.. it will become a life saver decision 😊

Collapse
 
clickclickonsal profile image
Sal Hernandez

Typically when I alias things I will alias them to something shorter. I see no reason to alias rm specifically.
when I use alias it be something like this
git checkout = gc

It's about reducing key strokes 😃

Collapse
 
anarchyrucks profile image
Ashish Acharya • Edited

I meant alias rm="trash"

Collapse
 
fulopm profile image
Márk Fülöp • Edited

Great article!

How did you make your terminal prompt look this way? It looks very familiar, but I can't remember where did I see this.

Collapse
 
clickclickonsal profile image
Sal Hernandez
Collapse
 
fulopm profile image
Márk Fülöp • Edited

Cool, thank you!

Collapse
 
pmogi profile image
Patrick Mogianesi

When my friend showed my tab auto-complete it changed my life lol

Collapse
 
clickclickonsal profile image
Sal Hernandez

Word! Tab auto-complete, completes me 😂❤️

Collapse
 
mrm8488 profile image
Manuel Romero

I use a lot
du -sh */ | sort -h
To order current folder files by size

Collapse
 
clickclickonsal profile image
Sal Hernandez

The -h flag comes invalid (I'm on a Mac). Thanks for that tip though! This is pretty cool!

Collapse
 
jgaskins profile image
Jamie Gaskins

They might've been going for sort -n instead. It sorts numerically instead of alphabetically (the only reasonable sort for du output). :-)

Collapse
 
plutov profile image
Alex Pliutau

telnet pliutau.com 8080 - snake game :)

Collapse
 
clickclickonsal profile image
Sal Hernandez

That's so cool!!!

Collapse
 
rusbra profile image
Rus

move cursor one word left
alt/option + ←

move cursor one word right
alt/option + →

Collapse
 
clickclickonsal profile image
Sal Hernandez

This is a good one, unfortunately, it doesn't work out of the box if you're using iterm.
For those using iterm & are looking to be able to do what Rus pointed out, check out this link coderwall.com/p/h6yfda/use-and-to-... !

Collapse
 
mrm8488 profile image
Manuel Romero

Another fast tip for
ls command
ls -l --sort <time | size>

Collapse
 
dmn1k_13 profile image
Dominik Schlosser

ls -lrt sorts by time ascending so the file edited last is at the bottom.

Collapse
 
rjpsyco009 profile image
Ryan Norton

Took a UNIX class last December. I have to say, I wish I had kept more notes, but this was a lovely refresher to terminal! Thanks!

Collapse
 
clickclickonsal profile image
Sal Hernandez

I'm glad it helped! 😁

Collapse
 
colorfultones profile image
Damon Cook

I believe tab auto-completion is something that is not out-of-box terminal command but a Zsh/Oh-My-Zsh package one must install?

Collapse
 
colorfultones profile image
Damon Cook

Oh my go(z)sh it is out-of-the-box functionality! Would ya look at that. 👀

Collapse
 
clickclickonsal profile image
Sal Hernandez

😉

Collapse
 
psnebc profile image
Patrick Schanen • Edited

I use zsh and ohmyz.sh on Linux Mint 17.3 psnc.github.io/linuxme/