1

It's possible to configure a mapping such that command line mode is active after the key press.

For example, with a mapping :map H :help , pressing H results in command line mode being entered with :help as the current text.

The documentation for :normal says:

A ":" command must be completed as well. And you can't use "Q" or "gQ" to start Ex mode.

Therefore, it's not possible to execute :normal : and end up in command line mode.

Are there commands such that command line mode is active upon completion?

Can a Vimscript function be used where command line mode is active with some specified text, after running the function directly (i.e., via a :call entered in isolation, as opposed to being wrapped in a mapping that can result in command line mode)?

1
  • 2
    Certainly you can create a mapping (e.g. nnoremap <leader>x :) that leaves you on command line. Don't know if that helps you... (oops...Luc's answer hadn't shown up for me before I hit send..oh well, I'll leave it since it's related)
    – B Layer
    Jan 11 at 21:29
1

You can use the feedkeys() function for this purpose.

For example:

function! Edit()
    call feedkeys(':edit ', 'n')
endfunction

Then :call Edit() will enter the Ex command-line, enter :edit into it and leave the cursor there for the user to complete and eventually execute the Ex command.

As :help feedkeys() explains:

By default the string is added to the end of the typeahead buffer, [...] The function does not wait for processing of keys contained in {string}.

I also recommend that you take a close look at :help feedkeys() for the possible values of {mode}, I used 'n' for no-remap in my example, but there are more mode flags that you might find interesting for your specific situation.

3

The easiest solution: you'll need a mapping and have the mapping returns the value of a function that returns a sequence that will have us end in command mode.

2
  • 2
    Thanks for posting this workaround. However, my interest is in running the function directly, without a mapping. I've updated the question to make this more clear.
    – dannyadam
    Jan 11 at 21:58
  • 2
    I've learned to distrust some solutions like feedkeys() -- there are too many side effects for my tastes. In the end, if I want to execute a function that may make me end up in command mode, this means that what calls the functions (typically a mapping somewhere) will receive the order to go into command mode, from the various function(s) called. There is a limitation: it won't work as well with commands to end up in command mode from commands. Jan 11 at 22:59
0

I'm agree with Luc. feedkeys() should be avoided whenever possible.

Carefully using :map-<expr> (or even :h abbreviations, as you seem to "misuse" a command to compose another command, see :h :cnoreabbrev) works in 90+ percent of cases. But if you really want the user to input a line or two, you can always fake it with :h input() and :h execute().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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