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

I often find myself doing something like this during development:

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

As some respondents noted, there is a much better way to do this :vs<CR> . Suppose there was no moment.

If I opened files in my shell, I would use the !$ History extension to capture the last argument of the previous command, for example:

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

This extension, and many others like it, completely changed the way you use the command line. Are there equivalents in vim? I know the % aliases of the current file. What about the arguments of the past-team?

+7
bash vim sh
source share
3 answers

As far as I know, Vim has no history extension.

Naturally, we need to use the tools that Vim gives us on the command line. And we have options!

  • <Up> arrow filtering
  • Command Line Editing Commands
    • CTRL-W to delete the word back
    • <Left> and <Right> to move, CTRL-B and CTRL-E to go to the beginning / end of the line
  • Use the q: command line window (or CTRL-F when already on the command line) for maximum editing power
  • Find idiomatic alternative Vim solutions for a specific problem
    • Automatically expanded tokens, such as % and # , are a good example.

However , if you really want a Bash-style history extension, you can pretty easily hack something along with the <expr> command line abbreviations (at least for simpler cases).

Elements of the story extension that 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
  • !$ , last argument of the previous command line

Here you can implement them as paragraph 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 this is how your example will 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 this route.

+3
source share

Usage :vsp with no arguments splits the current window.

Not quite what you ask for, but I often use % , which is the current file name:

 :e some_file :vsp % 

See :help cmdline-special

+1
source share

The outstanding best of VIM tips pointed to one specific area where history repeats, namely in permutations:

 " 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 
0
source share

All Articles