Wednesday, November 6, 2013

Vim Gitgutter

Vim Gitgutter Vim Gitgutter shows a diff in the sign (left-hand) column of your editing window. This is a really cool way to see what portions of a file have changed without resorting to git diff.















Edit:
Droggl asked how to disable vim-gutter when viewing log files because of slowness. I've come up with the following solution which seems like it should work.

Assuming your log files end in *.log, add the following to your ~/.vimrc.
autocmd BufNewFile,BufReadPost *.log setlocal filetype=log
filetype plugin on
Then make an entry like so in ~/.vim/ftplugin/log.vim
:GitGutterDisable
Hope that helps!

Basic Recovery

The following commands are useful for recovering an editing session.

Retrieve a list of recoverable files.
vim -r
Recover a specific file.
vim -r .file.extension.swp (from swap files listed above)
Recover an unnamed (unsaved) file.
vim -r .swp
Or from inside of Vim:
:recover .file.extension.swp

Tuesday, November 5, 2013

Watching Memcached Traffic with TCPDump

Here's a fun little one-liner I just hacked together to keep tabs on the get/set commands coming in on a memcached server that I administer.
sudo tcpdump -i eth0 -s 65535 -A -ttt port 11211| cut -c 9- | grep -i '^get\|set'
It'd be easy to feed the output of this command into another script to aggregate key summary data over a given sample period.

Monday, November 4, 2013

Wrap Git With Fugitive

From the Fugitive Github page:

I'm not going to lie to you; fugitive.vim may very well be the best Git wrapper of all time. Check out these features:

View any blob, tree, commit, or tag in the repository with :Gedit (and :Gsplit, :Gvsplit, :Gtabedit, ...). Edit a file in the index and write to it to stage the changes. Use :Gdiff to bring up the staged version of the file side by side with the working tree version and use Vim's diff handling capabilities to stage a subset of the file's changes.


Quite simply, Fugitive provides a ton of awesome hooks for integrating Vim with Git's best features. Check it out!

Thursday, October 31, 2013

Cheap Process Monitoring: Echo, Watch, and Netcat

I frequently work with memcached, gearman, and a variety of other services that allow you to retrieve useful stats by connecting via telnet and issuing a status command. In a lot of cases, retrieving these stats in realtime is beneficial for debugging and diagnosing data pipeline issues.

Fortunately, a few basic tools found on almost any Linux system can make this easy.

The watch utility runs whatever command you provide it at the interval you specify. Here's the world's cheapest realtime clock.
$ watch -n 1 'date'
Netcat is too versatile to cover in depth in this post, but one of its most basic capabilities is accepting data via standard input, sending that data to a host / port combination of your choosing, and then dumping the result to standard output.
$ printf "HEAD / HTTP/1.0\r\n\r\n" | nc google.com 80 | grep Server
Server: gws
The above command takes the HTTP HEAD request from printf, sends it to netcat, which then passes it on to google.com on port 80. The results are printed to standard output, and the grep command limits the result output to the server returned (in this case gws).

Combining these two commands provides a ton of utility. Here are two useful examples that I use regularly.

1) Monitoring Gearman queues in realtime
watch -n 1 '(echo status; sleep 0.1) | nc localhost 4730'

publishMessage  4       0       1
consumeMessage  7       0       8
archiveMessage  28      0       12
.
2) Monitoring memcached get/set commands in realtime
watch -n 1 '(echo stats; sleep 0.1) | nc localhost 11211 | grep cmd'

STAT cmd_get 506
STAT cmd_set 8
STAT cmd_flush 0
STAT cmd_touch 0
STAT auth_cmds 0

Wednesday, October 30, 2013

Sensible Defaults with Vim-Sensible

If you have a fresh Vim install, and you'd like a sensible set of defaults, consider installing the vim-sensible plugin. If you've already installed Pathogen, installing the plugin can be accomplished as follows.
cd ~/.vim/bundle
git clone git://github.com/tpope/vim-sensible.git

Tuesday, October 29, 2013

Sort: Human Numeric

The sort command can accept input data formatted in human readable form. The du -h command is a longtime favorite for finding file sizes in a format that's easy to read with the naked eye. Coupled with sort, you can easily find and display the largest files in a directory tree.
$ du -h | sort -r --human-numeric | head
Update: I should have mentioned that --human-numeric is a recent addition to GNU coreutils, so this won't work on OS X or older distros. Here's a more generic solution (borrowed from here).
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11