4

How can I edit the text of a Vim command I've started typing out in the Vim command line?

Example:

If I type out a long search and replace command and realized I made a mistake. Now I want to move back to the beginning of the line and fix it so I want to be able to exit Insert mode go to Normal then type 0 and go back to the beginning of the (:) command.

e.g.

:%s/add asserts for things you expectequal to the expected value thathappend/Trevor add asserts for expected values/g
7
  • Hit Ctrl-F and you'll enter a mode with full Vim edit capabilities of current and previous commands. :h c_CTRL-F ... I think there's already an answer for this somewhere...
    – B Layer
    Jun 3, 2021 at 13:51
  • You can use Ctrl-F from a command-line or search to open the "command window", where you can use Normal and Insert modes to edit your command. See :help c_CTRL-F. I believe there's a recent question that got the same answer recently, so I'm looking for that to mark this one as a duplicate... If I don't find it, I might post it as an answer here.
    – filbranden
    Jun 3, 2021 at 13:53
  • This is the one: vi.stackexchange.com/q/31387/18609 (see the second answer), but not sure if it's close enough of a duplicate...
    – filbranden
    Jun 3, 2021 at 13:55
  • 1
    The actual name is "command-line window" and it has its own tag...gotta be a suitable dupe somewhere around here I would think. Looking myself.
    – B Layer
    Jun 3, 2021 at 14:02
  • Thank you I am going to use it for a bit and see but it seems like it exactly or even better than what I was looking for. Thanks a lot :)
    – I J
    Jun 3, 2021 at 14:07

1 Answer 1

7

Note: F is a key on your keyboard not the input "F" which would be Shift+F

There's no inline, rich command-line editing within Vim in the way many might expect (i.e. like Bash command-line editing with "vi-mode") but you can edit commands with a feature that is arguably even better than that; a feature that even lets you yank/put parts of earlier commands into your in-progress command.

I'm talking about the command-line window.

To open this while in the middle of entering a command (after you hit :) type Ctrl+F

You will see your full Ex command history including your in-progress command. This window is like a regular Vim buffer; you'll start out in Normal mode and can edit your current command or any previous commands the same way you'd edit any other text.

When you're ready to submit a command make sure the cursor is on its line, and hit Enter. (Doesn't matter what mode you're in.)

If you want to bail out without submitting any command you can just use :q to quit the command-line window. (You don't need !, even if you made changes).

Or, if you want to continue editing a command back on the regular command-line hit Ctrl+C with the cursor over the correct command.

BTW, you can also open this window from any buffer by entering q: while in Normal mode.

Finally, you have this same capability for editing search patterns: q/ or q?.

See :h cmdwin for complete details.

0

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.