2

I was recently going through a tutorial on configuring the Vimrc file to make it behave more like traditional text editors. In that tutorial, I came across the following line: command! -nargs=* Wrap set wrap linebreak nolist. This has the effect of allowing one to simply type ":Wrap" and activate a number of behaviors.

The problem I have is two-fold. First: is there an easy way to disable these settings (after they have been enabled) with a simple command or do I need to enter something like ":set nowrap nolinebreak list" ? Second, how does that "command!...." work? The tutorial gave no guidance as to how to interpret the mechanics of that line. What does "command!" do? What about "-nargs=*"? I understand everything else methinks... I am still too new to this to find simply reading the documentation to be particularly elucidating and have yet to find a tutorial on the subject.

  • 3
    Have you read :h :command? The doc on this subject is quite straightforward. – Luc Hermitte Oct 2 '17 at 23:58
  • I have. It specifies that the explanation mark is used to indicate a "-bang attribute." I haven't been able to find a clear description as to what that is on google. Combine that with my ignorance as to the meaning of "-nargs" and it seemed like a wise vim sage might be able to clear things up quicker than I could by myself. – Shadow43375 Oct 3 '17 at 0:05
  • "bang" refers to the exclamation mark. On :command it permits you to source the file where the command is defined multiple times. -bang permits to define banged commands -- and it's up to you to decide what to do with it. -nargs shall be read as Number of ARGS. It could be 0, 1, any (*), or at least one (+). (:h :command-args). Over the years, I've implemented quite a few commands. See some of the simplest ones: github.com/LucHermitte/lh-misc/blob/master/plugin/… github.com/LucHermitte/lh-misc/blob/master/plugin/… – Luc Hermitte Oct 3 '17 at 0:43
  • The real difficulties are the parameters (<args>, <f-args>...), and the support for command-line completion -- I've ended-up with a set of helper functions to help me define command-line completion. – Luc Hermitte Oct 3 '17 at 0:46
  • 1
    If you omitted the exclamantion mark, then if you changed the line and ran it again, Vim would report a "That command already exists" instead of updating it to reflect your changes. – Rich Oct 3 '17 at 15:30
2

You've been looking at the documentation for :command without any arguments. The documentation when you are passing arguments is a couple of paragraphs further down. (:h E174 to jump straight there.) The relevant part reads:

If the command already exists, an error is reported, unless a ! is specified, in which case the command is redefined.

The explanation of -nargs is further down still, at :h :command-nargs. It begins:

By default, a user defined command will take no arguments (and an error is reported if any are supplied). However, it is possible to specify that the command can take arguments, using the -nargs attribute. Valid cases are:

-nargs=0 No arguments are allowed (the default)
-nargs=1 Exactly one argument is required, it includes spaces
-nargs=* Any number of arguments are allowed (0, 1, or many), separated by white space

So the command creates a new command named :Wrap which takes any number of arguments. If the :Wrap command already exists, it overwrites it with the new functionality. The :Wrap command ignores all the arguments and sets nowrap, nolinebreak, and list. Your guess is as good as mine as to why the arguments are allowed if they are simply going to be ignored.

(In case you don't know what an "argument" is: it's just words that you type after a command, that the command uses. e.g. when you save a new file with :w <filename>, the file name you type after the :w command is the command's argument.)

You are correct that :set nowrap nolinebreak list would set/unset the options again. If you wanted to write a command that performed the inverse of the one in your question, then you could try simply changing the options:

command! -nargs=* Unwrap set nowrap nolinebreak list

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.