Wednesday, May 23, 2007

Convenient tab commands

I was conversing with a friend about how to turn a split into a new tab, and vice versa this morning. You can call IMing 'conversing', right? Yes. At this day and age, I'd hope so. Anyway. We came up with a couple convenient commands/abbreviations. Take your pick:

" Close a split and then open it in a new tab:
:ca tb let a=bufnr("%") q tabnew exe "b ". a
:command! Tb :let a=bufnr("%") | q | tabnew | exe "b ". a

" Close the current tab and open as a split in another existing tab:
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q | tabprev
:ca ts :let a=bufnr("%") tabprev sp exe "b ". a tabnext q tabprev

Note these obviously aren't that smart. If you have no splits, its not gonna act quite right. I've made the order open X, then close Y so that vim still has *something* open. Hopefully that means your Vim session won't disappear out from underneath you.

Yep, I just tested it out. Vim doesn't close, and the 'tb' command works fine regardless of the # of windows open. The 'ts' command acts a little funny when there is more than one split open in a tab. It removes one split, sticks it in another tab but doesn't completely remove the other tab b/c there's still a window in it. Not so bad. Just a little wierd.

Monday, May 21, 2007

Recursive Diffs?

It appears that, yes, someone has already gone out and written a plugin for recursive diffing two folders. According to one person, it works relatively well. What I'm wondering is, could this plugin be used to diff two revisions in say, SVN?

Well, I had a chance to try the thing out and I have to say that I am happier doing:

:r ! diff -r --brief

and then making my own quick macro to diff any files mentioned in the diff results. I just like the ability to filter out any lines I already know aren't a factor in my diffing. The DirDiff plugin doesn't allow you to change the contents of the 'diff results'. Well, I was at first. Until I figured out all I had to do was type:

:set modifiable

Then I could delete the 'results' screen willy nilly. Much better. It still complained if I tried to insert text or what its kind of a toss up. If I needed to quickly copy the contents back and forth then this DirDiff tool would be just the thing.

The trouble is most of the time I'm actually looking for a needle in the haystack. Some little change in a text file is causing a world of havoc for me. I don't want to filter out 'types' of files, and copy whole swathes of differences between the trees. In this case, I think manually reading in the diff and recording your own little quick diff macro is the way to go. DirDiff is slightly too constrained in what it wants to let you do.

In summary: a good plugin if you need to copy back and forth changes, or you don't want to spend the extra 3 minutes it takes to record your own diffing macro. I've rated this script 'Helpful'.

Tuesday, May 15, 2007

Quickly delete several random lines

I just couldn't think of a quick way to take a subset of a file, where some # of lines had been randomly deleted. Since VIM doesn't have its own random() command you have to turn to some external method of determining which lines to delete. My solution was ruby:

:ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")

To repeat the command several times I couldn't think of anything better than a macro recording:

:let @l=':ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")^M'

Where '^M' is really Control-J cariage return. Then to do it...uh, 30,000 times I just typed 30000@l