Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vim autoreply

A modified version of Romain's gist.

See prior revisions for functionality closer to the original. This is now a simplification that aims only to provide prompts that would typically follow basic commands.

function! ExpandCommand(pattern) abort
  let aliases =
    \ { 'cn' : 'cnext'
    \ , 'cp' : 'cprevious'
    \ , 'g'  : 'global'
    \ , 's'  : 'substitute'
    \ , 'v'  : 'vglobal'
    \ }

  let completion = getcompletion(a:pattern, 'command')
  if &ignorecase
    call filter(completion, "v:val !~# a:pattern")
  endif
  let completion = len(completion) == 1 ? join(completion) : ''

  return get(aliases, a:pattern, completion)
endfunction

augroup AutoReply
  autocmd!
  autocmd CmdlineLeave :
    \ execute 'silent doautocmd AutoReply User'
    \          ExpandCommand(getcmdline())

  autocmd User marks     call feedkeys(':normal!`', 'n')
  autocmd User oldfiles  call feedkeys(':edit#<',   'n')

  autocmd User buffers   call feedkeys(':buffer'      . "\<Space>",  'n')
  autocmd User files     call feedkeys(':buffer'      . "\<Space>",  'n')
  autocmd User ls        call feedkeys(':buffer'      . "\<Space>",  'n')

  autocmd User clist     call feedkeys(':silent cc'   . "\<Space>",  'n')
  autocmd User llist     call feedkeys(':silent ll'   . "\<Space>",  'n')
  autocmd User registers call feedkeys(':silent put'  . "\<Space>",  'n')
  autocmd User tags      call feedkeys(':tjump'       . "\<Space>",  'n')
  autocmd User undolist  call feedkeys(':silent undo' . "\<Space>",  'n')

  autocmd User changes   call feedkeys(':normal! g;'  . "\<S-Left>", 'n')
  autocmd User jumps     call feedkeys(':normal! �'  . "\<S-Left>", 'n')

  autocmd User chistory
    \ if getqflist({'nr' : '$'}).nr > 1
    \ |   call feedkeys(':silent chistory | copen',  'n')
    \ |   call feedkeys("\<Home>\<S-Right>\<Right>", 'n')
    \ | endif
augroup END
@josefson
Copy link

josefson commented May 25, 2020

Tried using it as opposite to romain's CCR because it allowed me to nnoremap ,c :changes<CR>. Whereas with romain's i didn't know how to do that.
However, it collided with other stuff that used CmdLineLeave such as the builtin method motions for python filetypes for instance.
So if in visual mode i try to any method motion [m [M ]m ]M, it will jump to that motion but also it will stop visual selection in its tracks by ':' in command-line-mode.

@george-b
Copy link
Author

george-b commented May 25, 2020

This should already be fixed in the latest revision.

I suspect you have an older copy where get() was still returning its default of 0 in the failed case, rather than the empty string as it does now. The former results in the the first comparison against previous_cmd evaluating to true. This is because in Vimscript when comparing a number to a string the string is converted to the value 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment