有一个vim相当于bash的'!$'?

时间:2013-08-28 17:49:22

标签: bash vim sh

我经常发现自己在开发时会做这样的事情:

:e <file>
:vsplit <the_same_file>  "reopen/re-edit with a vertical split

正如一些响应者指出的那样,有一种更好的方法可以做到这一点::vs<CR>。假设暂时没有。

如果我在shell中打开文件,我会使用!$历史记录扩展来捕获上一个命令的最后一个参数,如下所示:

$ echo "Hello" "stackoverflow"
Hello stackoverflow
$ echo "I'm posting on !$"
I'm posting on stackoverflow

这个扩展,many others like it,彻底重新定义了我使用命令行的方式。 vim中有等价物吗?我知道%别名当前文件。那些过去的命令参数呢?

3 个答案:

答案 0 :(得分:3)

据我所知,Vim没有历史扩张。

然后自然要做的就是使用Vim在命令行上提供的工具。我们有选择!

  • <Up>箭头过滤
  • 命令行编辑命令
    • CTRL-W向后删除字词
    • <Left><Right>四处移动,CTRL-BCTRL-E移动到行/开头
  • 使用命令行窗口q: (或已在命令行上显示CTRL-F以获得最终的历史记录编辑能力
  • 针对手头的具体问题找到Vim精神中的惯用替代解决方案
    • 自动展开的代币,例如%#就是一个很好的例子

但是,如果你真的想要Bash风格的历史扩展,你可以很容易地与命令行<expr>缩写一起破解(至少对于更简单的情况)。

我经常使用的历史扩展项目(坦率地说不常见):

  • !!,最近的命令行
  • !-2,第二个最近的命令行
  • !*,所有参数,但上一个命令行的第一个
  • !$,上一个命令行的最后一个参数

以下是如何将它们实现为表达式缩写:

cnoreabbr <expr> !!  getcmdtype() == ':' ? @: : '!*'
cnoreabbr <expr> !-2 getcmdtype() == ':' ? histget(':', -2) : '!-2'
cnoreabbr <expr> !*  getcmdtype() == ':' ? join(split(@:,'\s\+')[1:], ' ') : '!*'
cnoreabbr <expr> !$  getcmdtype() == ':' ? split(@:,'\s\+')[-1] : '!$'

以下是你的例子在Vim中的用法:

:echo "Hello Stackoverflow"
Hello Stackoverflow
:echo "I'm posting on !$<Enter>
I'm posting on Stackoverflow

当然,如果你真的决定沿着这条路走下去,可以使用函数来表达更复杂的表达式。

答案 1 :(得分:1)

使用不带参数的:vsp会拆分当前窗口。

不完全是你要求的,但我经常使用%这是当前的文件名:

:e some_file
:vsp %

请参阅:help cmdline-special

答案 2 :(得分:0)

杰出的best of VIM tips指出了历史重复有效的一个特定领域,即替换:

" Summary of editing repeats [N]
.      last edit (magic dot)
:&     last substitute
:%&    last substitute every line
:%&gic last substitute every line confirm
g%     normal mode repeat last substitute
g&     last substitute on all lines
@@     last recording
@:     last command-mode command
:!!    last :! command
:~     last substitute
:help repeating