How to Not Suck (at Being a Programmer)

November 4th, 2013 about shells, vim, git, and regex

One of the lamest features of being a programmer is the overabundance of trivial tasks that are just plain boring and time-consuming. Fortunately, our craft lends itself very well to enabling laziness: not in a negative sense, but rather, a productivity-boosting, awesome sense. Traditionally, renaming a set of 5000 files to conform to a new naming convention (eg converting .JPG to .jpg) would take a tremendous amount of effort and an equally tremendous amount of forever. By being lazy, a programmer can expend a miniscule sliver of that effort and time to write a simple script that automates the whole process.

Update: even easier than I thought.

I'm a big fan of being lazy, so here's a bunch of tips to help you become a big fan of being lazy!

Shells

Learn to love terminals / shells and the incredible power and flexibility they offer you. A GUI cannot compete with a shell when it comes to raw power, extensibility, flexbility, or anything else that actually matters. Furthermore, there may be situations where you're confined to only a terminal and can't access a GUI. Knowing how to manipulate a shell is an incredibly understated skill in the programmer's arsenal.

Text editors

Use a real text editor. That means one that works through ssh, without a window manager. That means you need to get used to using a terminal. If you disagree with me on this point, that's fine, and I respect your opnion, but you're wrong and I hate you. GUI text editors get a few pretty features that a terminal editor simply can't support, but in the long run, they pale in comparison when it comes to features, power, and flexibility. Notice a theme here?

Regular Expressions

Regular expressions are like find-and-replace on mega steroids. I can't even count the number of times I've seen another person poking around a file, replacing a dozen function calls with a similar but slightly different function call. Not only is this a totally anti-lazy way of doing things, but it's error prone: one poor function call may go unnoticed and remain unchanged, leading to headaches for future you or future maintainers.

Version Control

This is the most important of all the things in this list. Everything else on this list is possible to argue against, but no one in their right mind would argue against using version control. Not using version control is completely imbecilic and unprofessional. Version control acts as a safety net against accidental hardware failures, file deletions, explosions, etc. Beyond that, it provides a whole host of awesome features that you should want to use anyway: easy collaboration, quick changelog summaries, project histories, systems to test new features or experiments, and more. Use version control or go home.

How to Learn Vim (Actually)

March 16th, 2013 about vim

When you first start learning Vim, it just sucks. Everyone (except a few lucky people) goes through the "this is stupid" phase, and a lot of those people just give up on Vim because it's so different. Don't. Vim has multitudes of advantages over traditional editors and IDEs; notably, its incredible efficiency and ease of access through remote shells like ssh. Since this article is about actually learning Vim, however, I'll be carrying on now...

Get Comfortable

Seriously. So many hardcore Vim users are dead set on fully immersing yourself immediately: "turn off the arrow keys", "don't use the mouse", etc. Wrong. You have to be comfortable before you can learn. If you're immediately thrown into a completely new editor with no sense of familiarity, you're going to go insane. It's hard to adapt to so much at one time, so take it slow. Start off with the very basics: arrows move, i enters insert mode, <Esc> leaves insert mode. That's it. Get comfortable with those things. If you want to use a mouse, try using the set mouse=a option in your .vimrc. You'll grow out of these habits eventually.

Learn Something New

Once you're comfortable using insert mode and the arrow keys / mouse, learn a few more neat tricks (check the bottom of this post for some starters). Don't try to learn a dozen; just pick one or two. Get comfortable with them. There's no point in learning a dozen commands if you won't remember any of them. Once you've gotten pretty comfortable and mostly mapped them to muscle memory, repeat the process and pick out one or two more. Take it slow and go at your own pace. There's no point to going too quickly because you won't retain as much. Notice a theme yet? Be comfortable.

Keep Out of Insert

You wan't to stay out of insert mode as much as possible. Only enter insert mode if you're actually going to be writing something. If you're just moving around the file, even if it's just a few lines or characters, get out! You're wasting keystrokes. Most of Vim's efficiency lies in normal mode, so spending all of your time in insert mode is just plain wrong. If you have to enter insert mode every time you want to delete a letter, it's a minimum of three keystrokes: i<Backspace><Esc> or a<Backspace><Esc>. If you're rocking out in normal mode, it's one: x. If you want to delete five letters, insert mode will be a minimum of seven keystrokes, while normal mode will be two: 5x. What if you want to delete a whole line? In insert mode, you have to move all the way to the end of the line with the arrow keys, enter insert, and hold backspace until you're done. Or just stay in normal mode and press dd or 0D, depending on whether you want the blank line to stay or not. Stop using insert mode. It's bad.

Make Your Own .vimrc

Don't download a suite of plugins and fully configured files. Instead, just start with a nice color scheme and some "essential" settings like line numbers. Get what you "need" to be effective, but try to keep it to a minimum. Just like learning too many commands, if you configure too many options or plugins you won't remember what you've done. Just start simple and build it up on your own.

Take Notes

Even better, write them using Vim. Open a file in your home directory and write down every new command you learn with a short description of how it's used or what it does. Check back on it every once in while. Alternately, make a cheat sheet or even buy one. Even if you take your learning slowly, you might forget one or two commands after a day off. It's critical to review your notes or have a nice cheat sheet to refer to.

Learn Vim's Grammar

Vim's commands aren't discrete parts designed to be used separately; they were designed to be a fluid language that can be combined and used together to multiply the results. If you're trying to compound commands as one single command, you should take some time to read about Vim's grammar. It'll give you a huge boost once you start getting some of the advanced motions mastered.

Starting Commands

Inserting

  • i enters insert mode before the cursor
  • I enters insert mode before first non-whitespace character on the line
  • a enters insert mode after the cursor
  • A enters insert mode at the end of the line
  • o inserts a new line below the cursor and enters insert mode
  • O inserts a new line above the cursor and enters insert mode
  • <Esc> leaves insert / visual mode

