0

Suppose I wrote this command:

command! -nargs=1 E echo ' -> <args>'

and when user feeds an argument,

:E abc

I intend to see the following effect as they hit Enter:

:E abc -> abc

Of course that's not happening, since vim flushes :E abc when printing -> abc, leaving the command-line showing:

 -> abc

Is there an option to retain the command typed by user in the command-line, before finishing execution? Or are there alternative ways to achieve the same effect?

3
  • 1
    See :help c_CTRL-\_e.
    – romainl
    Commented Dec 26, 2022 at 7:51
  • 1
    Since you're defining the command yourself, you can simply prepend "E <args>" to " -> <args>", can't you?
    – 3N4N
    Commented Dec 26, 2022 at 10:36
  • @3N4N could solve this problem to some extent, but what I really want is to retain the command as exactly typed by the user, where basically every white space matters.
    – Futarimiti
    Commented Dec 26, 2022 at 16:26

1 Answer 1

0

Came up with a solution with functionality of command E being moved entirely to another function F to be called inside command-line expression prompt, thanks to romainl's hint:

command! -nargs=1 E

cnoremap <CR> <C-\>eF()<CR><CR>

" call me only in c_<C-\>e mode
function! F()
    let cmd = ':' .. getcmdline()
    let parse = v:lua.vim.api.nvim_parse_cmd(cmd, {})
    let cmdname = parse.cmd
    if cmdname ==# 'E' && len(parse.args) > 0
        let arg = parse.args[0]
        return 'echo ''' .. cmd .. ' -> ' .. arg .. ''''
    else 
        return cmd
    endif
endfunction

While this solution parses command string utilising nvim API, vim users may need to come up with some other ways to parse command; I cannot think of any builtin functions with the same effect.

Your Answer

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

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