This is the second part of a non-nerd's guide to the command line. You'll be definitively intermediate by the finish. To get started, go that way. →
Looking at contents of files is nice, but you can also edit them right
from the command line. The simplest way is arguably nano
. Go ahead
and save this file to your current working directory and
$ nano my-father-moved-through-dooms-of-love
You can move the cursor around in nano
with the arrow keys. nano
also has a bunch of shortcuts, which are all listed at the bottom. This way to learn what they mean. ↓
^
means "Control".
"WriteOut" means "Write the contents of this file Out to disc", which really just means "save". In the crusty world of shell, "write" is a common synonym of "save".
So to save the file after editing, hit ^O
, that is, Control-O.
To exit, ^X
(Control-X).
PS: "nanocuts" would make a good band name
PPS: So would "Crusty World of Shell"
vim
is weird & hard to learn, but a lot of people love it a
lot. You'll inevitably run into it on your adventures, so it'll
be good to know some basics.
So let's open it up.
$ vim my-father-moved-through-dooms-of-love
Ok, there's a lot to say; right this way please. ↓
Vim has modes. Not moods; modes. Maybe it's moody, too, though. idk
Vim starts out in "normal" mode. This is where vim pros spend most of their time. But a funny thing about Normal mode: you can't type anything in it.
Yes! Vim is an editor that, by default, does not let you type text! And though that sounds crazy, it turns out to be delightful, empowering, out-of-the-box thinking.
Find out why ↓
Vim pros, being good lazy coders, don't want to move their hands the whole painful way to the arrow keys. Have you ever heard of Repetitive Strain Injury? No thank you! To learn The Vim Way, stop reaching and try:
j
to move downk
to move uph
to move leftl
to move rightAaahhhh… All right on home row! Stop moving those hands! For more vim goodness, right this way ↓
u
to undo.
Messed something up? Make sure you're in normal mode, then type u
. Phew!
To redo, type ^r
(that's control (not command) and r
. r
by itself
was already taken!)
Even though j
and k
move a cursor rather than type "j" or "k",
Normal Mode is great for manipulating text. Try:
dd
to delete the current lineD
to delete to the end of the current lineyy
to yank (copy) the current linep
to paste whatever you last deleted or yanked>>
& <<
to indent & outdent the current line6dd
, 5D
, 4yy
, 3>>
, & 2p
– guess. Try them..
to redo the last text-changing commandBut what about, you know, actually typing? ↓
To insert text, you need to put vim into Insert Mode. Once you're done
typing, hit escape
(top left of the keyboard) to go back to Normal Mode
asap. Try:
i
& I
to insert before cursor/linea
& A
to append after cursor/lineo
& O
to open a new line of text – try it!C
to change to the end of the lines
& S
: substitute (replace) current character/line.
same thing as before. Worth mentioning twice.How to save all these beautiful edits, though? ↓
:
not 💩)
Vim has a whole "command mode" that isn't really a mode. While in Normal
Mode, you can type :
, followed by a command. For example:
:w
will save (because "write" means "save"):q
will quit vim:wq
, :x
, or (even lazier) ZZ
will save and quit:!mkdir hey
will run mkdir hey
in your shell – you can run
any shell command right from vim this wayWee! You know so much vim now!
But how to remember it all? ↓
Vim is best learned in little chunks. If you memorize the commands you just learned, you'll already be as fast (or faster) with vim as with your current editor.
So bookmark this page, and keep coming back until you don't need to. But better yet, bookmark this cheat sheet, and refer to it constantly while you use vim. After a couple weeks, you won't need it anymore.
And one last thing. We all like pretty things. Let's make vim prettier. ↓
By default, yes. Vim is ugly. But with a configuration file, it too can have colors and remember indentation levels.
Save this file into your home directory as .vimrc
. It's a
simple file—open it (with vim!) and you could definitely figure out
what it's doing. Each line of it is actually something you could type in
a colon command (try it out!).
If you want to make it even prettier, check out these dotfiles. Using plugins, they add nice things like a file navigator sidebar, quick file finder, and super-fast folder-wide search.
Some programmers get really territorial and religious about their editor
of choice. They make gang signs for vim
and emacs
(another popular
editor). It's silly.
Vim uses a ~/.vimrc
file for configuration. Bash uses a ~/.bashrc
.
rc
stands for "Runtime Control", which doesn't shed a whole lot
of light. Just remember that rc files are configuration files.
Let's customize it! Open ~/.bashrc
using vim, and
alias
yourself a shortcutLet's pretend you have to change to your desktop all the time. You're sick of typing this:
cd ~/Desktop
You're in luck! You can add a simple alias
to your .bashrc! Add this:
alias d="cd ~/Desktop"
Now try it out. Save and exit vim (remember how?) and enter d
.
Before changes to your .bashrc
take effect, you need to source the file.
Your bashrc is automatically sourced when you open a new shell. So open
one! (Press ⌘T for a new tab.) And now press d
. Coool. 😎
But now head back to that original shell. Enter . ~/.bashrc
(shorthand
for source ~/.bashrc
). Now enter d
. Sweet!
Ok, one more thing ↓
Here's a nice introductory .bashrc file for you. Go ahead and download it, saving to your home folder as ".bashrc" (with the dot!).
Open it with vim and play around!
Wondering what else you can do with a bashrc? Search Github for lots of other ideas!
Look through the .bashrc you downloaded for instructions on how to make your shell look like this:
You're not allowed to do certain things on your computer, because you
could cause serious damage. You could literally erase your whole hard
drive by running something like rm -rf /
(don't do this!!). Since no one wants to
accidentally destroy things, you have to do extra work to change system
files. You need root access. You need to be a super user. An
administrator.
This works differently on Windows than anything else. This way for instructions on any system. ↓
On most *nix-based systems these days (unix, linux, macOS), you're not
allowed to just run around as a super user because that's scary.
Instead, you prefix specific commands with sudo
:
$ cd /
$ ls
$ mkdir bahaha
$ sudo mkdir bahaha
When you type in your password, you won't see it. You won't even see splats. That's intentional, just type your password into the void & hit Enter.
/
is the root directory, which is where your whole hard drive's
filesystem starts. When you cd ..
here, you stay here. You're not
allowed to change things in here unless you sudo
, which means Super
User DO.
On Windows, you'll need to find the Git-Bash icon either on your Desktop or in the Windows Menu (if you pinned it to the taskbar, this won't work there.) Right-click the icon and select "Run as Administrator". Now you can live as dangerously as you want! For example, you can now:
$ cd /c
$ mkdir woohoo
$ rm -r woohoo
By the way, you might want to rm -r bahaha
now.
Sometimes the inbuilt stuff just isn't enough! If you want more, you'll need a package manager.
$ cowsay package managers rock!
_______________________
< package managers rock! >
-----------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Yes! An ascii cow! This way to get your own. ↓
The preferred package manager for your Mac running macOS these days is Homebrew. An older way was MacPorts, so you might still hear people mention that now & then. You can follow the link to Homebrew to see how to install it, but once you do, go ahead and
$ brew install cowsay
$ cowsay Hooray!
Note that Homebrew may whine about not having Xcode (or a
compiler). Luckily, for cowsay
, you don't need it. But for some
things you might, so feel free to download Xcode from the Mac
App Store (you only need the command line tools for Xcode, but
to just get those you need to sign up as an Apple developer. But that may
be faster, since Xcode is a huuuge install.)
If you're using Linux, then you already have a package manager that you
can use. On Ubuntu, you have two options, apt-get
or aptitude
.
aptitude
seems to be preferred by some people because it's better at
uninstalling things. So:
$ sudo aptitude install cowsay
$ cowsay Hooray!
If you're using a different version of Linux, then instead of aptitude
you might have yum
or something. Just search the web for what package
manager to use with your flavor of Linux.
No idea. If you know, please tell me.
Now and then, when n00bs ask for help on forums, the crotchety enlightened will respond with a curt "rtfm". What manual??
Usually, every unix program comes with a manual (also called a "man page"). To read it, just do this with any command:
$ man ls
This may open in less
. Remember less?
Go ahead and check out the manual for some of the programs you've
learned about (less
, cowsay
, etc).
Look at the slash key on your keyboard that's above the Enter key
(that's the backslash, by the way; the one on the ?
key is just
"slash"). The vertical line above the backslash is called the "pipe"
character.
The philosophy of unix is that programs should do one very simple thing, and do it well. Then you can use pipes to hook multiple programs together. Try this:
$ ls ~ | less
You piped the results of ls
into less
! There are other kinds of
pipes, too. ↓
Try this:
$ echo "So what?"
Indeed, so what? echo
just spits out what you tell it to. But with
pipes, it gets cooler.
$ echo "Here's something" > tmp.txt && less tmp.txt
The >
pipe totally overwrites a file. Try this now:
$ echo "& more." >> tmp.txt && less tmp.txt
The >>
pipe appends to a file without destroying what was already
there.
There's an interesting twist on the >
pipe: it doesn't need anything
before it. Try this:
$ > tmp.txt
You just piped nothing into tmp.txt. You overwrote all of its contents
with nothing. Open it up (cat
it, or more
it, or whatever-you-like
it) and you'll see that it's now empty.
To force quit a process that seems to be stuck, just hit control c (not ⌘ C, macOS users!) Let's try it. Copy & paste this command:
$ I=0; while [ $I -lt 100 ]; do \
echo $I >> count.txt && let I=I+1 && sleep 1; \
done && less count.txt
Long command! But now you're in familiar-by-now less
, looking at a file
called count.txt
. It's a boring file right now. Just a 0
. But wait,
there's more! Press shift f
to follow the file. omg, counting!
But now less
is stuck! Aha. Press control c to stop
following the file. Then you can q
to quit, just like normal.
You know a lot about the command line.
Go boss your computer around.
Good for you!
Go learn about ssh
(secure shell)
Digital Ocean has a great guide to setting up your very own server.
Once it's set up, you can ssh
into it. Then you'll have command-line
access to a whole different computer. A computer in the cloud! All of the
commands you just learned will still work on it.
You're ready to rule the world.