Archive for the ‘vim’ Category

Vim – Add Line Numbers

Tuesday, September 8th, 2009

Have I mentioned that I love Vim? It is such a useful little tool. Let me give you an example. The other day someone gave me a messy list – a dump of email addresses from some database as a comma separated file. Here is a sample of how it looked like – note that instead of actual emails I’m using names of vegetables, fruits and other stuff (like Rupert, or poo for example):

test, poop, boob, apple, carrot, mango, kiwi, apricot, banana, apple, tomato, prune, cranberry, raspberry, orange, lemon, potato, pudding, lemonade, pants, spoon, flax, dogmeat, poison, pee, hamburger, rupert, apple, mango, sunflower, bee, pumpernickel, puddle

Of course the actual list had over a thousand emails and did not include Rupert (or his poo for that matter). It was equally messy, full of duplicate emails and basically looking like a wall of text. Someone requested it expecting a sorted, itemized list that they could print out and look at for reference. What they got was a text blob.

So I grabbed the file and opened it in the trusty old Vim and issued three commands. The first one was:

:%s/, /\r/g

Of course this is a single regexp. I’m replacing every occurrence of a comma followed by a space with a carriage return. This sort of unrolled my csv into a file with a single item on each line:

test
poop
boob
apple
carrot
mango
kiwi
apricot
banana
apple
tomato
prune
cranberry
raspberry
orange
lemon
potato
pudding
lemonade
pants
spoon
flax
dogmeat
poison
pee
hamburger
rupert
apple
mango
sunflower
bee
pumpernickel
puddle

To sort it, I simply did:

:%sort u

This sorted my list and removed the duplicates (the ‘u’ stands for unique list):

apple
apricot
banana
bee
boob
carrot
cranberry
dogmeat
flax
hamburger
kiwi
lemon
lemonade
mango
orange
pants
pee
poison
poop
potato
prune
pudding
puddle
pumpernickel
raspberry
rupert
spoon
sunflower
test
tomato

Last touch was to add line numbers to every single line. Yes, I know – I could print the file with line numbers enabled but the person who would be using this file was barely capable of using notepad. So the lines had to be hard coded. This is actually a new trick that I just learned and it goes like this:

:%s/^/\=line('.').". "/

The \=line('.') bit does the actual line numbering, while the .". " bit simply appends a dot and a space to each number so they nicely stand out from the actual items. The end result looks like this:

1. apple
2. apricot
3. banana
4. bee
5. boob
6. carrot
7. cranberry
8. dogmeat
9. flax
10. hamburger
11. kiwi
12. lemon
13. lemonade
14. mango
15. orange
16. pants
17. pee
18. poison
19. poop
20. potato
21. prune
22. pudding
23. puddle
24. pumpernickel
25. raspberry
26. rupert
27. spoon
28. sunflower
29. test
30. tomato

I’m putting this here as a useful tip – more for myself than anyone else. Chances are I will forget the line numbering trick in a few weeks and will need to look it up again. Hopefully some of you may find it useful as well.

To summarize: Vim is awesome. It is like a Swiss Army Knife for text files. Use it, learn it, love it!

The Directional Input Problem

Monday, March 9th, 2009

In the Friday discussion thread, Dr. Azrael Tod said something that resonated with me:

Problem with vim and its “less work for your fingers” is that i think its the wrong way to solve this problem. As example: vim uses hjkl for coursor-movement, so you dont need to move your fingers away from the main part of the keyboard to the arrow-keys. This is because and could have been solved via the keyboard-layout. This way this would be solved for EVERY application. Instead nearly every developer uses vim, so they dont see this problem and it will never be solved for the average user.

This is a very intriguing point. I do agree that the inverted T block of arrow keys is somewhat counter-productive. It’s almost as bad as the mouse, in that it takes your right hand away from the home row and thus slows down your typing considerably and introduces unnecessary hand movement where there ought to be none. If we could come up with a better keyboard layout that would use HJKL for movement and train users to use it we could improve typing speed for all applications.

But, there is a problem here. Or rather, a whole set of problems that we would need to overcome. For one, HJKL keys are normally used for character input. They can be used for movement in Vim because it is a modal editor. It uses one set of shortcuts in normal mode, and whole other set in input mode. Unfortunately most people out there find modal editors endlessly confusing. It’s not that they are hard to use, it’s just that they are a bit counter intuitive. Let me give you an example. To edit a document in a non-modal editor you:

  1. Use the main keyboard block to type
  2. Use the arrow key block and the square above it for movement

With Vim/Vi this is a bit more complex. To edit a document:

  1. If in normal mode, use HJKL and multiple other keys for movement
  2. To enter insert mode, you press i, a or one of several other keys
  3. If in insert mode, you use main keyboard block to type, but cannot move
  4. Hit Esc to exit insert mode and go back to normal mode

The former example is intuitive, and can be learned by trial and error. The latter must be trained, or explained to the user. Even if you had a keyboard with Vim shortcuts printed on select keys, the differences between the two (or more) modes would have to be communicated to the end user.

Modal editing by itself is not that great. It works well in Vi because the editors other features complement it. The large set of commands, the fact that you can chain them, record them and then repeat them via macros gives you an incredible amount of power. If you introduce modes to other applications, you will merely make them awkward. You would need to replicate the whole Vi experience. Doing it in a system wide manner goes beyond creating custom keyboard layout. It becomes a very complex process that would have to redefine many established methods for moving around within and between applications.

We could possibly avoid going modal, by requiring the user to press and hold some modifier key while pressing HJKL keys for movement. This sort of thing however takes us away from the Vim philosophy and introduces a new problem. Which modifier key do we pick? How do we prevent it from conflicting with each applications internal shortcuts? How do we train people to use it?

Something tells me this is not the right way to go either. Let me ask you a question – does any of you have a laptop which had the numpad keys mapped onto the main keyboard block? A lot of manufacturers will do this, and then let you access these keys via special modifier button (usually labeled Fn or something like that). How many of you use this feature? I have never actually seen people use it. Most folks who like to use the numpad on a daily basis, buy an external one and plug int in via USB port. Hardly anyone bothers with the modified one.

You would think that people would be all over this. You get the whole thing replicated literally on the home row, and around it. It ought to make entering numbers easier and faster. But it doesn’t. Why?

I guess the whole point of creating the numpad is to have a dedicated keyboard block for numeric input. It is shaped like a traditional, rectangular calculator face making it easy to use and intuitive. If you lift it up, and map it onto the main keyboard block it loses it’s appeal almost completely. People don’t like their keys to have too many functions assigned to them.

I think it’s the same for the arrow key block. The point of inventing it was to have a dedicated, distinctive group of movement keys. One that is easy to identify and intuitive to use. That’s precisely what the inverted T is accomplishing. Lift it up, and map it onto the main keyboard and you instantly make it less useful.

Vi and it’s derivatives are the only text editor that uses HJKL keys for movement. They get away with it, because their rich feature set makes jumping around in a document extremely easy and efficient. An expert Vi user hardly ever uses HJKL anyway. There are more efficient ways of getting around such as jumping by word, sentence, paragraph, seeking to character, string, jumping to front, middle or end of a line. You only use these keys to move 2-3 characters in a given dimension. Vim makes you work faster, not because it puts directional keys on the home row. It’s because it puts a million of efficient movement and editing commands there. The main problem it solves, is the one where people use arrows way to much to move around the document. The position of HJKL keys doesn’t really matter that much in the long run.

Vim will even let you use the arrow keys in the insert mode if you wish to. This feature is there to accommodate new users, but also because taking your hand of the home row to move around once in a while is really not that bad. As long as you efficiently use Vim’s other features, falling back on the arrow keys when in a pinch probably won’t hurt your efficiency that much.

We have the inverted T block for a reason. It was put on the keyboard to solve a different problem where each application would define it’s own movement methods and keys. We needed a dedicated movement block that could be used system wide, and it had to be separate from the main keyboard block to avoid conflicting with all the existing shortcuts. So they are a good thing to have.

The problem Dr. Azrael Tod alluded to is not really that the arrow keys are located away from the home row. It’s that most of modern editors really suck when it comes to efficiently moving the cursor around in the text. With Vim you can probably get to any point visible on the screen using 3-4 key strokes. This is not possible in most other text editing tools. You are pretty much limited to start of the line (Home), end of the line (End), paging screenfulls of text with PgUp and PgDown and slowly moving the cursor with the arrow keys. Sometimes it is actually faster to take your hand off the keyboard and use the mouse. It’s simple and intuitive but not efficient.

Vi fixes the efficiency problem at the cost of intuitive and straightforward design. The speed comes at a cost of a rich feature set, and dozens of keyboard combinations that must be mastered by the user before he can see a a difference in his typing speed. It’s a fair trade off – and one few other people are willing to make. Most gladly trade this efficiency for intuitiveness.

What is your favorite code editor?

Friday, March 6th, 2009

Happy Friday folks! I just wanted to tell you that I didn’t actually have a post ready for today until like midnight. Total writers block. Well, not total – I had couple of things in the queue, but nothing that could be reliably finished on time at my advanced level of fatigue and sleep deprivation. I just can’t do book reviews, or engaging rants while half of my brain is in a fatigue induced zombie like trance, and the other half is hyper due to high caffeine intake.

I had nothing up until around 3pm, when Mart asked about a good Python IDE in the AppEngine Bloging thread. I got my inspiration right there and then – thanks Mart!. Since I’m totally feeling lazy so I will do a poll on favorite text editors!

Quick search confirmed that I haven’t done this before. Perfect! We are officially back in business. Unfortunately I didn’t have a chance to type it up until around 1am on Thursday. If this post seems uncharacteristically brief, and more incoherent than usual, it’s because the only thing that keeps me awake right now is the sheer determination to finish typing this post before my body shuts down completely.

Let’s talk about text editors, especially when used for writing code. Are you a Vi/Emacs person? Do you prefer specialized IDE? Or do you hack in a plain text editor like notepad?

Favorite Code Editor
  • Add an Answer
View Results

Yes, Word is there as a joke. I really hope no one would seriously consider it as a legitimate code editor.

Also, as you can see I enabled the write-in option, but use it wisely. If I see it abused, I will disable it faster than you can type your favorite meme into it. Or at least I will try.

For me, it actually depends. I use Vim for most of the simple stuff like editing config files, shell scripting or simple programming that does not require me to jump between multiple files. I use Komodo Edit for large PHP projects. If I’m coding in Java I bite the bullet and use Eclipse. Yes, it takes an hour to launch and initialize itself, but it’s worth it. It works beautifully, and makes Java 40% less of a pain in the ass to work with.

Same idea with C# and Visual Studio. I actually use Visual C# Express 2008 for the little of .NET coding that I do, and it works very well. I tried couple of non-Microsoft IDE’s but the user experience was worse. I think they purposefully go out of their way to make .NET development without Visual Studio more of a pain in the ass.

Feel free to write in, and/or pimp out your favorite Editor/IDE in the comments. I’m off to sleep. This post will be queued to appear Friday morning and by that time I should be awake again, and slightly less sleep deprived. Have a good Friday people!