Vim is hard. Vim is scary. So was Git the first time I used it. If you stick with it, you’ll never want to use another text editor.

General

  • :edit reloads the current file

Line breaking

  • gqq breaks lines so they don’t exceed 80 characters, AWESOME!
  • :set textwidth=80 super handy for auto line breaking, use with gqq too

Sorting

  • :sort sort all lines by string, works over visual selects too
  • :sort n sort by number
  • :sort /*.\~/ sort by anything after the first instance of ~

Folding

  • zM fold everything
  • zR unfold everything

Searching

  • :set smartcase case sensitive if you use caps, otherwise not recommended
  • :set ignorecase makes searching case insensitive by default
  • :set number shows line numbers
  • :noh clears you last / search

Indentation

  • :set expandtab=false use real tabs
  • :set expandtab=true use spaces instead of tabs
  • :set tabstop=2 my personal favorites
  • :set shiftwidth=2 my personal favorites
  • :retab change existing tab characters to spaces depending on expandtab

Copy / paste

  • "ay yanks your selection into register a
  • "ap pastes the contents of register a
  • :reg displays the contents of named registers

Key binding

  • imap ii <esc> bind the key combo ii to the escape key

Screen movement

  • zz center the screen on your cursor
  • zb make the bottom of your screen flush with your cursor
  • zt make the top of your screen flush with your cursor

Etcetera

add autocmd BufNewFile,BufRead *.jade set filetype=jade to .vimrc to ensure .jade files have their type set correctly. Works for other stuff too.

Linux is hard. Really hard. I didn’t start getting good at it until I started writing everything I learned down. Maybe this stuff will help someone else.

Terminal

  • ctrl-u delete all left of the cursor
  • ctrl-k delete all right of the cursor
  • ctrl-w delete word left of the cursor
  • ctrl-y paste
  • ctrl-l clear
  • watch -n 2 date run the date command every 2 seconds and show the results as they happen fullscreen
  • tput colors display number of colors your terminal supports
  • echo $TERM shows what terminal you are using
  • watch -n 2 "dmesg | tail -30" continually watch the kernel messages for debugging

Recording sessions

  • script terminal_session.txt starts recording all the output from your terminal to a file
  • ctrl-d ends the recording session
  • less -R terminal_session.txt displays the session in its “raw” form for coloring, etc.

File system

  • du -sh * This command shows the file sizes for all files and folders in the current folder. Very useful.
  • mv ~/Linux/Old/!(Tux.png|Tux2.png) ~/Linux/New/ move all files except one
  • tree shows current directory as tree (usually doesn’t come with stock OS)
  • tree -H http://jondelamotte.com outputs an HTML tree of a site with a base href
  • pushd . pushes your current directory into a temporary array
  • pushd switch between current director and the one on the top of your stack, like alt-tab on windows
  • popd recalls the last stored directory and sends you there
  • tar -zcvf test.tar.gz /home/jon/test create a compressed archive and store it to a new file called test.tar.gz
  • tar -xvf test.tar.gz uncompress and extract an existing archive
  • tar -zcvf test.tar.gz -C /home/jon/test . create a compress archive without an extra folder in it
  • diff folder1 folder2 -q runs a diff on two folds and gives the brief differences
  • diff file1 file2 diff two files

Finding stuff

Search the root directory for anything with ‘something’ in it and pipe that to less:

  • find / -name 'something' | less
  • find / -iname '*something*' case insensitive with wildcard matching
  • find / -name 'something' 2>/dev/null searches all files/folders and redirects Standard Error output to /dev/null
  • grep -rn 'something' recursively grep through files in the current dir, show the line numbers
  • grep -rn 'something' --exclude-dir=node_modules exclude a directory from your search
  • grep -rin contact_bg ./ | cut -f 1,2 -d: case insensitive search for “contact_bg” in current directory, split the results on colons and only show the first two columns from the result, basically hide the contents of the result
  • mv {,new.}somefile.txt moves somefile.txt to new.somefile.txt using a bash feature called “brace expansion”
  • dpkg —get-selections lists packages that are installed
  • which man or which node find where your binary is located on the file system

Networking

  • /etc/hosts is a file that is like a personal DNS. You can map custom IPs to hostnames in it.
  • sudo netstat -tulpn display all the open ports on your machine

  • ssh -i ~/.ssh/id_rsa jon@10.10.10.10 log into the 10.10.10.10 machine with the user jon using the private key file ~/.ssh/id_rsa

User management

  • usermod -a -G youre_mobile jon add existing user to existing group
  • chmod g+rw srv add read and write permissions for the group to the srv folder

Release and renew IP

  1. sudo dhclient -r
  2. sudo dhclient
  3. sudo ifdown eth0
  4. sudo ifup eth0

Compiling programs

  1. ./configure
  2. make
  3. sudo make install install as root

Environment Variables

  • echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile adds a folder to your path for the bash shell
  • env list out all your environment variables
  • export SOMETHING="la la la la" set an environment variable
  • unset SOMETHING remove an env variable
  • echo $SOMETHING print out an env variable

Set time zone

  1. sudo rm /etc/localtime
  2. sudo ln -s /usr/share/zoneinfo/US/Pacific /etc/localtime
  • date —iso8601=seconds -u displays iso 8601 date time for UTC. Some version of linux don’t have this flag, see below
  • date +%FT%T%z displays iso 8601 local and works with all versions of date

Security

Generating a new ssh key

For the best security, you should be using one SSH key per device you use. An SSH key can optionally be password protected for extra security. When you generate a key, two files are created. One of the private key (don’t give it to anyone), the other is the public key, which is meant to be public.

  1. Make sure you don’t already have an ssh key you can use, check ~/.ssh
  2. ssh-keygen -t rsa -C "your_email@example.com"
  3. ssh-add ~/.ssh/id_rsa

Using ssh-add puts your private key into ssh-agent that stores your password and private key while your machine is running

Make sure your ssh-agent is running with ssh-agent bash

Ubuntu unity

  • alt + num5 hit any of the numpad numbers to align the windows
  • hold super key for keyboard shortcuts

Compilation

  • make -j16 number of threads, 2x your number of cores makes sense”

I haven’t been able to write many blog posts lately. I’ve been very busy and have lots to share, but for the time being I am going to release a series of cheat sheets I’ve been compiling for my own reference. Theses posts will continually be updated as I find more useful commands. I’ll kick the series off with Git; the best source control on the planet.

General

  • git show HEAD~5:file.txt shows a file from 5 commits ago
  • git diff HEAD^ — file.txt diff file.txt to the last commit
  • git diff master..remotes/origin/master compares local master to remote origin master
  • git diff --stat summary of changes by file and line number changes
  • git rebase -i interactively rebase your commits, for example to squash commits into one before pushing
  • git reset --hard HEAD WARNING: reset your directory to the HEAD state
  • git init initalize a repo
  • git add --all stage all changes
  • git commit -m "the first commit" commit the changes
  • git checkout master -- file.html checkout a file from another branch
  • git checkout HEAD -- file.html checkout a file from HEAD

Merge conflicts

  • git checkout --ours something.txt
  • git checkout --theirs something.txt

There are plenty of fun and quirky features in Javascript. Beginners typically have trouble with the for in statement. In most other programming languages, a for in statement lets you loop through each of the items in an array or list. Javascript won’t yell at you if you try to use for in on an array, but the result will be really weird. In javascript, for in is meant to loop through each of the properties of an object. Let’s look at a bad example, followed by a good one.

Bad

var arr = [1,2,3,4,5];
var sum = 0;

for (item in arr) { sum += item; }

console.log(sum); // "001234" // WTF?!

I won’t go into the details of why this is bad, just don’t use for in with arrays. You should be using forEach or a simple for loop instead.

Good

var jon = {
  name: 'Jon',
  age: 25
};

for (item in jon) {
  console.log(item + ' ' + jon[item]);
}   // name Jon
    // age 25

Prototype Pollution Problem

Even if you use for in correctly, you can still sometimes get unexpected results. It happens when you make changes to the prototype of an object. Let’s look at another example:

function Person(name) {
  this.name = name;
}

Person.prototype.getName = function() {
  return this.name;
};

var jon = new Person('Jon');

console.log(jon.getName()); // "Jon"

for (item in jon) {
  console.log(item + ' ' + jon[item]);
}   // name Jon
    // getName function .... // WTF?!

The latest version of javascript allows you to specify if you want a prototype method to be enumerable. I won’t go into the details of that. Rather, here is a good way to exclude prototype methods:

for (item in jon) {
  if (jon.hasOwnProperty(item))
    console.log(item + ' ' + jon[item]);
}   // name Jon

Recently I started learning Ruby on Rails. Mainly because it’s in high demand and ruby is supposed to be a clean, expressive language. Just being honest. It’s been pretty interesting so far. I started my professional programming journey in javascript and node, which has a very different philosophy than rails. In node land, the community respects small modules that have clearly defined responsibilities. It’s the developers job to string those small modules together to create something cohesive. Frameworks are generally looked down upon. Well, rails takes a very different approach. It happens to be a giant framework that bundles a ton of modules together for you.

The last website I built was on top of the express.js node framework. Express is a pretty minimal framework. It doesn’t give you all the bells and whistles that rails does. One of the big pain points I had was managing forms. Particularly with keeping the “create” page and the “update” pages in sync. Perhaps I was missing something in the express framework to help out. Early on with rails they showed the proper way to maintain a single view for both your “update” and “edit” forms. It was pretty thrilling to see they had already solved a problem I had and it came included with the package. On the other hand, I bet there’s a module out there that helps with just that issue.

I’m not sure I buy into either the “completely small modules” or “giant framework” philosophies. There are times when having features included in a framework can save a ton of time. There are other times when I want custom functionality that the framework author didn’t anticipate, it’s in those moments I wish I had small modules. Hopefully by learning rails I’ll be able to have a more informed opinion on the heated debate.