DEV Community

Cover image for What are your command line tips?
Ben Halpern
Ben Halpern Subscriber

Posted on

What are your command line tips?

Let's here them!

Top comments (77)

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Oh my, I love this topic! Here comes a wall of text:

Using !! redo the last command, which is specially useful for those times you forgot to run that big annoying command as sudo, you can just run sudo !! this time.

Also, if you just cd without arguments, you end in your home. Sometimes thats useful.

On the same theme of cd, you can cd - to go to last directory. I love this! Also works with git checkout - to go to the last branch!

Another one is that if you use bash or zsh (and probably fish?) in emacs mode, which is the default, there are some commands that are very helpful:

  • Control + w Deletes one word behind, like control + backspace on some editors
  • Control + k Deletes everything from cursor to the end of the line. Maybe only the start of your command was right and you don't want to retype a bunch of things.
  • Control + u Deletes everything from cursor to the start of the line. Maybe only the end of your command was right.
  • Control + y Paste the last thing you deleted using the above commands. A god send.

Honorable mentions:

  • Control + e and Control + a go to the end of the line/start of the line, respectively
  • Control + p is the same as up arrow, useful if you think the arrow keys are far away
  • Control + n is the same as down arrow. Ditto above
  • Control + m is the same as hitting enter
  • `Control + i is the same as tab (completion)

I particularly use control + p a lot!

And as a side note, if you don't want to go sudo !! like I suggested initally, you can always go up in history (control+p or up arrow, you name it), cut everything with control+u, write sudo and then paste it with control+y. Really good commands here!

And you can always customize what key presses do with bind in bash and bindkey in zsh.

If you are on bash, you can see all commands with bind -P and bind -p. To set different things, you can run (or put in config file, eg, .bashrc) something like bind '"\C-f":clear-screen'.

If on zsh though, just bindkey is enough to list them, and bindkey "^F" clear-screen to remap.

I personally use control + f as clear screen for so long, that when I'm at a computer that does not have it I feel lost initially, which is arguably a downside to customization of hotkeys, specially for commands that already have a simple command, like clear screen. Maybe you should not use the mapping I do! 😅

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

!! is nice, but it's really only the beginning. If you force yourself a bit to use more complex expansions even when they slow you down at first, you'll quickly end up making your life infinitely easier with things like !?vim?:gs/foo/bar or mv some_long_file_name.jpeg !#:$:r.jpg (although that last one could also be done easily with rename "s/jpeg$/jpg/" some_long_file_name.jpeg, of course)

Collapse
 
lucassperez profile image
Lucas Perez

Even simple things like mv file.{txt,md} already feels nice to do.

rename is a good program, by the way!

Collapse
 
lucassperez profile image
Lucas Perez

But the most game changing learning was on how to do some shell scripting. How to use grep, regexes, sed, cut, cat and even for loops. Simply a whole new dimension opens up to you, and it's all in your hands.

Collapse
 
leob profile image
leob

Add awk to that ...

Collapse
 
arjunsahlot profile image
Arjun

control + l is a native hotkey for clearing screen. Maybe you would want to switch to that?

Collapse
 
lucassperez profile image
Lucas Perez • Edited

Yes, but when I started using tmux, I remapped control + l to something else (switch to right pane). That's when I remapped control + f. Pretty much a sequence of changing native hotkeys that just pile up for me when I'm not with my computer... 😅

Customizing things sure is nice, but having standards are probably nicer. We should always proceed with care when customizing!

Collapse
 
nandhae profile image
Nandhagopal Ezhilmaran

Amazing write up!! You have listed down all my favourites and few I didn’t know before. Thanks.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

I always forget how to bind keys, so I need to fallback to Ctrl+R, on a VPS or un-setup machines.

Collapse
 
afheisleycook profile image
technoshy

I tried selfish

Collapse
 
pedroifgonzalez profile image
Pedro Iván Fernández González

Thanks a lot

Collapse
 
ben profile image
Ben Halpern
Collapse
 
waylonwalker profile image
Waylon Walker

Tack on fzf and you have gold.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Seconded on fzf, only external CLI utility that I will recommend

Collapse
 
pandademic profile image
Pandademic

what? fzf is the gold!

(joke)

Collapse
 
maxxon profile image
Ma-XX-oN

Using Ctrl+O after going back in the history (either with arrow keys or Ctrl+R) will populate the next prompt with the next item in the history. Great if you have multiple commands you want to execute one after the other repeatedly.

Collapse
 
leob profile image
leob • Edited

Yep that would be my #1 too - control-R all the way! Oh and I use cd - all the time ...

Collapse
 
waylonwalker profile image
Waylon Walker

If you are going to take the time to setup your command line real nice, then you need a dotfiles repo, if you have a dotfiles repo you need to stow.

I always thought the stow docs jump right into the weeds and make it sound very complicated, but It's pretty simple if you worry less about the implementation.

Collapse
 
lvjian700 profile image
Jian Lyu • Edited

TL;DR. The first step of being master of any command:

tldr.sh/

➜  ~ tldr curl
curl

Transfers data from or to a server.
Supports most protocols, including HTTP, FTP, and POP3.
More information: <https://curl.se>.

- Download the contents of a URL to a file:
    curl http://example.com --output filename

- Download a file, saving the output under the filename indicated by the URL:
    curl --remote-name http://example.com/filename

- Download a file, following location redirects, and automatically continuing (resuming) a previous file transfer and return an error on server error:
    curl --fail --remote-name --location --continue-at - http://example.com/filename

- Send form-encoded data (POST request of type `application/x-www-form-urlencoded`). Use `--data @file_name` or `--data @'-'` to read from STDIN:
    curl --data 'name=bob' http://example.com/form

- Send a request with an extra header, using a custom HTTP method:
    curl --header 'X-My-Header: 123' --request PUT http://example.com

- Send data in JSON format, specifying the appropriate content-type header:
    curl --data '{"name":"bob"}' --header 'Content-Type: application/json' http://example.com/users/1234

- Pass a username and password for server authentication:
    curl --user myusername:mypassword http://example.com

- Pass client certificate and key for a resource, skipping certificate validation:
    curl --cert client.pem --key key.pem --insecure https://example.com
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tqbit profile image
tq-bit • Edited

if, during development, you

  • must type a command more than twice
  • type several commands that look similar
  • work in a team of more than 2 devs

you should use bash or powershell to automate whatever you're doing.

Computers are way better than keeping stuff in mind than people. I'm writing scripts for every non-throwaway project by now

Latest one for the appwrite hackathon goes

#!/bin/zsh
COMMAND=$1

startAppwrite() {
  docker run --rm -it \
    --volume /var/run/docker.sock:/var/run/docker.sock \
    --volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
    --entrypoint="install" \
    appwrite/appwrite:0.13.4
}

startFrontend() {
  docker-compose up --build
}

install() {
  echo "Installing appwrite cli ..."
  npm install -g appwrite-cli

  echo "Logging in to API ..."
  appwrite login

  echo "Initializing Project"
  appwrite init --all
}

# Commando functions
initServices() {
  echo "Setting up appwrite for the first time"
  startAppwrite
  install
  startFrontend
}

stopServices() {
  echo "Stopping iCuisine services"
  docker-compose down
  docker-compose --file appwrite/docker-compose.yml down
}

# Command execution
case "$COMMAND" in

up)
  initServices
  ;;
down)
  stopServices
  ;;
[iI]*)
  install
  ;;
*)
  echo "No command recognized. "
  echo "Possible commands: [up, down, login, i(nstall)]"
  ;;
esac
Enter fullscreen mode Exit fullscreen mode

This little bit of code saves me at least 1 minute whenever I want to bring up or down my dev server. Took like 5 to write. And I have to memorise only a few commands instead of several. Totally worth it

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Yep. I even write scripts for the simplest of tasks now:

#!/bin/sh
fantasy_test --some_param || exit
made_up_lint || exit
fake_package install project.fake --parameter
Enter fullscreen mode Exit fullscreen mode

These micro-scripts can seem like a waste of time while writing them, but after calling them 10 or 20 times, you've already saved more time than it took to set them up.

Collapse
 
hoffmann profile image
Peter Hoffmann

set -e

Will also exit a skript if a command inside fails

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

No it won't. Look at the script again ;)

Collapse
 
gbhorwood profile image
grant horwood
Collapse
 
equiman profile image
Camilo Martinez • Edited

I have a series of 7th articles with a lot of them for ZSH:
dev.to/equiman/series/11407

Another to config zsh + power level 10 on macOS:

But the most useful for me is config the fingerprint reader to use on the terminal when asking for a password:

Collapse
 
cerchie profile image
Lucia Cerchie

git diff more of a git tip but it saves me time and trouble seeing if/what differences I have between my branch and main.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾 • Edited

Only 1 thing,
Learn a liitle bit of shell scripting, it changes how one works with a terminal/CLI
Knowing a bunch of things and putting them together to improve your workflow as developer everyone should strive for.

Collapse
 
yechielk profile image
Yechiel Kalmenson