Moving

  • hjkl move the cursor
  • h moves left (it's the leftmost key)
  • j moves down (it looks like an arrow pointing down)
  • k moves up (because)
  • l moves right (it's the rightmost key)
  • Learn these once you're comfortable with a few others.
  • 0 moves to the first character
  • ^ moves to the first non-blank character
  • $ moves to the last character
  • gg moves the cursor to the first line
  • G moves the cursor to the last line
  • :X or XG moves the cursor to line number X
  • e moves to the end of the current word, or the end of the next if you're already at the end of a word
  • w moves to the beginning of the next word
  • b moves to the beginning of the current word, or the beginning of the previous if you're already at the beginning
  • EWB are similar but use whitespace as the word delimeter instead of punctuation (commonly referred to as WORDs instead)
  • tX moves the cursor to the first occurence of character x
  • fX moves the character onto the first occurence of character x
  • TX is the same as tX but searches left
  • FX is the same as fX but searches left
  • /search will search the whole file for the term 'search'
  • n will find the next occurrence of your search term
  • N will find the previous occurrence of your search term
  • starting the search with ?search will automatically start in reverse search mode

Editing

  • x deletes the character under the cursor
  • dd deletes the current line
  • D erases everything on the line after the cursor
  • yy yank (copy) the current line into the default register (a register is like a clipboard)
  • p pastes the default register after / below the cursor
  • P pastes the default register before / above the cursor
  • :wq saves and quites; :w just saves; :q just quits; :q! quits and discards unsaved changes.
  • cc or S erases the current line and enters insert mode
  • C erases everything after the cursor and enters insert mode
  • s erases the character under the cursor and enters insert mode
  • ciX erases everthing inside the delimeter X and enters insert mode. X can be several items:
  • ( or ) will select inside the pair
  • [ or ] will select inside the pair
  • { or } will select inside the pair
  • ' or " will select inside the pair
  • w or W will select inside the word or WORD
  • t or T will select inside the HTML/XML tag
  • caX erases everything around the delimeter X (including X) and enters insert mode. You can use the same options as with ciX
  • diX and daX work like ciX and caX
  • rX replaces the character under the cursor with X
  • R enters replace mode, causing any characters typed to replace the character under the cursor

"Essential" .vimrc settings

Just add these to your ~/.vimrc file and Vim will load them every time it starts up. If you're looking for more powerful options, check my GitHub.

  • set number will turn on line numbers
  • set incsearch will turn on incremental search. This jumps to search terms as you type them
  • set hlsearch will highlight all matching search terms
  • set ignorecase will ignore case while searching
  • set smartcase same as ignorecase UNLESS you use a capital letter in your search
  • set tabstop=X set the number of spaces that represent a tab
  • syntax on turns on syntax highlighting

How to Learn Vim

February 17th, 2013 about vim

Well, at least that's what I was planning on writing today... Unfortunately, I got a little distracted by the idea of rewriting this web site to use Python instead of PHP. It's not that I hate PHP (I do) or anything, but I've recently become fascinated with Python. I've tried this before, but it ended rather quickly because I was completely unfamiliar with Python, web frameworks in general, and the specific framework I was using (Django). Since I've gained significantly more experience with Python, hopefully I'm much more prepared for attempt two.

Since I was originally going to write about learning Vim, I also wanted to show off this neat Chrome extension I just discovered: Vimium. Essentially, it adds some awesome Vim-style keybindings to the browser, such as hjkl movement, gg and G jumping, and /searching. If you're familiar with Vim, give it a shot. If you're still learning Vim, give it a shot. If you've never used Vim, give Vim a shot. If you've never heard of Vim, look it up and then give it a shot.

"Coming Home to Vim"

October 17th, 2012 about vim

I've read countless articles about using Vim, why to use Vim instead of other editors, how to make the big switch, etc. Among all of those articles, however, this is definitely one of the most thorough and comprehensive articles that I've read on the subject. Along with being fairly descriptive and explanatory, it doesn't disappoint when it comes to the things I've come to expect from Vim articles: an exciting and heart-warming tale about the author's struggle as they transition from a standard text editor to Vim's modal editing, and a list of useful features and commands that the author has collected in their time with Vim.

Vim sucks

Almost every article you can find about the switch from a standard text editor to Vim will tell you one thing: Vim sucks at first. The most likely reason for this phenomenon is the simple fact that Vim actually does suck at first. The change from a normal text editor is incredibly frustrating; even the most trivial of tasks becomes a monumental chore. It's suddenly a confusing mess of weird commands, keys, and modes. But then, as every article will tell you, it gets easier. Your brain starts mapping commands to muscle memory. Movement with hjkl no longer baffles you, toggling between command and insert mode becomes second nature, and you might have even learned a few nifty commands like yanking and searching.

The reason all of these articles are formulated the exact same way is because that's exactly how it happens. When you make the switch, you will hate it. You will be confused and wonder how anyone could possibly use this archaic system. But if you keep using it, eventually you'll start to find out how people can use it, and then you'll start to prefer it, and then you'll start to crave it.

Vim rocks

If you're lucky, the article you're reading will also give you a list of some commands and options the author has stumbled upon and compiled together for your education. My best guess for why this happens is the fact that there is such an incredible amount of commands that almost every person adopts a different subset to use on a regular basis. The sheer number of commands and level of customization that Vim offers is astounding, and that's part of what makes it suck at first. Being handed a sheet with even a tiny subset of the commands will be overwhelming, so most people are stuck using hjkl to move, i to enter insert mode, esc to go back to moving around your file, and :wq to save your file. After a while those commands get relocated from the "new" section of your brain to the "familiar" section, making some room in the "new" section for new commands, like / to search, dd to delete entire lines, and using numbers to multiply commands (eg 7dd).

This is my favorite part of Vim articles, because even though I've been using Vim for close to three years, I still learn at least one new command or configuration every single time I read a new article. Every author has discovered a unique subset that flows with their individual editing style, and almost every one of those subsets will have a command you've never even heard of before. Since this is technically an article about Vim, I might as well follow suit and list just a few of my most used commands:

  • :## jumps to line number ##
  • ctX and dtX will both delete from the cursor until before the first occurrence of character X. If you want to delete from the cursor until the first occurrence including the first occurrence, you can use f instead of t. Using c will place the editor into insert mode immediately after deletion
  • ci" will delete all text between two quotes and place the editor into insert mode. Useful for changing an entire string
  • ctrl-o jumps to the last position you were at. Very useful when I need to search for a line, make some changes, and then jump right back to where I was
  • >> and << will move your current line one indent forwards and backwards, respectively. You can also highlight multiple lines with v or V and indent them as a group with > and <
  • . repeats the last text modification (eg ciwbear changes the current word to "bear", . will repeat that word change)
  • gv will highlight the previous visual selection

The end.