3

I'm somewhat new to vim (read: really new). I'm used to vi-keys for moving and I understand the concept of operators vs motions vs commands and whatnot, but I'm still learning the commands.

When I tried to open a file, my first instinct was :open/:o. This works, but doesn't support tab autocomplete. :edit/:e works with tab autocomplete though. Furthermore, I've found :visual, which appears to also have tab autocomplete.

What's the differences between these 3 commands? When do I use each of them?

I've also found this, but that doesn't really explain the differences between :open and the others.

The documentation for the three says:

:vi[sual][!] [++opt] [+cmd] [file]
                When used in Ex mode: Leave |Ex-mode|, go back to
                Normal mode.  Otherwise same as |:edit|.

And:

:e[dit] [++opt] [+cmd] {file}
                Edit {file}. 

And lastly:

:[range]o[pen]          Works like |:visual|: end Ex mode.
                        {Vi: start editing in open mode}

... so they all just open a file? The other difference I can see is the 'end ex-mode', which I don't really get.

4
  • 1
    :tabe works as well with auto-complete (but it opens a tab).
    – user41805
    Mar 9, 2017 at 18:37
  • @KritixiLithos hm, okay. Not entirely sure what tabs are yet but they can't be too hard. (like browser tabs?)
    – user11389
    Mar 9, 2017 at 18:38
  • Sorta. To navigate you have to use :tabn and :tabp
    – user41805
    Mar 9, 2017 at 18:40
  • Tabs in vim are more like browser windows than browser tabs; the closest equivalent to browser tabs in vim are buffers.
    – evilsoup
    Mar 11, 2017 at 13:47

1 Answer 1

5

So first things first, what is this "ex-mode"? Ex is an ancient text editor written by Bill Joy (the original author of vi). It is very primitive. Ex is a lot like vim, but without normal or insert modes. The only thing you can do is commands that start with a colon (known as ex-commands).

When vi was written, a lot of people were used to ex and didn't want to learn this fancy-shmancy new editor, so vi was designed to be backwards compatible with ex. Because of this, vi has an ex-mode, where you can only use ex-commands. (Vim keeps it around for similar stupid reasons)

To enter ex-mode, type Q in normal mode. To get out of this annoying mode, you can type the command visual (or vi as a shortcut). This will always put your cursor back where you started. open is very similar to visual, except that it takes a number as an argument, and puts your cursor on that line. This makes sense since ex was line-based and could only show a line at a time. So the idea behind the command was "open line 'n' for me to look at/read".

Now, what is the difference between the three when you ignore ex-mode? Not really very much. As Josh explains in this wonderful answer, :visual and :edit are implemented in the exact same place, so they're essentially the same (when you ignore visual being used to leave ex-mode). This goes well with the help docs, where it states:

                            *:vi* *:visual*
:vi[sual][!] [++opt] [+cmd] [file]
            When used in Ex mode: Leave |Ex-mode|, go back to
            Normal mode.  Otherwise same as |:edit|.

Otherwise same as :edit! However, :open is slightly different because 1) the ex-command is different (accepting a range) and 2) the normal command is different (accepting an optional pattern). So :open has it's own separate implementation.

Now there's only one question remaining. Why does :open not tab-complete? I'm not Bram Moolenaar, so I could be wrong, but it's probably because the open command takes a pattern as an argument. It makes sense for it to behave more like / and :s/ when the user could be typing a regex or a file.

To summarize:

  • If you want to edit a file, use :e. It's standard, well-supported, and convenient (because of tab-complete). You could use :visual if you really want too, but it's an unusual practice and confusing.

  • Generally avoid ex-mode. If you want to leave ex-mode, either visual or open are fine ways to exit it (depending on which behavior you want)

  • I have no idea why Bram chose to make it so that using the right command in the wrong context (e.g. visual or open from normal mode) defaults to pretending to be :edit.

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.