DEV Community

Cover image for Setting up Vim for Bash Scripting
Meet Rajesh Gor
Meet Rajesh Gor

Posted on • Updated on • Originally published at mr-destructive.github.io

Setting up Vim for Bash Scripting

Vim and BASH?

Bash Scripting is a powerful skill to have as a programmer because we find Linux almost everywhere and to get through it you must have a command over its interface which is generally the BASH shell. Vim is a great option for doing this, or probably the best out there! Because Vim is pre-installed in almost every Linux distribution. This is not an in-depth setup for BASH on Vim, it is a simple editorial on starting up BASH scripting on the Vim editor. So without wasting time on "Vim features" let's dive in with the setup for BASH in Vim.

Boilerplate macro

Setting up a bash script doesn't require much code but still in some cases it can be a bit hassle and to avoid the repetitive task, one can easily set up a macro for the boilerplate BASH script.

nnoremap bs i#!/bin/bash/<ESC>o
Enter fullscreen mode Exit fullscreen mode

Ok that was pretty dumb but it can scale pretty quickly and it will be nice to tailor it as per needs, here's some snippet with function pre-loaded.

nnoremap bs i#!/bin/bash/<ESC>o<ESC>ofunction main(){<ESC>o<ESC>o}<ESC>ki<S-TAB>

Enter fullscreen mode Exit fullscreen mode

type bs to load boiler-plate code

When the key bs is typed in normal mode, you enter into insert mode as per the command macro, then we type in the required text and escape to move to the next line and continue the same stuff. This could be extended further like making some input or printing out some text and any other formatted text that you could think it as repetition.

Sourcing Scripts

So, after creating the file, sourcing the script, and running it can be a bit slow for some people, as you have to go to the terminal and toggle in the permission to run the script and then run, But pull on your seatbelts as this is VIM! You can die due to slowness!

nnoremap sh :!chmod +x % && source %
Enter fullscreen mode Exit fullscreen mode

type sh to run script

When the sh keys are typed in the normal mode, the preceding command after ! (bang) will be executed in the terminal, the && keywords will execute the second command only when the first command is successfully executed.
That just can so fast! Imagine doing this for long scripts and especially for debugging, it will waste 2 minutes every time you leave the editor and for 10 times you do the debugging, you will carelessly was roughly 20 minutes! Improve your debugging skills surely :)

Plugins

There are very few plugins out there for BASH as for VIM, but it's quite to write scripts even without any plugins. One of the most supported and popular plugins for BASH in Vim is Bash-Support-Vim for auto-completion and Shell-Check for finding/correcting any bugs or error in the script .
The mentioned plugin is quite awesome and it can greatly improve the speed of scripting for BASH, some commands such as shortcuts for writing if-else, while, for loops, commenting and other aspects in the scripting. The thorough documentation for such commands is also provided by the plugin website.
This can be used for autocompleting keywords and writing nested if-else and other logical operators in BASH scripting. Again, you can do absolutely fine without plugins in Vim as it is heavily customizable to the user's need and can be very rewarding to set up your own configuration for BASH. You can use standard Vim(barebones) for auto-completion as well with the command CTRL+N and CTRL-P to move down and up respectively.

Some More Tricks

BASH in Vim can be quite versatile to use as it provides some custom addons to make the script more functional and easier to understand. Some tricks such as using autocompletion can be quite inconvenient to use at once but it can get really smooth after some runs at writing the scripts.

  • In BASH Scripts there are quite a lot of brackets to play with that's why to jump around swiftly around such parentheses or brackets you can use % to move from the opened to closed brackets or vice versa.
  • You can execute any terminal command from Vim, be sure to be in command mode and press ! after the command, you would like to execute. This will run the command from the terminal and you don't have to leave the editor, it saves a ton of time and it's blazingly fast.
  • With the above trick, you kind of have a superpower within Vim to make, build, source, run the files or scripts within Vim, that is not repetition but it can run bash within bash. Ok! that's was pretty fast. Don't die of quickness now!

Writing BASH scripts in Vim can be also boosted by using some built-in commands such as adding comments for multiple lines at once and some unexplored stuff which can be learned in the way to understanding the flow of Vim and BASH together. Happy Coding and Viming :)

Top comments (5)

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.

Collapse
 
a_b profile image
Al B

Iā€™m curious to see how vim community utilizing new vim built-in terminal hotness for shell scripting.

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Thanks, I'll add that