
Know Your Tools – Terminal and Bash - zan
http://www.spacecowboyrocketcompany.com/2015/02/know-your-tools/
======
tensorproduct
The first thing I do on any bash terminal is `set -o vi`. This changes the
default keybindings from emacs-like to vi-like. I find it to be a huge booster
to my speed in navigating the command line.

edit: Actually, something cool I learned recently is that if you put the line
"set editing-mode vi" in your ~/.inputrc then vi-like editing keys are
available in any program that uses readline. This includes bash and the python
repl. Presumably, a bunch of other repls and interactive command lines too.

~~~
wolfhumble
How do you typically go to e.g. beginning/end of line; via insert or normal
mode and which key combinations do you prefer? Always been using ctrl+a and
ctrl+e in emacs mode myself . . .

------
guruparan18
Very true. I have difficulties in training new recruits with UNIX. For some
reason, beginners have developed an aversion to UNIX/shell scripting. I myself
have started more and more of Ruby/AWK/Python, but it is always SHELL that
blows me off with the sort of impact it has on the daily work.

I ended up highly optimizing my work environment to replace long ssh
connections with `conn prod` (will use my RSA key and connect). File tranfers
with `go get prod filename` or `go send prod filename`. Another script that
can remember directory paths (I have to go over lot of them every day, and
typing long lengthy ones does not help).

To me, UNIX shell scripting is a lazy men's most effective tool, only if you
know how to use that rightly.

Loved the post. And thanks much.

~~~
codyb
I'm a relative newbie in the field, but I really love using terminal (I do all
my C work for grad school in ViM), it's really essential at work, there's no
faster way to do batch file processing a lot of times (especially with ffmpeg,
awesome).

I'm surprised people don't like terminal, I love using it. It makes me feel
old school, sort of like one of those hackers in the movies. That's probably
just a personal thing.

I was familiar with most of the concepts but the links section made things
click in that regard. Enjoyed the post as well.

~~~
endur
It just takes a while to be faster in the terminal than with the gui, and most
people won't switch from the thing they're used to until they see significant
advantages.

I remember when I first started using the terminal, it was slow and confusing.
You had to read the man pages for every little thing. Discovering the command
you want is tough, I'd be screwed without the internet.

Now I hate when I need to take my hands of the keyboard to use the mouse. What
a waste of time!

------
lukabratos
[http://xkcd.com/1168/](http://xkcd.com/1168/) ^_^

~~~
function_seven
I forget where I got this from, but this is how I remember it:

    
    
        tar -xzf # eXtract Ze Files!
    

EDIT: I missed chadzawistowski's similar comment below

~~~
Apofis
I don't understand why you need options at all... If you point it at a
file/directory it should automatically compress and if you point it at an
archive it should automatically decompress. The options should be there for
advanced tasks not 90% of the tasks you need the app for. It's really poor ui
design.

------
bcook
My most used bash command is probably "sudo !!", which runs the previous
command with "sudo" prepended.

The book "Unix Power Tools" showed me that higher-level programming languages
(Perl, Python, Ruby, etc) are not needed to accomplish most administrative
tasks. Awk is incredibly useful (but also very confusing, imo).

~~~
endur
I really like '!$' and '!:1' in bash. '!$' holds the last argument of the last
command and '!:n' holds the nth argument.

$ cd /big/long/path/to/this.txt Oops $ vim !$

~~~
DougMerritt
It should be noted for onlookers that this is the abbreviated form; !-3:4 goes
back to the third-to-last command and refers to its fourth argument, and
similarly !-3:$ is its last argument.

It's also apparently not universally known that bash has csh-style (similar to
sed) editing, so "s^foo^bar" will repeat the previous command with "bar"
substituted at for bar. So "ls foo" becomes "ls bar".

Similarly with !-3:s^foo^bar

I freely mix these things with readline-editing, depending on what needs to be
done.

Since csh gets a lot of disrespect these days, to give credit where due: these
are all Bill Joy features from the original csh -- it's a good thing that bash
borrowed innovations from various shells beyond Bourne shell and ksh.

Historical footnote: We donated code to him to do readline-style editing circa
1978, but he said raw mode was bogging down the pdp 11 too much. Systems were
a bit slower back then.

------
babuskov
Two very basic tips for vi:

You can use slash(/) and question mark(?) to search for text (forward and
backward) in vi. It's useful to know these, because they also work with
less(1).

If you are Windows user and used to press Ctrl+S to save the file and you
accidentally do that in Vi, your terminal would get stuck. But you can use
Ctrl+Q to unfreeze it.

~~~
madsravn
vi move commands works a lot of places in the UNIX world.

------
nicksuperb
_" Fact of life is, as you spend time in the terminal, you're probably going
to launch Vim at some point, whether willingly or not..._ _For all other
intents and purposes, just friggin use nano. Or sublime. "_

Words to live by.

------
olalonde
Tip: I recently looked up the man page for "which" and found out about the -a
flag which will list all instances of the executable found (instead of just
the first one). Incredibly useful when you have multiple installations of the
same software and it's causing problems. e.g.:

    
    
        $ which -a ruby
        /Users/olalonde/.rbenv/shims/ruby
        /usr/local/bin/ruby
        /usr/bin/ruby

------
kbaker
To exit vim use <ESC> :wq, not just :wq - most of the problem is it getting
stuck in insert mode, and not knowing how to escape into normal mode.

~~~
chriswarbo
My first experience of vim was trying to fix my xorg.conf from a tty. I
figured out how to save the file, but couldn't exit. In the end I hard-
rebooted :$

------
zan
Thanks guys, I'll collect these thoughts and add them to the blogpost. After
all, I want this to be available to all and every one of us!

------
voidhorse
I love playing around in bash. Most of the scripts I have right now are just
for setting up project templates, adding common packages etc.

Actually, I think most people who use computers at work, be they writers,
designers, whatever, not just programmers, would benefit from learning some
bash (or batch). You can automate a lot of useful processes with a few simple
scripts.

------
blueblob
don't type `cd ~` just type `cd`

~~~
lfowles
Also: Accidentally ran `cd`? Run `cd -` to return

I think there are some pop/push mechanics for working directory too, but I
can't find them right now.

~~~
GauntletWizard
The commands are pushd and popd, and they're bash (and zsh, and most sane
shell) builtins. Pushd to get to a new directory, popd to go back to the last
one on the stack.

~~~
OlivierLi
Tip : Setting cd as an alias for pushd make the whole thing much more
intuitive.

------
throw7
1\. I like tab-completion, but I can't personally stand bash's completion
feature. Be aware of differences.

2\. If you want to save your command without running it... comment it out with
a '#'. Bang, it's in your history.

------
tieTYT
This is probably misunderstanding the demographic, but why does he suggest
using nano over vim?

~~~
tensorproduct
Maybe they just prefer nano?

More likely, if this is meant to be a gentle introduction to the command line,
vim is a pretty scary place. Nano works much more like a "normal" text editor.

~~~
endur
Until you want to undo something! Nano is definitely more user-friendly
though. Some of the engineers I work with still don't know basic vim
shortcuts, like 'w' and 'b'

~~~
eridal
I use a lot of "yw", "d4w", or "dd" ..

are there others life-saving useful as these?

~~~
mryan
How about find and replace?

:%s/foo/bar/gc

The "gc" will apply the find and replace globally, and ask for confirmation
before changing an occurrence of "foo".

