Using ‘bash-support’ vim Plugin in Linux

от автора

в

The current version plug-in is 4.3, version 4.0 was a rewriting of version 3.12.1; versions 4.0 or better, are based on a comprehensively new and more powerful template system, with changed template syntax unlike previous versions.

How To Install Bash-support Plug-in in Linux

Start by downloading the latest version of bash-support plug-in using the command below:

$ cd Downloads
$ curl http://www.vim.org/scripts/download_script.php?src_id=24452 >bash-support.zip

Then install it as follows; create the .vim directory in your home folder (in case it doesn’t exist), move into it and extract the contents of bash-support.zip there:

$ mkdir ~/.vim
$ cd .vim
$ unzip ~/Downloads/bash-support.zip

Next, activate it from the .vimrc file:

$ vi ~/.vimrc

By inserting the line below:

filetype plug-in on   
set number   #optionally add this to show line numbers in vim
How To Use Bash-support plug-in with Vim Editor

To simplify its usage, the frequently used constructs as well as certain operations can be inserted/performed with key mappings respectively. The mappings are described in ~/.vim/doc/bashsupport.txt and ~/.vim/bash-support/doc/bash-hotkeys.pdf or ~/.vim/bash-support/doc/bash-hotkeys.tex files.

Important:
  1. All mappings ((\)+charater(s) combination) are filetype specific: they are only work with ‘sh’ files, in order to avoid conflicts with mappings from other plug-ins.
  2. Typing speed matters-when using key mapping, the combination of a leader ('\') and the following character(s) will only be recognized for a short time (possibly less than 3 seconds – based on assumption).

Below are certain remarkable features of this plug-in that we will explain and learn how to use:

How To Generate an Automatic Header for New Scripts

Look at the sample header below, to have this header created automatically in all your new bash scripts, follow the steps below.

Script Sample Header Options

Script Sample Header Options

Start by setting your personal details (author name, author reference, organization, company etc). Use the map \ntw inside a Bash buffer (open a test script as the one below) to start the template setup wizard.

Select option (1) to setup the personalization file, then press [Enter].

$ vi test.sh
Set Personalizations in Scripts File

Set Personalizations in Scripts File

Afterwards, hit [Enter] again. Then select the option (1) one more time to set the location of the personalization file and hit [Enter].

Set Personalization File Location

Set Personalization File Location

The wizard will copy the template file .vim/bash-support/rc/personal.templates to .vim/templates/personal.templates and open it for editing, where you can insert your details.

Press i to insert the appropriate values within the single quotes as shown in the screenshot.

Add Info in Script Header

Add Info in Script Header

Once you have set the correct values, type :wq to save and exit the file. Close the Bash test script, open another script to check the new configuration. The file header should now have your personal details similar to that in the screen shot below:

$ test2.sh
Auto Adds Header to Script

Auto Adds Header to Script

Make Bash-support Plug-in Help Accessible

To do this, type the command below on the Vim command line and press [Enter], it will create a file .vim/doc/tags:

:helptags $HOME/.vim/doc/
Add Plugin Help in Vi Editor

Add Plugin Help in Vi Editor

How To Insert Comments in Shell Scripts

To insert a framed comment, type \cfr in normal mode:

Add Comments to Scripts

Add Comments to Scripts

How To Insert Statements in a Shell Script

The following are key mappings for inserting statements (n – normal mode, i – insert mode):

  1. \sc – case in … esac (n, I)
  2. \sei – elif then (n, I)
  3. \sf – for in do done (n, i, v)
  4. \sfo – for ((…)) do done (n, i, v)
  5. \si – if then fi (n, i, v)
  6. \sie – if then else fi (n, i, v)
  7. \ss – select in do done (n, i, v)
  8. \su – until do done (n, i, v)
  9. \sw – while do done (n, i, v)
  10. \sfu – function (n, i, v)
  11. \se – echo -e “…” (n, i, v)
  12. \sp – printf “…” (n, i, v)
  13. \sa – array element, ${.[.]} (n, i, v) and many more array features.
Insert a Function and Function Header

Type \sfu to add a new empty function, then add the function name and press [Enter] to create it. Afterwards, add your function code.

Insert New Function in Script

Insert New Function in Script

To create a header for the function above, type \cfu, enter name of the function, click [Enter] and fill in the appropriate values (name, description, parameters and returns):

Create Header Function in Script

Create Header Function in Script

More Examples of Adding Bash Statements

Below is an example showing insertion of an if statement using \si:

Add Insert Statement to Script

Add Insert Statement to Script

Next example showing addition of an echo statement using \se:

Add echo Statement to Script

Add echo Statement to Script

How To Use Run Operation in Vi Editor

The following is a list of some run operations key mappings:

  1. \rr – update file, run script (n, I)
  2. \ra – set script cmd line arguments (n, I)
  3. \rc – update file, check syntax (n, I)
  4. \rco – syntax check options (n, I)
  5. \rd – start debugger (n, I)
  6. \re – make script executable/not exec.(*) (in)
Make Script Executable

After writing script, save it and type \re to make it executable by pressing [Enter].

Make Script Executable

Make Script Executable

How To Use Predefined Code Snippets To a Bash Script

Predefined code snippets are files that contain already written code meant for a specific purpose. To add code snippets, type \nr and \nw to read/write predefined code snippets. Issue the command that follows to list default code snippets:

$ .vim/bash-support/codesnippets/
List of Code Snippets

List of Code Snippets

To use a code snippet such as free-software-comment, type \nr and use auto-completion feature to select its name, and press [Enter]:

Add Code Snippet to Script

Add Code Snippet to Script

Create Custom Predefined Code Snippets

It is possible to write your own code snippets under ~/.vim/bash-support/codesnippets/. Importantly, you can also create your own code snippets from normal script code:

  1. choose the section of code that you want to use as a code snippet, then press \nw, and closely give it a filename.
  2. to read it, type \nr and use the filename to add your custom code snippet.
View Help For the Built-in and Command Under the Cursor

To display help, in normal mode, type:

  1. \hh – for built-in help
  2. \hm – for a command help
View Built-in Command Help

View Built-in Command Help

For more reference, read through the file :

~/.vim/doc/bashsupport.txt  #copy of online documentation
~/.vim/doc/tags

Visit the Bash-support plug-in Github repository: https://github.com/WolfgangMehner/bash-support
Visit Bash-support plug-in on the Vim Website: http://www.vim.org/scripts/script.php?script_id=365

That’s all for now, in this article, we described the steps of installing and configuring Vim as a Bash-IDE in Linux using bash-support plug-in. Check out the other exciting features of this plug-in, and do share them with us in the comments.