Vim command mode complete word

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?


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


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.


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