How to Make ‘Vim Editor’ as Bash-IDE Using ‘bash-support’ Plugin in Linux

[‘

n

An IDE (Integrated Development Environment) is simply software that offers much-needed programming facilities and components in a single program, to maximize programmer productivity. IDEs put forward a single program in which all development can be done, enabling a programmer to write, modify, compile, deploy and debug programs.

n

In this article, we will describe how to install and configure the Vim editor as a Bash-IDE using the bash-support vim plug-in.

n

What is bash-support.vim plug-in?

n

bash-support is a highly-customizable vim plug-in, which allows you to insert: file headers, complete statements, comments, functions, and code snippets. It also enables you to perform syntax checking, make a script executable, start a debugger simply with a keystroke; do all this without closing the editor.

n

It generally makes bash scripting fun and enjoyable through organized and consistent writing/insertion of file content using shortcut keys (mappings).

n

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

n

How To Install Bash-support Plug-in in Linux

n

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

n

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

n

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:

n

$ mkdir ~/.vimrn$ cd .vimrn$ unzip ~/Downloads/bash-support.ziprn

n

Next, activate it from the .vimrc file:

n

$ vi ~/.vimrcrn

n

By inserting the line below:

n

filetype plugin on   rnset number   #optionally add this to show line numbers in vimrn

n

How To Use Bash-support plug-in with Vim Editor

n

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.

n

Important:

n

    n

  1. All mappings ((\)+charater(s) combination) are filetype specific: they are only working with ‘sh’ files, in order to avoid conflicts with mappings from other plug-ins.
  2. n

  3. 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).
  4. n

n

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

n

How To Generate an Automatic Header for New Scripts

n

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

n

Script Sample Header Options
Script Sample Header Options

n

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 like the one below) to start the template setup wizard.

n

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

n

$ vi test.shrn

n

Set Personalizations in Scripts File
Set Personalizations in Scripts File

n

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

n

Set Personalization File Location
Set Personalization File Location

n

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.

n

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

n

Add Info in Script Header
Add Info in Script Header

n

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 screenshot below:

n

$ test2.shrn

n

Auto Adds Header to Script
Auto Adds Header to Script

n

Make Bash-support Plug-in Help Accessible

n

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

n

:helptags $HOME/.vim/doc/rn

n

Add Plugin Help in Vi Editor
Add Plugin Help in Vi Editor

n

How To Insert Comments in Shell Scripts

n

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

n

Add Comments to Scripts
Add Comments to Scripts

n

How To Insert Statements in a Shell Script

n

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

n

    n

  1. \sc – case in … esac (n, I)
  2. n

  3. \sei – elif then (n, I)
  4. n

  5. \sf – for in do done (n, i, v)
  6. n

  7. \sfo – for ((…)) do done (n, i, v)
  8. n

  9. \si – if then fi (n, i, v)
  10. n

  11. \sie – if then else fi (n, i, v)
  12. n

  13. \ss – select in do done (n, i, v)
  14. n

  15. \su – until do done (n, i, v)
  16. n

  17. \sw – while do done (n, i, v)
  18. n

  19. \sfu – function (n, i, v)
  20. n

  21. \se – echo -e “…” (n, i, v)
  22. n

  23. \sp – printf “…” (n, i, v)
  24. n

  25. \sa – array element, ${.[.]} (n, i, v) and many more array features.
  26. n

n

Insert a Function and Function Header

n

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

n

Insert New Function in Script
Insert New Function in Script

n

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

n

Create Header Function in Script
Create Header Function in Script

n

More Examples of Adding Bash Statements

n

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

n

Add Insert Statement to Script
Add Insert Statement to Script

n

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

n

Add echo Statement to Script
Add echo Statement to Script

n

How To Use Run Operation in Vi Editor

n

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

n

    n

  1. \rr – update file, run script (n, I)
  2. n

  3. \ra – set script cmd line arguments (n, I)
  4. n

  5. \rc – update file, check syntax (n, I)
  6. n

  7. \rco – syntax check options (n, I)
  8. n

  9. \rd – start the debugger (n, I)
  10. n

  11. \re – make script executable/not exec.(*) (in)
  12. n

n

Make Script Executable

n

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

n

Make Script Executable
Make Script Executable

n

How To Use Predefined Code Snippets To a Bash Script

n

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:

n

$ .vim/bash-support/codesnippets/rn

n

List of Code Snippets
List of Code Snippets

n

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

n

Add Code Snippet to Script
Add Code Snippet to Script

n

Create Custom Predefined Code Snippets

n

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:

n

    n

  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. n

  3. to read it, type \nr and use the filename to add your custom code snippet.
  4. n

n

View Help For the Built-in and Command Under the Cursor

n

To display help, in normal mode, type:

n

    n

  1. \hh – for built-in help
  2. n

  3. \hm – for a command help
  4. n

n

View Built-in Command Help
View Built-in Command Help

n

For more reference, read through the file :

n

~/.vim/doc/bashsupport.txt  #copy of online documentationrn~/.vim/doc/tagsrn

n

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

n

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.

n

‘]