views:

108

answers:

3

I'm doing some debugging, which involves checking logfiles name "blah N.log" where N is an integer. Periodically, a new log file gets added, and I'd like to just skip to the next log file, rather than explicitly do :e blah\ N.log. I can't just do % vim *.log and then :n, b/c not all the log files exist yet.

I've got an expression that yields the next logfile name:

:echo substitute(expand('%'), ' \zs\d\+', \=submatch(0) + 1', '')

But I can't figure out how to pass the result of that expression to an :e command. Any tips?

+2  A: 

Here's a way to do it - though I hope there's a more elegant way.

:execute ':e ' . substitute(substitute(expand('%'), ' \zs\d\+', \=submatch(0) + 1', ''), ' ', '\\ ', '')

I have to add a second substitute() command to escape the space in the filename.

rampion
+2  A: 

Or you can wrap it in a function. I would do the following:

function! GetNextLogfile()
    "" store expression in a
    "" let a=...
    return a
endfunction

then

:execute "edit " GetNextLogfile()

You might want a mapping or abbreviation.

alvatar
+1 : I was hopeing for a one-liner, but this is a good method to note
rampion
If you plan to repeat this many times, maybe is worth to include some functionality in the vimrc... :)
alvatar
+1  A: 

Yet another way: :e <CTRL-R>= brings up the expression prompt. Enter the appropriate expression (including using up for expression history), and then hit <Enter> to substitute the value in the original prompt.

This makes the keystrokes:

:e <CTRL-R>=<Up><Enter><Enter>

Which is not as few as just :<Up><Enter> to redo my :exec command, but a useful trick, nonetheless.

rampion