0

Why does VIM allow auto-completion of items that are not a valid command? For example, if I type :s, and press the tab key, the following entries are displayed:

enter image description here

However, none of these commands seem to work. It seems to me that auto-completion should only provide suggestions that actually work. Why doesn't it?

6
  • 4
    They are valid commands, although not all of them may make sense to run. In all places I can think of, commands suggested by autocompletion aren't filtered based on whether the effect of the command makes sense or not, but only if it's usable from/in the current context.
    – frippe
    Commented Aug 3, 2022 at 9:01
  • I was hoping this worked more like what you'd see in the console of a web browser. There, it only auto-completes stuff that you can actually do. So, each of those entries listed must be preceded or succeeded by something else before they'll work? @frippe Commented Aug 3, 2022 at 9:10
  • 4
    No, the suggestions are valid commands that can be inserted at that point (i.e., at the beginning of a command). A command may or may not require additional arguments, but it still makes sense to include them as suggestions if they do. As for commands that don't make sense, take :sNext for instance. It's pretty useless if there isn't anything in the argument list, but it's still a valid command and won't crash if there isn't; it just won't do anything. It seems like you've misunderstood something here, because it works like you explain/expect.
    – frippe
    Commented Aug 3, 2022 at 9:29
  • @frippe : Thanks for explaining that. It makes more sense to me now. It is indeed more like I'd expect than I thought. Commented Aug 3, 2022 at 9:40
  • 1
    @frippe Your comments look like an answer to me! You should post them as one so I can upvote it.
    – Rich
    Commented Aug 3, 2022 at 10:48

1 Answer 1

3

Those are valid commands that can be inserted at that position (in this case, at the very beginning of the command line), although not all of them may make much sense to run. By make sense, I mean that a command may have certain dependencies/prerequisites to have a meaningful effect. Some commands are also designed to modify other commands (e.g., :silent) or take arguments (e.g., echo), although they are perfectly valid (albeit pretty useless) by themselves.

Take :sNext for example. We have :h :sNext:

:[N]sN[ext][!] [++opt] [+cmd] [N]           *:sN* *:sNext*
        Short for ":split | [N]Next": split window and go to Nth
        previous argument.  But when there is no previous file, the
        window is not split.  Also see |++opt| and |+cmd|.

That is, if there is no previous argument/file, it won't have any (observable) effect. It won't crash, though. It'll give you a pretty harmless error msg, but some commands won't even do that, just silently do nothing. For completeness, one could also mention that it's quite common that commands assume one or more arguments if none was supplied. For instance, :e {file} would edit {file}, while :e without arguments would edit the current file.

As an example, enter :echo sNex and press tab and you'll notice that vim won't suggest sNext. Now, do the same but for :echo expa. Notice how the suggestions are context dependent and you'll get a couple of suggestions this time.

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.