Tuesday, December 09, 2008

Registers

There is always something new to discover out there, particularly with Vim. I thought I knew how registers worked: you got your window manager registers (+ and *); the 'last thing I deleted' register ("); the confusing 'last 10 deletes' registers (0-9); some read only registers that I don't consider to be registers because, hey, you can't write to them (% and #); and then the multipurpose named registers (a-z).

I've programmed myself over the years to use specific letters for specific purposes. The 'n' and 'm' registers are reserved for recorded macros (yes, macros are saved in registers - handy to know if you want to save a macro in your .vimrc for later use, say), the 'iou' I tend to use for registers that are used in a macro, and 'fvc' for yanking and storing.

For instance, if the lines below existed, and I wanted to copy the first one for later use:

every good boy
does good

I'd do: "fyy

Now, if you type ":registers" you can see all of your registers, and thats overwhelming. I tend to just look at the register I'm especially intersted in ":echo @f" or ":registers f" does the trick nicely. Also, since I know I use the 'fvc' registers for yanking - I tend to just look in those registers if I forget what I've done with ":reg fvc".

Did you know that you can append to the named registers? I had NO IDEA. By using the capital letter 'F' instead of the lowercase letter 'f', for instance, you append rather than replace. Take the example above. Say that I was going along editing and I saw another line I might want to use later:

...
...
blown up beneath my glass.
Colors dazzle insect wings.
...
...

I could, if I wanted, just add it to register 'f' with the other line by doing: "Fyy

Type ":reg f" and you see:

--- Registers ---
"f Every good boy^JColors dazzle insect wings.^J


Not too pretty, but thats where this comes in handy: ":echo @f"

Every good boy
Colors dazzle insect wings

Press ENTER or type command to continue

There you are - a case for using 'echo' over 'register' and an interesting note about the named registers.

Friday, November 07, 2008

Another one trying to come back to vim. I sense a theme.

Just saw this post today, about another person trying out switching from TextMate back to their first love: VIM. He switched back to TextMate, and I can understand his reasons. Plugins in Vim do tend to feel 'bolted on', and they can be kinda slow. Although you can certainly customize Vim so that it fits your work requirements, many times I think we all have to customize it for our own needs because, out of the box, its not doing what we need.

This makes me think of cream, a distribution of Vim customized for non-Vim users, in the mode of word processing.

We need some other distribution out there that tightly integrates some of the really amazing plugins that are out there. A cream-for-programmers who know the power of TextMate, IntelliJ, etc.

One thing that might make also go into this soup...has anyone out there noticed the 'netbeans' extension to vim? I've seen the switch and read about what it was supposed to do - tightly integrate Vim on top of netbeans...so you can harness the power of netbeans inside Vim. Fancy icons in the gutters, cool refactoring features of netbeans in Vim. I'm only guessing here, but thats what I *think* the netbeans integration was supposed to have provided. There are certain automagical properties of the big battleship IDEs that Vim just isn't meant to provide...but perhaps providing a way to interface with them would bring those 'other IDE' users back into the fold.

Eh...I don't know. Personally, a kickass tightly integrated distribution of vim with existing plugins would make me happy ... the IDE integration really seems like overkill. But...who knows.

Friday, October 24, 2008

file lookups

I've been hearing about this post from a TextMate guy (I'll call him TM1) finally coming home to Vim(hey, if you're gonna link to some other random editor, you GOTTA plug your own!). Good good. In fact, from any command line editor's point of view: amazing. Its funny though because THIS TextMate guy (call him TM2) got me to write up a plugin very similar to TM1s.

Before I go any further - just click on the 'this post' link above and watch the short movie the guy recorded. I'm lazy, I'm not going to make a video. Once you've watched the video you'll understand what we're talking about: an gosh-its-almost-like-its-an-IDE friendly way of finding files in your project.

Its the kind of thing that just has to be there for any programmer under 35 - we're just not going to fall in love with the :find and :grep and :vimgrep commands of the previous generation. Its just not easy enough. For some people the project plugin does the job. I admit, I kinda like it myself...but I always missed the easy file lookups you get in Eclipse, IntelliJ, etc etc etc. Being a Java guy mainly, I really love how easy Intellij makes it.

When TM2 expressed interested in making the final journey from emacs->textmate->vim, you better believe I was there to encourage him. When he complained that there just wasn't any good Command-T equivelant in Vim, you bet I looked around. I found the lookupfile plugin. At the time this was version 1.4 of the plugin - it was slow, it didn't do camel case searches (fuzzy finder doesn't do that either !?), you couldn't search inside files (ala grep).

I extended the plugin with my own version and tried to get TM2 to use it. Unfortunately my skillz aren't quite what they could be in vimscript. However I was able to get the following working (and hey, it still works!):
  • I implemented searching using the standard unixy textutils and findutils packages underneath (find, sed, grep, xargs, etc). The first search is still slow b/c it builds up the search terms, but then they stay cached...so subsequent searches are blindingly fast.
  • Compatibility with &path and &suffixesadd settings (the variables used by :find, :grep, etc). Basically you can setup &path/&suffixesadd and use :find, :grep, and your friendly lookupfile functions all interoperably. I setup mappings where I only searched say...java files; or java, xml and properties files; or ruby and xml and yaml. Complete control over which directories you search under, as well as their extensions.
  • Support for case sensitive or non case sensitivity.
  • Camel Case search. Not only can you do 'c*frog' to find 'CamelFrogMasterFactory.java' (or with case sensitivity 'C*Fro'), but you can do 'CFro' as well (a * is implied after each capital letter).
  • Grep search: same way of searching (nice filtering list) but search the file contents as well. If it worked better, I'd maybe win that $100 bounty :).
But I had a problem - those darned findutils and textutils I built everything on aren't standard on OSX and so my TM2 friend had no end of trouble. It just was buggy. I guess it still is, but...

since somone else has published their version, and I found it hella slow, I figure...hell, here's my effort as well. Maybe someone out there would like to pick up where I left off.

You can find the result of my efforts right over here. Now.To work.

Thursday, July 10, 2008

MacVIM and cd

Just a quick note. For you Mac & Vim users out there - MACVIM. It truly rocks.

Also, a quick "oh my gosh I'm so glad I finally found a command for this," and then a "doh, of course!"

I often launch Vim and then have to work on a set of files in some specific project directly. Its a pain to have to type something like:

:e ~/Documents/ProjectA/subProjectX/file1.py
:sp ~/Documents/ProjectA/subProjectX/file2.py
:sp ~/Documents/ProjectA/subProjectX/file3.py

over and over and over again. I've been looking for the command to set your 'current directory', and I found it: cd. Duh:

:cd ~/Documents/ProjectA/subProjectX
:e file1.py
:sp file2.py
:sp file3.py

So much better.

Monday, July 07, 2008

javascript vi clone

Thats right, check this out. VI implemented in javascript.

Tuesday, July 01, 2008

Review of "100 Vim Commands every programmer should know"

This morning I woke to find that a number of blogs out there there have picked up an article called "100 Vim commands every programmer should know" over at CatsWhoCode.com. Of course, I had to go over there and scan down the list to see if there was something there that I might need to know.

Search

Commands under the 'Search' category are simple enough: straightforward word boundaries and regular expressions mixed in.

Replace

The 'Replace' category is much the same. Ranges are covered extensively. Unfortunately, I think it got a bit too general. The 'global' command was mixed in here, and only glossed over with the following paltry commands:

:g/string/d Delete all lines containing “string”
:v/string/d Delete all lines containing which didn’t contain “string”

Interestingly I'd never heard of the ':v' command, but now I know why...its equivalent to the ':g!' command.

An oldy but goody was included in the 'Replace' category as well, to with XML tags:

:%s#<[^>]\+>##g Delete HTML tags but keeps text

At the bottom of this section I saw "Change text to Rot13." Rot13?? Ah. To be honest I haven't in my paltry 14 years on the internet ever found myself using Rot13 for anything, so I have to say I'm a little surprise this makes it into the "Top 100". Onward.

Case

Amazing! I have a big black hole in my head when it comes to case commands. Oh no, nevermind. Funny how it is that you sometimes actually have to use the command sequences before you realize, oh...yeah, I know that. Duh. To deal with words I prefer the 'inner word' type commands. Rather than doing 'vE' to select a word as is done a couple times here, I would do 'viw'. I think you would find 'viw' more forgiving than 'vE' if you were whipping up a recording, say, as it'll actually select the whole word. 'vE' only selects the current position to the end of the word. Minor detail I suppose. I didn't know about this setting though. Sweet:

:set smartcase Ignore case in searches excepted if an uppercase letter is used

There were also a number of case replacements using sequences like:

:%s/\<./\u&/g Sets first letter of each word to uppercase

I'd tend to categorize this as 'Replace', but thats just me. Also, I don't often use the these generic sub replace commands in favor of the generic sub replace method. I'd do this more generic replace:

:%s/\<./\=toupper(submatch(0))/g

Hmm, yes. I should learn to use those other matches. Mines a bit verbose.

Okay, so I've only reviewed about 1/3'rd of the entire list. On the whole I think its a good list, though the categorization is a bit loose. There were a few good comments on the article (and the author acknowledged them as well; they should get a pat on the back for listening to the readers). Worst offense: leaving out recording macros. Definitely should have been in the top 100.