Archive for the ‘Programming’ Category
Vim – starting from scratch
As part of my internship at Open Systems, I had to develop on linux boxes without graphical user interface. I had therefore to setup an appropriate development workflow, whose central piece was, of course, the editor. After seeing a colleague performing some “magic” in Vim, I decided that this was going to be my editor of choice. As I litterally started from zero, I thought it would be nice to share the resources that I found the most useful in learning Vim, and this is what this blog post is about.
First, some very basic notes on command line text editors. Vim (Vi IMproved) is, as its name suggests, the descendent of an editor called Vi. From what I’ve understood, Vi is no more in use nowadays, and Vim offers great improvements (e.g. redo something you undid) over Vi. There is a famous rivalry between Vim and Emacs, a competing editor, which I haven’t tried, but which might be an interesting alternative. I have to say, Vim is not intuitive for absolute beginners: you can’t just fire it up and expect to be able to edit your file. If you want a very simple and easy to use text editor, I can think of nano (a pico clone), which is far more intuitive than Vim.
Vim has a steep learning curve, but once you’re acquainted with it, you’ll find that the commands aren’t difficult to remember, and it will save you a ton of text edition time. Another advantage is its wide deployment. It is, as far as I know, a more or less standard tool in every POSIX-compliant operating system – including, for example, Mac OS X.
The most important thing to get at the beginning: Vim is a modal editor, where the two “most important” modes are
- command mode: this is the “default” mode, the one you are in when you start Vim. It is the mode where you can the most effectively view and manipulate the document.
- insert mode: where you actually can type some text! Switch to insert mode by pressing
ifrom the command mode. Return to command mode by hittingESC
A very deconcerting error, and certainly one you’ll make when you start (I can’t count how many times this happened to me) is to start typing something while in command mode. As nearly every key is associated to a command in this mode, this triggers a pseudo random batch of commands, and you will be totally lost! So beware, and learn to use undo (described below)
Here are the very, very few basic commands you need to know to get your head out of the water. You can open a file by typing vim file_name. Once you’re in Vim (in command mode):
:qexits the program:q!exits the program and discards changes:wsaves the fileESCexits from insert mode and switches to command modeiswitches to insert mode (you can then type some text)uundo
Now here’s the list of the resources that helped me the most:
- Why, oh WHY, do those #?@! nutheads use vi? – an essay which explains the philosophy behind Vi/Vim. This is the article that convinced me to try it.
- in your terminal, type
vimtutor– it launches an interactive Vim tutorial, and it is an absolute MUST! - Vim Novice Tutorial Videos – Nice screencasts, might be a little long for some of you, but it’s fun and requires zero previous knowledge.
- Vim Tips Wiki – One of my favorite Vim resource. It contains an impressive amount of information, in the form of a wiki.
- Vim Recipes – These “recipes” offer a result-oriented approach to Vim. It’s like a giant FAQ. It answers questions in the form “How do I navigate between files”, “How can I search / replace text?”, etc…
- Vim as Development Environment – A programmer explains how he sets up Vim (with topics such as syntax highlighting, …).
- Splits and multi-file editing – having several files open side by side was one of my primary needs. This explains how to do it.
- Graphical vi-vim Cheat Sheet and Tutorial – I love nice cheat sheets. This one is certainly one of the nicest you can find for Vim.
- Michael Goerz’s Refcard – Another cheat sheet. Not really for beginners, but a good reference once you are already experienced.
Serialization issues with SimpleXML
I’m using PHP for my semester project @ EPFL, and I stumbled upon a curious bug when trying to serialize an object (using PHP’s serialize()):
Node no longer exists
First I thought it was because of an infinite recursive loop (object A contains object B who in turn contains object A again, and so on), but in fact, infinite recursivity is allowed with serialize()!
Therefore, the problem was elsewhere: it ended up being an issue with SimpleXML, as explained here. SimpleXML’s parser creates strange, unserializable, “built-in objects”.
Until you try to serialize them, you never realize they aren’t just strings. An explicit cast solves the problem.
// isn't serializable. $track->URI = $first_track->location; // resolves the problem! $track->URI = (string)$first_track->location;