Search code examples
vimvineovim

How to create a command that uses vim-function?


Say that we have a function:

function StripTrailingWhitespace()
  if !&binary && &filetype != 'diff'
    normal mz
    normal Hmy
    %s/\s\+$//e
    normal 'yz<CR>
    normal `z
  endif
endfunction

How to map this function to StripTrailingWhiteSpace command so that we can run :StripTrailingWhiteSpace instead of :call StripTrailingWhiteSpace() ?

How to do the same for functions with arguments ?

The reason for doing that is: we get commands as completion window of fzf/vim-clap/coc plugins. And it's more align with vscode's cmd+shift+p functionality.


Solution

  • The :command command is what you are looking for.

    Please read :h command for details. E.g. range handling, arguments handling etc.

    In your case:

    command! StripTrailingWhiteSpace call StripTrailingWhiteSpace()