Monday, December 29, 2008

Just Keep Vimming In Tabs

Lately, I've been using vim more frequently. Here are a few useful tricks that I've picked up.

So the feature for viewing multiple files in tabs has been around for a while, but I only just discovered it. You can open up multiple files in tabs with

vim -p file1 file2 .. fileN

You can go to the next or previous tab with :tabn or :tabp. And you can edit a new tab with :tabe file_name. Close all but the current tab with :tabo. Annoyingly, I haven't found a single command that just quits and closes all the tabs, but :tabo followed by :q will get you there.

Also, since I use a black background for my terminal, the default font color for the selected tab name wasn't visible. You can change this color with the following command:

hi TabLineSel ctermfg=white

Also I wanted to remap the gt command used to toggle between commands to the Ctrl-T keystroke which is closer to what I use for tabs in Firefox. This can be accomplished through

map ^T gt

but it is tricky typing in Ctrl-T because it's mapped to something else. You need to type Ctrl-V first (right before typing Ctrl-T) to make this work.

You can put all these commands in your .vimrc file so that they get loaded every time you edit a file.


Tuesday, September 23, 2008

Python Silliness

So I was implementing the K-Means and the Fuzzy C-Means algorithms in Python for a class assignment and something just wasn't working. I double-checked my logic several times and everything seemed right. And indeed, the logic was right, I just got into trouble using some Python shortcuts.

The * operator in Python is overloaded to allow you do cool things like

a = 'h'*5 # stores 'hhhhh'

So naturally when I needed to initialize a list of lists, I did something like

t = [[]]*3

and expected to get a list with three empty lists. Then here's where I made my error. If you do something like this


you might think you should get

t = [['hi'], [], []].

That would make sense, but what you actually get is

t = [['hi'], ['hi'], ['hi']]

and that can mess up your logic if you're not careful. What I really wanted was the more verbose

t = [[] for i in range(3)].

In retrospect, it makes sense that the * operator just creates more pointers to the object I'm multiplying, not completely new objects. Thus when I change the underlying object, I change all the copies. But still this wasn't entirely intuitive and is a nasty pitfall to watch out for.


Tuesday, September 16, 2008

Harry Potter Politics

So that was a looong hiatus, and I'm going to try to start blogging more regularly again. For most of the past year, most of my writing has been in the form of hard news stories for The Tech or the occasional essay for a HASS class. While I did have a lot of fun with that, I also feel the need for some more free-form writing as well, if for no other reason than for my own sanity.

To start things off, here's my first (of hopefully more) non-news piece for The Tech: Potter Politics.