I'm going to go ahead and start this by saying I am not a master of bash and all things vim. This post isn't for teaching you ways to streamline your unixporn setup. If that's what you want, move on!
Who should keep reading are the people who want to use tmux, bash, and vim, and keep finding posts telling you to start copying dotfiles and configuring keys you haven't used. I'm going to walk you through getting comfortable with this software, explain some things that might be confusing to you, and hopefully motivate you to stick with changing your workflow.
If you happened to get dropped straight to part 2 here, I started this article originally to be an easy introduction to working in a terminal, using a terminal-base editor (specifically vim), using a multiplexer (tmux)... After talking to a few people about it and showing them some drafts, I realized I was still assuming too much background knowledge for a lot of people. To fix that, I split this into three parts:
- What is a terminal, how does your computer work - Part 1
- Tips for using bash well and why vim is better than your editor - You are here
- Multiplexers and Looking like a 1337 h4x0r - Part 3
Collectively the name (meant to be tongue-in-cheek) is How To Be a Terminal Wizard. You aren't going to be an expert from my rantings, but hopefully you'll have a good foundation and be able to use some other resources I offer to make your development environment better!
Let's start by running through how to effectively use your CLI. All the examples here are geared towards bash, but you'd be amazed how many will work on Windows too with little-or-no differences.
The first thing I like to let everyone know is that you can almost always exit a terminal application by hitting CTRL+C. When you get stuck, smash that a few times. If it doesn't work, many programs will helpfully tell you how to exit.
The biggest thing you need to know is essentially applying the UNIX philosophy. Bash lets you very easily pipe (ie: stream) output from one command into another. Distributed with your OS are lots of little programs that do one thing extremely well. By piping commands together, you can easily accomplish all sorts of ridiculous things.
How do you pipe output? With a pipe of course. Let's say we're looking in a huge directory for a specific file - hopefully you know ls
will list the files, you can then use grep
to search for what you want.
1 | $ ls | grep partOfFilename |
And you'll only see lines with that partOfFilename. Some other common things you might use are awk
, sed
, cat
, touch
, more
, ps
, top
, and less
. There's a built-in manual for all of these too, type man [cmd]
and you'll be in the documentation. Navigation can be a little tricky - you can use j
and k
to scroll and q
to exit.
You can also send data directly into files with >
or >>
: (the two greater-thans appends)
1 | $ echo "Check this out!" > test.txt |
Maybe you need to run two commands in order, like deleting a directory (clearing it out) and remaking it for some part of a build process:
1 | $ rm -rf build && mkdir build |
You could also use a semicolon (;
) instead of the double ampersand. The difference is that the semicolon just separates the statements, both will always run, while the ampersands will only continue running as long as the first command doesn't return an error.
That command above uses two switches (or options) on the rm
, the -rf
. That R stands for Recursive, and most commands have that option when you want to run it on an entire directory (including subdirectories). A common trick is searching for some string in files with grep -r variableName
. The F is Force, basically don't prompt for permission or check for errors. You want to be very careful using a force switch, but it is common when you know you want the option to happen and don't want to be asked if you're sure.
Finally, you might want to run things in the background, like firing off a server.
1 | $ node myserver.js & |
That's right, you just put an ampersand at the end. You can also use the single ampersand to run multiple commands in parallel instead of sequentially, if you wanted to run several things at once.
Now that you got it in the background, how do you get it back? You can run jobs
! That will list any background processes. You can then bring them back to the foreground by typing fg [number]
where the number is listed in the jobs
output, or start a stopped job by typing bg [number]
. Why would a job be stopped? You can hit CTRL+Z to stop and place any running program into the background; it's a good fallback when CTRL+C doesn't work.
Like I said, this isn't mean to be an in-depth guide, more a quick overview to what can help you be more productive and comfortable at the command line. In addition to the man pages, here are a few links that will help you dive into the world of bash:
Now let's talk about one of my favorite pieces of software in the world, vim. Vim stands for VI Improved, because it's an updated version of vi, an editor that itself added a visual mode to ex. Computing was a smaller world back in the 1970s! There's several cool things about vim, mostly related to modal editing (and that it runs in your terminal), but before we get to that...
I image some of you are probably wondering why you'd want to switch to some crazy old text editor. There's actually a few good reasons, not the least of which being they're available everywhere. Actually everywhere -- any computer you connect to, in virtually any way, from now until forever, will be capable of running the same programs, or one crazy similar. Also, since people have been doing exactly that with them for decades, most everything you could want has been done worked out.
Still not convinced? Aside from the mad hacker cred you get working in terminals, productivity from using the same tools everywhere, and unbelievably mature ecosystem, you'll also have an easier time doing your daily tasks. There's definitely a learning curve, but in a couple weeks you'll be just as productive if not moreso. Trust me.
Which leads me to my first suggestion: Just fucking switch. Seriously, do it. Now. Close Sublime, Webstorm, Atom, whatever, and don't go back unless you absolutely have to. It's going to suck... Deal with it. If you wait until it's convenient you'll never learn.
There's a nice tool called vimtutor
that'll run you through the basics. Do it a few times. Don't worry about memorizing everything, just go through the motions. I'd recommend installing vimium (Chrome/Firefox browser extension) too; you really want to get used to using letters as directions. You're already a good typist, right? This is all about getting you used to common combos, directions, and conventions that you'll use in vim (and other text-based programs). Basically every key is now a shortcut -- Embrace it!
The big change is exactly that - every key is now a shortcut. This is possible because vim has modal editing. You have to be in an INSERT mode to write text (and have your keyboard work as-expected), if you aren't actively writing you will probably be in the default, EDITING mode. It also has a VISUAL mode (in the name, right?) that's useful for selecting portions of text and editing multiple lines at once.
Along with this, vim introduces it's own kind of language or motions. You can stack any command, motion, or direction you're familiar with in intuitive ways. Want to move 3 lines down? Type 3j
. Delete the previous two lines? d2k
It takes practice, but the language of vim makes a lot of sense. Delete 2 up, move 3 down. As you learn more and get your common tasks under your fingers, you'll find editing text - what is a huge chunk of your time as a programmer, after all - gets much easier.
Lots of guides here will say to replace the default keybindings or start changing settings; I think that's terrible advice. When a key combination becomes a problem, change it. Don't change a bunch of things because it works for somebody else (it also ruins the whole "same everywhere" thing). It's not that hard to hit ESC or CTRL. The big thing that will speed up your adaption is to not spend much time in INSERT mode. Unless you're actively typing text, tap that ESC key. Move around with HJKL. Use /string and search. Bounce around with wbe
, wipe out text with di"
or df$
, and replace chars with r
.
You'll wonder why you spent so much time scrolling and highlighting in no time.
I'll link you to much more comprehensive guides shortly, but here's a super-small cheat sheet that can get you going:
Key | Action |
---|---|
ESC | Go back to EDIT mode |
CTRL+[ | Same as ESC |
CTRL+c | Exits VISUAL OR INSERT |
h | Move left |
l | Move right |
j | Move down |
k | Move up |
d | Delete something |
dd | Delete current line |
$ | Signifies end of line |
^ | Signifies beginning of line |
i | Enter INSERT mode |
o | Add new line below current position and enter INSERT |
O | Add new line above current position and enter INSERT |
: | To send a command to vim |
u | Undo |
CTLR+r | Redo |
Note that capitalization is important. vimtutor
will help you a lot getting moving around and with basic editing, after a few days of struggling you'll be moving around much easier. Another big issue for many new vimmers is copying text outside of vim - I like a program called xsel
, where I go into VISUAL LINE mode (with V
), select the lines I want, then type :!xsel -i
to copy text to the system clipboard. You can paste from there (in INSERT mode) with shift+insert.
I do recommend adding a few things to your vimrc to get started, though:
1 | " Basic config |
Basically this sets a longer history, turns on line numbers, turns on syntax highlighting, and sets some saner defaults for indentation (I like two spaces, YMMV). You will probably want to add some plugins to get extra functionality as well, I use vim-plug.
Here's some links that really dive into using, configuring, and grokking vim - If anything I said perked your interest, I hope you check them out and start trying it out today. For the record, Emacs is a decent choice too, but I hate the chords.
- Learning Vim in 2014 - Great Series
- StackOverflow answer on grokking vi
- Vim Adventures - Game to Learn Vim Keys
- Vim Primer
Please share feedback on this article, as I know it's a little brief on explanations. Go ahead and file an issue on Github! Next up we'll discuss terminal multiplexers, or how you can have multiple tabs, save your terminal sessions, and even share them across machines!