5

I read a document about vim and the author suggest a line of code that should be put in .vimrc but I totally have no idea what it is.

command! -bang -nargs=* Find call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>), 1, <bang>0)

So what is command!, -bang, -nargs,etc?

11

As always, the help is your best friend:

:h :command:

:com[mand][!] [{attr}...] {cmd} {rep}

Define a user command. The name of the command is {cmd} and its replacement text is {rep}. The command's attributes (see below) are {attr}. If the command already exists, an error is reported, unless a ! is specified, in which case the command is redefined.

Thus :command! is used to create a new command that you can use from the command line. Here the new command is :Find and it is used to execute fzf#vim#grep() which is a call to the plugin fzf which will allow to fuzzy find some files.

The other arguments are described in the documentation:

  • :h :command-nargs -nargs is used to make the new command able to take an argument :Find arg1
  • :h :command-bang -bang is used to make the new command able to interpret a ! like in :Find!

The ! right after command is used to tell vim to redefine the command if it already exists, this way when you source your .vimrc you don't have an error message.

As for the arguments passed to the fzf function, I'll let you read the doc of the plugin to see what they do :)

  • You answer's more precise than doc. So if I don't have fzf installed, that said I'd like to define a simpler command, this command command! -bang -nargs=* Find 'rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always"' will work? – Tuyen Pham Oct 18 '17 at 16:35
  • 1
    @TuyenPham probably not: the code in your comment creates a command Find which will be replaced by rg --column etc... but rg is not a vim command it is an argument to the fzf function so Vim will echo an error. If you want an example of a simple command see this one: command! DT windo diffthis. It creates the command :DT which doesn't take an argument or anything and which starts the diffmode in vim. The {rep} part of the command must be an expression that vim understands. – statox Oct 19 '17 at 6:45

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.