3

I'm looking for a fast and efficient way to re-use the parameters of the last command in a new command. For example, in a Bash shell you can use the $1 placeholder to reuse the 1st argument of the last command, $! to reuse the last argument, etc. In a Fish shell you can use Alt-Up and Alt-Down keys to bring up the parameters of the complete history.

A regular use case is that I want to re-use parts of a file name I used in a previous command. If the file is still open, I could use Ctrl-R, % in the command line, but if I switched files, I have to open the command history, look for the command and copy the file name.

Ideally, I'd like to have an autocomplete-like key binding in the command line that allows me to skip though parts of the history just like I would skip through file names when hitting Tab.

4
  • 5
    At that point, I would probably q:k and start editing the previous command
    – D. Ben Knoble
    Oct 28, 2022 at 15:06
  • 1
    @D.BenKnoble I just learned about q: and it's a bit of a game-changer.
    – MDeBusk
    Oct 28, 2022 at 15:15
  • q: and h k j l are hard to beat. Don't be afraid to :q to escape back to Normal Mode after q:. I feel that's what has people shying away from q commands in general (fear of quitting out of Vim entirely). @D.BenKnoble Nov 2, 2022 at 22:58
  • q: has already been mentioned. Besides that, there is also :<C-N>, :<C-P>, and :<C-F>.
    – husB
    Nov 4, 2022 at 14:51

2 Answers 2

2

A regular use case is that I want to re-use parts of a file name I used in a previous command. If the file is still open, I could use <Ctrl-R %> in the command line, but if I switched files, I have to open the command history, look for the command and copy the file name.

# is the name of the "alternate file". It can be inserted with <C-r># or used directly, just like %. See :help cmdline-special.

There is no universal "last command argument" or "last filename used". The last command can be retrieved with :help histget() but, even then, what you get is only a string because that's what is stored… so you will have to split, quote, etc… Nothing as easy as $_ and friends but that sounds like an interesting week-end project.

0

function! CompleteCommand(arg)
    call fzf#run({'source': GetCommands(),'sink': function('HandleCommand'),'options': '-m --query "'.a:arg.'"'} ) 
endfunction 

function! GetCommands()
    let lines=[]
    let nu=histnr("cmd")
    for i in range(1,nu)
        let lines+=[histget("cmd",i)]
    endfor
    return lines
endfunction

:cmap <expr> <c-a> &cedit.'^"xy$'."<esc><esc>:call CompleteCommand(@x)<CR>"

You start to type a command in the command line (:), and using FZF plugin, it allows you to fuzzy search commands that has part of it.

(That is done by pressing <c-a>)

requires

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
``

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.