-
Open previously edited file in vim
You can open previous location by hitting
Ctrl-O. You can prefix the command with a number to go multiple files back. You can also travel forward in “file history” by usingCtrl-I.There’s a nice article on Vim Wikia with more details on a subject.
-
Old file version in split with vim
If you use git VCS, you can view previous version of the file you are currently editing in a split window by executing following command:
:vsp tmp | read !git show HEAD^:path/from/working/directoryFor the instant syntax highlighting, you can specify temporary file’s extension, like following:
:vsp tmp.py | read !git show HEAD^:lib/module.pyYou can also cycle back by few versions by replacing
HEAD^(which points to the previous commit) withHEAD~N, whereNis the number of commits you would like to go back in history by. For example, if you would like to get a version of the file 4 commits ago - you can do so by executing following command::vsp tmp | read !git show HEAD~4:path/from/working/directoryIt’s a pretty nice hack when you need to quickly view previous version of the file you are working on.
-
Pasting with vim and indentation issues
If you use vim often - you probably had to paste something into vim from the outside source. And, if you have corresponding indentation rules, they will get applied, turning your nice block of code into something that looks more like a case of stairs:
def foo(a, b): a, b = b, a print "I am doing something important." return a - bQuite nasty, isn’t it? But that’s where vim’s
pasteoption comes in. Before pasting, execute:set paste. If you go into insert mode, you’ll see the usual mode indicator switch to-- INSERT (paste) --. Try pasting the same block of code now:def foo(a, b): a, b = b, a print "I am doing something important." return a - bBeautiful. Don’t forget to switch back to a regular mode by executing
:set nopaste. -
Remap your Caps Lock
The following three paragraphs are an angry
Caps Lockrant. Feel free to skip past it or join me by commenting below.I’ve had it with
Caps Lock! How many times did I accidentally press it while hitting theAkey! How many times did I meanTaborShift! There is an obvious problem with theCaps Lockplacement, and there being only a millimeter of space to designate it from an adjacent key, it is quite difficult to notice when you accidentally press it.Pushing
Caps Lockis more tolerable when typing, but while using keyboard controlled software it’s a real pain;vimturns into a beeping ravaging nightmare,vimperatormesses up all your bookmarks… Same thing with websites supporting keyboard shortcuts.When was the last time I ever used
Caps Lock? Over ten years ago, when I was playing a video game that usedCaps Lockto switch between running and walking. Em… Seriously? Time to put an end this nonsense.Linux and Mac
Drop this into your
~/bin/capslockremap, and don’t forget tochmod +x ~/bin/capslockremap. Now run the script with root privileges (that’ll last you until the next restart).#!/bin/sh # This temporarily remaps the Caps Lock key to a Control key. # The keyboard will return to the previous settings after a # reboot. The Linux console and the X Window system each # handles keypresses separately, so each must be remapped # separately. First remap the X keyboard since this does not # require root access. # Remap the Caps Lock key to a Control key for # the X Window system. if type setxkbmap >/dev/null 2>&1; then fi # You have to be root to remap the console keyboard. if [ "$(id -u)" != "0" ]; then echo "This script is not running as root so" echo "the console Caps Lock cannot be remapped." echo "Perhaps you forgot to run this under sudo." echo "Note that this problem does not effect X." echo "This only effects the consoles running on" echo "Alt-f1 through Alt-f6." exit 2 fi # Remap the CapsLock key to a Control key for the console. (dumpkeys | grep keymaps; echo "keycode 58 = Control") | loadkeysWindows
Download Sysinternals Ctrl2Cap v2.0, run it as Administrator with
installflag:ctrl2cap.exe /install.Source CapsLock Remap Howto - Noah.org.
-
Python doctests and decorators bug
Now, this as well might be a feature, but doctest strings are not being executed for decorated functions (at least in version 2.7). However, there is a workaround.
You need to decorate your functions with
functools.wrapswithin a decorator to import docstrings into a decorator scope.#!/usr/bin/env python from functools import wraps def decorator(func): @wraps(func) def wrapper(): return func() return wrapper @decorator def foo(): """ >>> foo() False """ return True import doctest doctest.testmod()Now you can see this test failing, where otherwise it would have been ignored:
********************************************************************** File "decorator.py", line 12, in __main__.foo Failed example: foo() Expected: False Got: True ********************************************************************** 1 items had failures: 1 of 1 in __main__.foo ***Test Failed*** 1 failures.