2

I am trying to configure prettier-eslint and prettier-standard as the js formatter with neoformat:

" neoformat should try formatprg where available
let g:neoformat_try_formatprg = 1
augroup vimrc
  " if eslint is found
  if HasEslint()
    "get path of prettier-eslint
    let g:prettier_eslint_path =  system('PATH=$(npm bin):$PATH && which prettier-eslint')
    echom g:prettier_eslint_path

    if s:has_eslintrc_path
      "get full path to eslintrc if eslintrc file exists
      let g:eslintrc_full_path = getcwd() + s:eslintrc_path
      echom g:eslintrc_full_path

      autocmd FileType javascript set formatprg = g:prettier_eslint_path --filePath g:eslintrc_full_path
    elseif s:has_eslintConfig
      let g:eslintConfig = system("jq < package.json '.eslintConfig'")
      echom g:eslintConfig
      autocmd FileType javascript set formatprg = g:prettier_eslint_path --eslintConfig g:eslintConfig
    endif
  else
    " just use prettier-standard
    autocmd FileType javascript set formatprg=prettier-standard
  endif
  autocmd BufWritePre *.js Neoformat
  " autocmd BufWritePre,TextChanged,InsertLeave *.js Neoformat
augroup end

When opening a js file, it gives me an error saying that it could not find g:prettier_eslint_path.

How can I configure vim to use the local version of prettier-eslint and pass the appropriate options to it. It seems to use the default settings if no options are provided.

1 Answer 1

4

Would be nice to see the full error, on which line it complains. Nevertheless you have error in the script, the set command requires no spaces around =, or spaces at all after = sign. You should also use setlocal, since set works globally (it will set for all buffers). And whole auto-command expression should be enclosed in execute with concatenation with variables.

So, the autocmds should look like this:

autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path."\\ --filePath\\ ".g:eslintrc_full_path

autocmd FileType javascript execute "setlocal formatprg=".g:prettier_eslint_path."\\ --eslintConfig\\ ".g:eslintConfig

Also, because you're enclosing this in a augroup, would be usefull to add au! on a first line to clear this group

5
  • All my autocmd are enclosed within the same group called vimrc, I usually do source % on my .vimrc after changes and I do not want duplicates. Should I group autocmd by type such as js-indent instead. Apr 23, 2017 at 21:40
  • I am getting the error E10: \ should be followed by /, ? or & when I added the autocmd in place of the original autocmd. I have set nocompatible on top of my .vimrc, so it could be a line continuation issue from here Apr 23, 2017 at 21:44
  • It works for me with \\ so shouldn't be that thing, or you broke those lines into multiple? Check the content of your variables that are used in those autocmds. Maybe there is something needing escaping, like space or backslash. You get them from system() so there might be trailing \n at the end.
    – grodzik
    Apr 23, 2017 at 22:05
  • 1
    That may be the case, got same error when set variable with system("echo $PATH") - there was \n at the end. You need to call substitute(<variable>, '[\n\t\r]\?', '', 'g') on those variables and fnameescape(<variable>)
    – grodzik
    Apr 23, 2017 at 23:02
  • Thanks @grodzik, I got my vimrc to compile without errors now, I can see the correct values when I use echom to print the messages. Apr 23, 2017 at 23:51

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.