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 filteringCTRL-W
to delete a word backwards<Left>
and <Right>
to move around, CTRL-B
and CTRL-E
to move to start/end of lineq:
(or CTRL-F
when already on the command line) for ultimate history editing power
%
and #
are a good exampleHowever, 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 lineHere'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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With