Monday, June 27, 2011

Cat to or from clipboard in Terminal

On a Mac, man pbcopy or pbpaste to see how to redirect into or out of your pasteboard/clipboard. With pbpaste, -Prefer txt can be useful to ensure ascii output.

cat file.txt | pbcopy
pbpaste -Prefer txt > out.txt


Yay.

Tuesday, June 21, 2011

Rotate tablet PC screen with a single button in Ubuntu 11.04

Someone asked me for help on tablet screen rotation in Ubuntu for an older (2006) Lenovo X60 tablet PC - in particular, the desire was to make it as simple as possible to flip the screen 180 degrees since on this model, for right-handers, a flipped screen is more comfortable ergonomically for....tableting. Simplicity suggested a big noticeable blue button on the Unity 11.04 Launcher (the slide out bar on the left). Being new to Unity myself, there were a couple of quirks to figure out.

My goal was to add a button to the launcher that would fire off a shell script. The script relies on xrandr (for screen rotation) and xsetwacom (for the stylus to map correctly to the rotated screen). They're easy to use after some manpage-skimming and they don't require elevated privileges.


Now, how to add it to the Unity launcher bar? Either you can create a .desktop file from scratch or you can use the menu option for creating a launcher, and repurpose the .desktop to point to your script. Here's the one I used, which also points to a serviceable picture of a generic monitor icon, courtesy of Gnome.

After dragging it to the launcher and a re-login, the button does the intended trick.

And here are a couple of useful links to ubuntuforums.org posts that told me everything I needed to know to rush out the above tidbits.

Thursday, June 2, 2011

Keeping your idle ssh connection alive (the wrong way)

Need to keep an ssh connection alive? Maybe you're doing it wrong - use nohup. Can't? Got some weird corner case that needs a living ssh connection with some process doing something in the foreground? You could always use expect/pexpect. But that's too much trouble! This python script is a quick and dirty way of doing it wrong:
import time
import sys

def infinite():
    i = 0
    while 1:
        print "been asleep for ", i ,"hours"
        time.sleep(60 * 60)
        i += 1

def sleeper(minutes):
    print time.strftime("%a, %d %b %Y %I:%M:%S +0000", time.localtime())
    time.sleep(minutes * 60)
    print 'awaking'
    print time.strftime("%a, %d %b %Y %I:%M:%S +0000", time.localtime())

def main():
    if len(sys.argv) != 2:
        print "fail!"
        return
    if sys.argv[1] == "infinite":
        infinite()
    else:
        sleeper(int(sys.argv[1]))

if __name__ =="__main__":
    main()

Usage:
python keepawake.py 60 
Sleeps for 60 minutes
python keepawake.py infinite 
sleeps until you Ctrl+C

If you're using this often (why?! You're doing it wrong!) then of course just throw in an alias within your .bashrc:
alias keepawake='python ~/path/to/keepawake.py'

And now you can call:
keepawake 60
keepawake infinite
whenever your heart desires.

Get colors from Terminal, and pasting into vim from other programs

In seeking an answer to copying terminal output with ANSI-style colors, thanks goes to this question. I wanted to get colored output from Mac OS X's Terminal, and the easiest way was to print to a pdf and copy from there.

Also, here's the right way to paste into vim from an external source (such as a gist) because I seem to forget this (since I'm usually pasting from vim into vim). To avoid vim's auto-indenting or auto-commenting as the lines are written from the pastebuffer into vim, to enter paste mode just
:set paste
and then, back in insert mode, Ctrl+v or Command+v or whatever suits your fancy. Once you're done,
:set nopaste
You've now sidestepped all that pesky smart indentation/commenting.

Usable parenthesis matching in vim

I'll post my messy hodgepodge vim configuration below but the only part worth highlighting (no pun intended) is the following trick for making vim not do terribly confusing parenthesis matching:

   hi MatchParen cterm=bold ctermbg=none ctermfg=red

Serviceable explanation here. It's pretty self explanatory though: this changes the foreground (and background if you so choose - yellow is serviceable) of the matched parenthesis to something a little more distinctive so you don't lose track of where your cursor is. Default matching always makes me confused whenever I'm navigating around, especially in parenthesis-heavy code. And I guess it's nice that you can just use color names like white or green, even though that bugs me, because I have no idea if it will support, say, fuchsia... or glaucous. (I think it probably just supports basic 256-color palette names, but feel free to comment with a link to the specification or vim source)

And for posterity, my whole vimrc, with some comments mixed in:

More vim tips to come in the next post.