Friday, September 30, 2011

Vim's text objects

Let's say I have this line in a file:
logging.info("some boring message")
and I want to change it to:
logging.info("request served")
For a long time I did it this way:
1. keep the cursor on 's' of 'some'
2. type ct" (which means change till (the first) " character)
3. type request served.

Recently I figured there's an easier/faster way:
1. keep the cursor anywhere inside the "some boring message" string
2. type ci" (which means change inside quotes)
3. type request served.

Like every Vim feature, this is just one among a dozen or so possible selections.  Check out text objects section in Vim manual.

Saturday, August 6, 2011

Shortcut key to switch to any app

I use the command line quite a bit, so I always keep a Konsole window running.  Since I frequently switch to the Konsole window, I thought it'd be much faster if I can bind a global shortcut key.  Like with most things in Linux, the solution is only a Google search away :)

I had to install wmctrl first (sudo apt-get install wmctrl on Ubuntu).  And then I bound the shortcut key Ctrl+Shift+K from my KDE's settings dialog to run the command wmctrl -x -a konsole.Konsole.  -x says that I would be specifying windows using their WM_CLASS values; -a activates the window that follows it.  To get the list of currently open window with their WM_CLASS values, I used the command wmctrl -xl.

Sunday, July 31, 2011

Finding information about commands you use

Q: How to find out where the binary of a command I'm running?
A: You can use type command (available on both zsh and bash):
% type ls
ls is an alias for ls -h --color=auto
% type cat
cat is /bin/cat
% type alias
alias is a shell builtin

Q: Sometimes I want to know if a command is a shell script or a compiled binary.  How do I do that?
A: If you use zsh, you can use =command to get to command's full path.
% file =backup
/home/manki/d/bin/backup: a /bin/rbash script text executable
% # To show you what =backup actually translates to
% echo =backup
/home/manki/d/bin/backup
If you use bash, you can use type command within backticks or the equivalent $(...).
$ file `type -p backup`
/home/manki/d/bin/backup: a /bin/rbash script text executable
When you have aliases, this can get tricky.  On bash I don't know how to do this, but zsh is smart enough to find the executable even when you have aliases set up.  For instance, I have aliased ls to ls -h --color=auto, but zsh gives me the right binary for =ls:
% file =ls    
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped

Thursday, July 14, 2011

Saving the input to `less' command

less is good for viewing long outputs from other programs.  There may be times when you pipe the output of a program to less and realise only later that you want the output in a file... maybe because you need to send the output to someone via email.

You can quit less and change the command line to output to a file.  But there are better/faster options.  less supports an s command to save its input to a file.  Just press s, enter a file path to save the text to, and you're done.

Tuesday, June 7, 2011

Watching Apple's Keynote Videos in Linux

Essentially, you’d have to dig through Apple site’s HTML/JavaScript and find the video URL for Windows.  And then pass that URL to a stand-alone media player like VLC.

Apparently Chrome for Linux has a QuickTime plugin!  (Or it’s bundled with Ubuntu; I am not sure.)  Watch Sep 2012 event (iPhone 5 launch) is available at goo.gl/Nn77F.

Watch Mar 7, 2012 iPad event video using this command:
vlc 'http://stream.qtv.apple.com/events/mar/123pibhargjknawdconwecown/12oihbqeorvfhbpiubqnfv3_650_ref.mov'

Watch Oct 4, 2011 iPhone event video using this command:
vlc 'http://stream.qtv.apple.com/events/oct/11piuhbvdlbkvoih10/11mnbzxcbnkhbvshdfeuygxvst10_350_ref.mov'
-----
If you have a Linux computer, you cannot watch Apple’s WWDC 2011 keynote video from Apple’s web site.  Because the site picks a video URL based on what your browser/OS is, but it fails miserably if you don’t run an Apple OS or Windows.  So, if you want to see the video, open http://stream.qtv.apple.com/events/jun/11piubpwiqubf06/11piunpaiubsdvadibvas06_350_ref.mov on VLC Media Player and you’ll be good.