10

Is there a way to get Vim in command mode to complete words from the current file? Say I have this file:

one
two
three
once

and I enter command line mode

:myruncommand o|

where | denotes the cursor position. How do I make Vim complete one or once when pressing Tab?

2

It's not possible natively, but it took about one minute to find this script and this one on vim.org.

4

jamesc's answer would often be the most convenient, but for when you know ahead of time that you'll want to use autocomplete for a command or search string, or other functions only available in a buffer, here's another way to get to the appropriate buffers:

  • Type q : to open the command buffer
  • Type a to start entering the new command: myruncommand o
  • Use Ctrl+N/Ctrl+P for keyword completion, just as in jamesc's answer

Basically, q : does the same thing as : Ctrl+F: let you enter commands as if you're entering text in a buffer. Similarly, q / and / Ctrl+F both let you type search strings in a buffer.

  • Great answer. Command buffer is something that I'd definitely like to be better at using. – jamesc Dec 22 '15 at 9:17
  • @jamesc: I'm glad it was helpful for you. I didn't know about Ctrl+F, so your answer helped me, too. – Michael Scheper Dec 22 '15 at 19:36
1

Completion is available if you enter history of command mode. Very handy to alter or repeat previously used commands.

Press q: in normal mode to enter command history pane.

See http://vim.wikia.com/wiki/Using_command-line_history for more details.

  • In command line mode, if you type Ctrl-f the command line history opens, and if you want to close that window you can type Ctrl-w q – SergioAraujo Sep 10 '17 at 18:09
1

Although true that this can't be completed natively with 'tab', there is the possibility of:

  • Entering the command you want in command line mode. For the example above this was:

    :myruncommand o
    
  • Now press Ctrl-F to enter the command window.

  • Use A move to the end of the line and enter Insert mode.
  • Use Ctrl-N / Ctrl-P to open keyword completion and select the option you want - in this example both one and once should be available since they are in an open buffer.

I've added this answer because my search for "vim complete word in command mode" brought me here and it seems to be a workable solution, even though it doesn't use the 'tab' key.

This works for me in Vim 7.4

  • I should've guessed there was an easy way to go to the command/search buffer while already entering a command or search string! Thanks! I've added an answer based on yours, for the way I already knew. But doubtlessly, Ctrl+F is more convenient in many cases. superuser.com/a/1016058/219388 – Michael Scheper Dec 21 '15 at 0:09

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.