Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Monday, October 5, 2009

Kill the CAPS LOCK!

The Caps Lock key can do wonders to wreak havoc in VI. You're in command mode, minding your own business, when all of a sudden all hell breaks loose. All because you pressed the caps lock key instead of the shift key.

For a long while I was reluctant to follow the plethora of tutorials on how to kill the caps lock key. The reason is that there are several cases where I thought it was very beneficial to actually use it to, well, lock the caps. For example, by tradition, definitions in C/C++ are written in all capital letters. I finally figured out how to get along without the evil key, as I will explain below:

g~w - capitalize one word
g~~ - capitalize the entire sentence
gU{motion} - capitalize motion

I still have some open issues. For example, to capitalize the word I just wrote, I have to go backwards ('b') and then capitalize it ('g~w') and then go back to insert mode ('A'). I'll be happy to learn of better options, options that take into account that I despite having to configure my setup. Nonetheless, I'm now much better off than I was with the occasional caps lock accident.

You can now make the caps lock key do something else, such as behave as a shift key or an esc key.


Further Reading:

Saturday, September 26, 2009

Python, Subprocess and Multiple Arguments

Calling external programs from within a python script is a pretty common task. However, most of the times when I want to pass some arguments to the external program, I usually get stuck. The official Python documentation for the subprocess module is a bit lacking, in my humble opinion, in its treatment of argument passing.

This is not to say that all the information isn't available in the official documentation. In fact, it is. It's just not where I always look for it. The first thing I look for are examples. I simply do not have time to read the entire documentation page for something which should not be too difficult.

The basic idea is this:
  1. Either provide a string of the entire command and argument, but use Shell=True
  2. Provide a string for only the command
  3. Provide a list of strings, where the first item is the command and the other items are the arguments
It's the third option that keeps on giving me trouble, so here is a simple working example for reference:
 1 #!/bin/env python
 2 import subprocess
 3 
 4 program_name = "ls"
 5 arguments = ["-l", "-a"]
 6 
 7 command = [program_name]
 8 command.extend(arguments)
 9 
10 output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
11 print output


Passing Arguments with Quotes
If you want to pass arguments that are passed with quotes in the shell, then just pass them as a single list item, without the quotes.

Links
  1. James Gardner wrote the most comprehensive introduction to the python subprocess module I've encountered.
  2. The official documentation.

Further Reading
  1. Bash: Passing Arguments with Quotes
  2. Why Not to Write Shell Scripts