DEV Community

Discussion on: Setting up Vim for Bash Scripting

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

You can implement linting and error checking for your shell scripts as well.

First, create $HOME/.vim/compiler/shellcheck.vim and put the following configurations in it:

CompilerSet makeprg=shellcheck\ -f\ gcc
CompilerSet errorformat=%f:%l:%c:\ %trror:\ %m\ [SC%n],
               \%f:%l:%c:\ %tarning:\ %m\ [SC%n],
               \%f:%l:%c:\ %tote:\ %m\ [SC%n],
               \%-G%.%#
Enter fullscreen mode Exit fullscreen mode

You can learn more about this by reading the vim docs on writing a compiler plugin.

In short, makeprg tells vim how to execute a program when you invoke :make and errorformat instructs vim how to parse the output of the program to load it into the quickfix list.

With these settings, you can run :make on any shell script to run shellcheck and get a list of errors in vim.

Once results are in the quickfix list, you can navigate the errors with :cnext/:cprevious, :cfirst/:clast, or :copen to open the quickfix list.

For convenience, I recommend mapping :cnext and :cprevious to something like ]q and [q. See tpope/vim-unimpaired for mapping inspiration.

Some plugins aim to automatically do all the linting integration heavylifting for you, like ALE, Syntastic, and NeoMake, and some plugins aim provide a Language Server Protocol (LSP) integration in vim, like vim-lsp, coc.nvim, and now neovim with its built-in LSP implementation.

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

WOW! Thats a great suggestion, but I wanted to make it simple for beginners, still a fantastic recommendation :)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

I agree with making things simple for beginners. I just wanted to illustrate the possible range of customizability with and without plugins.