Is there a vim equivalent to bash's '!$'?

I often find myself doing something like this while developing:

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

As a few responders have pointed out, there's a much better way to do this: :vs<CR> . Suppose for a moment there weren't.

If I were opening files in my shell, I'd use the !$ history expansion to capture the last argument of the previous command, like this:

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

This expansion, and the many others like it, utterly redefined the way I used the command line. Are there equivalents in vim? I know % aliases the current file. What about past-command arguments?


As far as I know Vim does not have history expansion.

The natural thing to do then is to use the tools Vim gives us on the command line. And we have options!

  • <Up> arrow filtering
  • Command-line editing commands
  • CTRL-W to delete a word backwards
  • <Left> and <Right> to move around, CTRL-B and CTRL-E to move to start/end of line
  • Use the command-line window q: (or CTRL-F when already on the command line) for ultimate history editing power
  • Find idiomatic alternative solutions in the Vim spirit for the specific problem at hand
  • Automatically expanded tokens like % and # are a good example

  • However, if you really want Bash-style history expansion you can hack something together with command-line <expr> abbreviations fairly easily (at least for the simpler cases).

    The history expansion items I use most often (frankly not very often):

  • !! , the most recent command line
  • !-2 , the second most recent command line
  • !* , all arguments but the first of the previous command line
  • !$ , the last argument of the previous command line
  • Here's how you can implement them as expression abbreviations:

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

    And here's how your example would work in Vim:

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

    Of course, use functions for more complex expressions if you really decide to go down this route.


    Using :vsp with no arguments splits the current window.

    Not exactly what you're asking for, but I often use % which is the current filename:

    :e some_file
    :vsp %
    

    See :help cmdline-special


    VIM技巧中最杰出的指出了历史重复的一个特定领域,即替代:

    " 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
    
    链接地址: http://www.djcxy.com/p/73978.html

    上一篇: 元素大小问题

    下一篇: 有没有相当于bash的'!'的vim?