Vim:使用表达式作为文件名

时间:2023-01-23 00:28:18

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.

我正在做一些调试,其中涉及检查日志文件名称“blah N.log”,其中N是一个整数。定期添加一个新的日志文件,我想跳到下一个日志文件,而不是明确地执行:e blah \ N.log。我不能只做%vim * .log然后:n,b / c并不是所有的日志文件都存在。

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?

但我无法弄清楚如何将该表达式的结果传递给:e命令。有小费吗?

3 个解决方案

#1


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.

我必须添加第二个substitute()命令来转义文件名中的空格。

#2


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()

:执行“编辑”GetNextLogfile()

You might want a mapping or abbreviation.

您可能需要映射或缩写。

#3


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.

另一种方式:: e =调出表达式提示。输入适当的表达式(包括用于表达式历史记录),然后按 键替换原始提示中的值。

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.

这不仅仅是: 来重做我的:exec命令,但仍然是一个有用的技巧。

#1


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.

我必须添加第二个substitute()命令来转义文件名中的空格。

#2


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()

:执行“编辑”GetNextLogfile()

You might want a mapping or abbreviation.

您可能需要映射或缩写。

#3


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.

另一种方式:: e =调出表达式提示。输入适当的表达式(包括用于表达式历史记录),然后按 键替换原始提示中的值。

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.

这不仅仅是: 来重做我的:exec命令,但仍然是一个有用的技巧。