Settings

Theme

Ask HN: What are some terminal tricks you wish you knew sooner?

66 points by aj-4 7 years ago · 35 comments · 1 min read


Here are mine:

1. create aliases for everything in ~/.bashrc to speed things up

2. window stacking / multi-tasking in iTerm with cmd + (shift) + d

3. turn on vi(m) syntax highlighting by adding :syntax on to ~/.vimrc

4. use jq to format and colorize json files / streams with the pipe | operator (so useful!)

5. Generate a custom command prompt at http://bashrcgenerator.com/ (just for fun)

I really like this topic, so made a video going into more detail on mine, if anyone's interested https://youtu.be/okGN2RXFw_U

eb0la 7 years ago

Using the caret (^) character to correct mistakes.

Imagine you write something like this:

# moutn -o ro -o remount /dev/hda0 /

Arg! I should have written mount, not moutn!

Easy fix:

# ^moutn^mount

I find it better than editing the last line (with cursor). It even works at boot and without 'doskey-like' arrow history.

pizza 7 years ago

byobu and tmux are god-tier

vim: use :!some_shell_command --with_arguments to run a shell command from within vim quickly

also :set hlsearch to allow search highlighting, and use :noh to disable the current highlight once you no longer need the text to be a different color

tmux: Prefix-[ to scroll up console text. use / to search down, and ? to search up (just like in vim)

tmux: Prefix-z to zoom current pane to full size of window

zsh: vi-mode plugin to quickly use vim shortcuts on terminal input text, history-substring-search plugin to quickly remember what finicky command I was using earlier

also shells in general: ctrl-s to pause text (eg if you want to pause log spam), ctrl-q to resume

ssh -XC for compressed x11 forwarding, to speed up x11 responsiveness

on remote server: python2 -m SimpleHTTPServer 8080 --bind localhost to quickly start a simple file/http server from the current directory that is NOT internet accessible (ie nobody steals your shit)

then on local computer: ssh -L 8080:localhost:8080 to tunnel to remote server's localhost server. then just type in localhost:8080 in firefox to access remote server's files

  • NikkiA 7 years ago

    vim: use :%!cmd to pipe the current buffer into a command, and replace the buffer with the output from the cmd

    vim: use :w !cmd to pipe the current buffer into a command but display the output from the cmd to screen and discard it afterwards

    vim: use :r !cmd to read the output from the command and insert it into the current buffer

    All of the :! and :r and :w commands that deal with a shell can also be passed the filename for the current buffer with %, eg:

    ":r !md5sum %" will execute md5sum with the filename of the current file (if it is unsaved, it will use the on-disk version, obviously) then pull the output checksum into the current buffer at the cursor position

    • NikkiA 7 years ago

      Emacs equivalents are (by default) mapped to M-! and M-| with/without prefix arguments (so C-u / M-<num> etc)

  • aj-4OP 7 years ago

    great list

jiva 7 years ago

If your SSH session gets locked, you can kill the session without killing the terminal window by pressing Enter, ~, . (period)

sethammons 7 years ago

Pretty sure these count:

ctrl+r (reverse search) on steroids: fzf (https://github.com/junegunn/fzf)

put a symlink to my dot files to a dropbox folder (always backed up .bash_profile or whatnot)

ssh tunnel to expose a port from a remote server on my computer: ssh -L 8500:localhost:8500 $remotehost

expose a port on my machine to the internet (demos and such): https://ngrok.com/

git bash prompt and git completion: know at a glance the branch I'm on and if there is un-staged/un-committed code. Allow tab complete for remotes and branch names. Lots of options here.

autojump (https://github.com/wting/autojump): `j foo` will jump directories to my /whatever/foo directory. Better than popd and pushd.

And before I had it baked into my prompt: `echo $?` to show the exit code of my last command.

codegladiator 7 years ago

ctrl+r for reverse searching of previous command

add a "# tags" at any commands end to use above for future.

so lets say you have a ssh into multiple servers

$ ssh -Uasfd host1 # server1

$ ssh -Uaiouhqe host2 # server2

then later, ctrl+r -> and type ver1

rat_melter 7 years ago

  $ mv file.text !#:1:r.txt
  evaluates to:
  $ mv file.text file.txt

  (colon separated)
  !# means this command
  1 is the position of the argument to be modified
  r means strip extension
There's tons of history modifiers and a lot to be learned reading `man bash`.
croh 7 years ago

1. for vim - https://laymanclass.com/vim-essential-setup-to-boost-your-pr...

2. autojump

3. solarized theme

4. zsh and antigen(particularly https://github.com/desyncr/auto-ls and https://github.com/psprint/zsh-navigation-tools)

lowkey 7 years ago

sudo !! to run the previous command as sudo (so useful when you forgot to sudo a long command)

ksaj 7 years ago

Do you mean cmd shift t? The d exits the terminal, which is the opposite of what I assume you meant. T for Terminal, and D for Die.

Also in .vimrc I add: set noswapfile, set nobackup, and set nowb

These were probably useful when terminal sessions were conducted over flaky modem connections. I use Raspberry Pis a lot, so I'd rather not speed up the SSD card's inevitable failure.

  • bradknowles 7 years ago

    So, your vi never crashes? Your ssh session never abruptly terminates?

    I understand that you don’t want to further shorten the lifespan of your raspberry pi MicroSD card life, but maybe you should just buy better cards with a longer life?

    Or maybe don’t work directly on the raspberry pi all the time, and only scp or rsync the files over there when you’re done developing stuff?

    Do you make /tmp unwritable by all processes, too? What about /var/tmp?

    Any other standard safety measures you always turn off that you want to share with us?

I_complete_me 7 years ago

fd is a simple, fast and user-friendly alternative to find. Install from https://github.com/sharkdp/fd/. Example: to find all files with .md extension in current directory : fd -e md

znpy 7 years ago

Ctrl-z to suspend a program, jobs to view the list of suspended programs and fg to resume a program.

They are the terminal equivalent of iconising a window. You can do some form of multitasking without screen/tmux/byobu

ktpsns 7 years ago

Search in bash history -- CRTL+R (many people also bind this to other keys)

iraldir 7 years ago

ctrl-r to history search commands

ctrl-a to go to begining of line, ctrl-e to go to end of line, ctrl-u to delete line

oh-my-zsh to display nice contextual info on git, navigate more easily etc.

If you have commands you use regularly (unit test, building, git, etc.), keep a tab for each and create a profile so that it goes directly in the folder you need, and can have a different color.

  • Crontab 7 years ago

    The CTRL-U was one I just recently learned and it has been surprisingly helpful.

tkjef 7 years ago

use less for safe read-only mode and to utilize vim commands still

esc-b to jump back a word on the terminal

esc-f to jump forward a word on the terminal

nerdcommenter (my favorite vim plugin)

git-gutter (my 2nd favorite vim plugin)

!-2 (repeats 2nd to last command)

practice! https://www.shortcutfoo.com/app/dojos/vim

yulaow 7 years ago

history

I didn't know that command even exists. You can just use it in pipe with grep/sed/... to look for specific commands used in servers you do not directly manage often (Eg last time I used them I was looking for all the docker commands in a server to understand how to startup the correct containers and in which order)

adolph 7 years ago

Using vi for the terminal prompt and history:

set -o vi

  • znpy 7 years ago

    You can set this for all the programs that use readline... can’t remember what’s the configuration file to edit though.

thepete2 7 years ago

fc ("fix command") opens an editor with your last command and executes it when you leave the editor. fc -l lists your latest commands, fc -ln does so without line numbers. Useful if you want to write a script (fc -ln > myscript.sh).

thisisrajat 7 years ago

- CMD + A to go to front of current line.

- CMD + E to go to end of current line.

- `sudo !!` to run last command with `sudo`

  • GoMonad 7 years ago

    Slight generalization of the first two: many emacs keybindings work in bash.

    • znpy 7 years ago

      Slight generalisation: many of the emacs keybindings work with commands whose command interpreter is built using the gnu readline library (bash is one of those).

      Also, iirc the readine library cod also be switched to vin-like keybinding.

      Also, a nice surprise thst i found in macOS is that come keybindings like C-a and C-e work in textboxes in macOS.

znpy 7 years ago

pushd/popd - manipulate a stack of directory paths.

Useful to save the current directory, go somewhere else, do stuff and then come back to the original directory.

It works awesomely in bash scripts too!

aj-4OP 7 years ago

hyperlink btw https://youtu.be/okGN2RXFw_U

jquast 7 years ago

fill the screen/DEC tube alignment sequence

    printf "\033#8"
gabrielblack 7 years ago

bash disown command, particularly useful if I forget to prepend nohup

exhoez 7 years ago

cd - to come back to your previous directory location

tmwed 7 years ago

ctrl+r to search previously entered commands.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection