Vim Tips

Hey kids! Guess what time it is? It’s time for some Vim Tips!

What? Don’t make that face! Vim is awesome! Don’t give me that “B-b-but mistur Luke, we don’t use vim!” That won’t work with me again! You know damn well you should be using it. What is your lame excuse today? Is it “vim is to hard”? Suck it up you babies. Real men and women use vi and that’s that! There is only one excuse that I’m willing to accept, and that excuse is “I use Emacs”. So, how many of you whining babies use emacs? One person? Ok, you are excused – you can go sit over there. Your exercise is to write an elisp macro to do something interesting.

The rest of you, STFU and listen. No, I don’t care you use Eclipse on a daily basis. Just get the viPlugin and you will have the combined power of Vi and Eclipse at your fingertips. How awesome is that. No, don’t shrug – it’s fucking awesome, and you are gonna like it, right now! There, that’s better.

Ok, question from the back row. Yes, you – the guy with the stupid face. You use word? You know what – get the fuck out! Just get out and never come back. Actually, on the second thought wait. Sit your ass down and try the ViEmu thing. I tested it a while ago and it was pretty cool. Unfortunately it tad expensive and I could not justify purchasing it since I only use Word when I’m forced to. Still, it might work for you.

Anyways, this might be the first post of a series. I’m going to use it to drop little bits of vim lore that I want to remember for the future.

How do I use Vim input methodology for my text boxes in Firefox?

I think I mentioned it before but I use the It’s All Text! plugin. It puts a tiny little button next to all the textarea elements on the page. Pressing that button opens up the textarea contents in your editor of choice. For me that’s gVim but just about anything will do.

Hey, emacs guy – did you hear that? That one will be useful for you too. Write it down. MS Word guy, STFU! You don’t get to say anything!

There is another plugin out there called MozEx which seems to be doing something very similar. I haven’t tested it but it seems like it will also let you use vim for textareas.

I use It’s All Text! and my HTML doesn’t get highlighted

That’s because the plugin saves your buffer as a txt file. I got around this by simply forcing all text files to highlight as if they were HTML. It doesn’t really affect normal .txt files, but if you plug some HTML into one of them, it will look nice. If you want to do the same, just paste this into your .vimrc

au BufRead,BufNewFile *.txt setfiletype html

Luke, why is this post so long? How many words have you typed so far?

I don’t know, let me check…

g

I typed 520 words so far. I’m like halfway done so stop whining. Anyway, this is how you do it. Select some text, then press g and then Ctrl+G and you will see bunch of useful information in the status line. For example, the number of words you have typed so far. Cool, eh? It’s especially useful if you are trying to meet some word count requirement. Which, I usually don’t do very often as you might have noticed. I start typing, and then stop when the post seems like it’s done.

Heh, I wonder how many times you typed the word “vim” in this post?

Again, not sure, but let me check…

:%s/vim //gin

I typed it exactly 7 times. Yep, it’s another nifty trick – you can use the good old regular expressions in a non-standard way. For example the n modifier will prevent the replacement taking place in the regex above. Instead vim will simply print out the number of matches on the status line.

Finally, last tip of the day:

How do I delete everything from the cursor up to but not including a specified character sequence?

A little specific, isn’t it? But it is useful when you want to delete a big chunk of text. I mean, yes there is always the good old dt command which deletes till the first occurence of a character that follows it. There is also d) which deletes the whole sentence and d} which deletes the whole paragraph but this let’s you be very, very specific. Here is how you do it:

d/char-sequence

That is, you press d (or c if you just want to switch to insert mode), then press ‘/’ to enter search mode, enter a character sequence, and hit return. This also works with other commands so you can also do:

v/char-sequence

That’s all I have for now. I hope you enjoyed this quick batch of Vim tips. And if you didn’t I don’t care. The emacs guy over there seems happy because he found out It’s All Text! plugin and the MS Word guy is crying cause I called him names. Eh…

There might be more of these. :)

This entry was posted in Uncategorized. Bookmark the permalink.



14 Responses to Vim Tips

  1. Thanks for the d/

    I’ve been using dt and df (df is incuding the char), with quantifiers (d3d for 3 lines), movement keys (dE till end of whitespace delimited word, db *backwards to start of word*)

    I’m just a novice, though it is my exclusive editor on *nixes. Whenever butterflies are not around. ;)

    Reply  |  Quote
  2. jambarama UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Real men use ed.

    Reply  |  Quote
  3. jambarama UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Not even Bill Joy uses vi. Real men use ed.

    Sorry I couldn’t resist. Good post!

    Reply  |  Quote
  4. jambarama UNITED STATES Epiphany Linux Terminalist says:

    Damn, sorry for the double posting, I didn’t think the first one had gone through. Feel free to remove these posts at your discretion.

    Reply  |  Quote
  5. Starhawk UNITED STATES Mozilla Firefox Linux says:

    Just wonder whether ya have heard of or use Vimperator:

    Vimperator is a free browser add-on for Firefox, which makes it look and behave like the Vim text editor. It has similar key bindings, and you could call it a modal web browser, as key bindings differ according to which mode you are in.

    Reply  |  Quote
  6. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @Tormod Haugen: Dude, you totally need to star using the following:

    d) – deletes to the end of the sentence
    d} – deletes to the end of paragraph
    d$ – deletes till the next newline character
    d^ – deletes till the start of the current line
    dg$ – deletes to the end of the end of the screen line (ie to the point where the text wraps to the next line).
    dg^ – deletes to the beginning of the screen line

    In fact, you can combine these to do any motion. Skip d and you will just move by that much. Use v for hilighting. Use c to delete and go into insert mode. I find that I use these way more often than de and db stuff. :)

    Oh, and also instead of hitting Esc you can do Ctrl+[ so that you don’t have to lift your hand off the home row. :)

    @jambarama: I use sed sometimes for doing quick manipulation on text files sometimes. Does it count?

    @Starhawk: Yeah, I installed it once but didn’t particularly like it. I just prefer to browse with a 5 button mouse with the thumb buttons bound to back/forward. Vimperator just seemed a bit awkward. I’m sure I could get used to it, but I actually like to see my toolbars – I use bunch of bookmarklets that sit on my toolbar (for example one for posting to /dev/random and I have bunch of plugins that sit in the status-bar (gmail manager, twiterfox, firebug, adsense manager and etc). The headless, modal browser just didn’t really work for me.

    Reply  |  Quote
  7. Pingback: It’s all text! « G33KY^2 - The Nerd Strikes Back UNITED STATES WordPress

  8. Allan Viz PHILIPPINES Mozilla Firefox Debian GNU/Linux says:

    @jambarama: Remember this? :D

    Reply  |  Quote
  9. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @Allan Viz: Funny you mention this: exhibit 1, exhibit 2. Heh!

    Reply  |  Quote
  10. luca UNITED STATES Internet Explorer Windows says:

    nicely written.
    Thanks

    Reply  |  Quote
  11. @Luke Maciak: Dude?!

    I’ve been using those, except for the brackets ones. This has mostly to do with the fact that curly and square brackets lie on on the Norwegian keyboard. We’ve got a few extra chars at the right hand; æøå. is a bit harder when it is ;)

    I’ve become much better at vim since I found this handy cheat sheet two years ago. It even includes a tutorial! (It made me leave Emacs completely. Wasn’t good at that either, though. :)

    @Allan Viz: Nice. As you can see, I hinted at that one in my first comment. ;)

    Reply  |  Quote
  12. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @Tormod Haugen: Yeah, the cheat sheet is hanging over my desk at work. :) It is great, but it has a downside – you sort of need to know what you are looking for to begin with. For example you can’t really use it to answer questions such as “how do I move by this amount” or “how do I enter that mode”. Or rather you can, but it involves scanning the whole cheat sheet for the right key. It is great for reference, but not perfect when you are trying to learn new things. :)

    Reply  |  Quote
  13. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @Tormod Haugen: Oh, and Re:keyboard layouts. I’m probably pretty lucky that Polish Programmer’s layout is a straight QWERTY. We get additional letters like ąęółśćźż by pressing AltGr and the letter to be accented. The only tricky one is ź which is actually AltGr+x but you get used to that pretty quickly. :)

    Reply  |  Quote
  14. KillerSpaz UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Kick ass tips man… And I like your writing style :)

    I haven’t installed the plugin for eclipse, but ran across it a while back… what happens if you don’t pay for it? Is it nagware? Limited? etc?

    Actually, I’ve been making an effort to go BACK to VIM from Eclipse cuz it sucks up my resources…

    Check out my blog, i’ll be posting some more stuff on my vim as an ide tips.

    thanks again!

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *