Source :

This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a Linux sysadmin or programmer, you may do following repetitive tasks while coding bash shell script:

  • Adding file header
  • Adding function/frame comment
  • Including default code snippet
  • Performing syntax check
  • Reading documentation about a function
  • Converting a code block to comment, and vice versa


The bash-Support Vim plugin offers easiest way to do all of the above, saving lot of time and keystrokes.

The plugin was written by Fritz Mehner, who explains the purpose of the
plugin as: “Write and run BASH-scripts using menus and hotkeys.”

This article explains how to install the plugin in 3 easy steps and 8 powerful features of the plugin.

3 Steps to Install the bash-support plugin

Step 1: Download the bash-support plugin

Download the plugin from vim.org website.

$ cd /usr/src
$ wget -O bash-support.zip http://www.vim.org/scripts/download_script.php?src_id=9890

Step 2: Install the bash-support Vim Plugin

$ mkdir ~/.vim # if the directory does not exist already
$ cd ~/.vim
$ unzip /usr/src/bash-support.zip

Step 3: Enable the plugin in the ~/.vimrc

Add the following line to the ~/.vimrc to enable the plugin for Vim editor.

$ vim ~/.vimrc
filetype plugin on

8 Powerful Features of Bash Vim Plugin

Feature 1: Add Automatic Header to *.sh file

When you open a file with the extension .sh it opens the file with header as shown below. This will also place the cursor in the Description field in Insert mode.

#!/bin/bash
#============================================================
#
# FILE: myscript.sh
#
# USAGE: ./myscript.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: (),
# COMPANY:
# VERSION: 1.0
# CREATED: 02/14/09 15:42:08 IST
# REVISION: ---
#============================================================


To change the default value of the AUTHOR and COMPANY, add the following lines in ~/.vimrc

let g:BASH_AuthorName   = 'SathiyaMoorthy'
let g:BASH_Email = 'subscribe@thegeekstuff.com'
let g:BASH_Company = 'Open Source Corporation'


Now, when you create a new bash script file, it will show the modified values for AUTHOR and COMPANY as shown below.

 
#!/bin/bash
#============================================================
#
# FILE: myscript.sh
#
# USAGE: ./myscript.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: SathiyaMoorthy (), subscribe@thegeekstuff.com
# COMPANY: Open Source Corporation
# VERSION: 1.0
# CREATED: 02/14/09 15:39:58 IST
# REVISION: ---
#============================================================


Note: To add custom fields to the header, modify the
~/.vim/perl-support/templates/bash-file-header file and add your own
custom field.

Feature 2: Adding Bash Function Using \sfu

For writing a subroutine, type \sfu in normal mode, which will prompt
for the function name (as shown in Fig1 below) and inserts the
subroutine with default function content (as shown in Fig2 below).

Fig 1: Type \sfu to add a bash function inside a shell script

Fig 2: Bash function added automatically inside shell script

Feature 3: Insert a Function Header Using \cfu

For inserting a function header, type \cfu in normal mode, which shows comments as shown in Fig 3.

Fig 3: Type \cfu to insert a function header inside a shell script

Feature 4: Add a Frame Comment Using \cfr

To add a frame comment, type \cfr in normal mode, which will give the following formatted comment as shown in Figure 4.

Fig 4: Type \cfr to insert a frame comment inside a shell script

Feature 5: Insert Bash Statements Inside Shell Script

Short cut keys to insert statements are:

  • \sc case in … esac
  • \sl elif then
  • \sf for in do done
  • \sfo for ((…)) do done
  • \si if then fi
  • \sie if then else fi
  • \ss select in do done
  • \st until do done
  • \sw while do done
  • \sfu function
  • \se echo ­e “\n”
  • \sp printf “\n”

Example: Insert the Case Statement inside a shell script automatically

\sc will insert the case statements and places the cursor next to the
case statement in INSERT mode as shown in figure 5. Like this you can
use all the mentioned short cut keystrokes to get the appropriate
statement in the table 1.

Fig 5: Type \sc to insert case statement inside bash shell script

Feature 6: Insert Predefined code-snippet to the Bash Script Using \nr

Code snippets can be read / written by using \nr and \nw
respectively. The plugin comes with few pre-defined code snippets that
you can insert into your code. Following are the default code snippets
that comes with the plugin.

$ ls -1 ~/.vim/bash-support/codesnippets/
assert
basename+pathname
basename-function
check-number-of-command-line-arguments
create-tempfile
create-tempfile-with-trap
free-software-comment
read-and-split-into-array
timestamp
usage-and-command-line-arguments.noindent
use-file-descriptor-read
use-file-descriptor-write
well-behaved-script


To include the check-number-of-command-line-arguments code snippet,
press \nr and you will be prompted for a file name. Give the file name
as check-number-of-command-line-arguments and the following code will be
automatically inserted to the shell-script.

#-----------------------------------------------------------------------
# Check number of command line arguments
#-----------------------------------------------------------------------
if [ $# -lt 1 ]
then
echo -e "\n\tUsage: ${0##/*/} File\n"
exit 1
fi


Note: You can define your own code snippets and place
it under ~/.vim/bash-support/codesnippets/. You can also build your own
code snippets from the existing code – select the part of code need to
be made as code snippet, press \nw, and give a file-name to it. From
next time, type \nr and the file-name to get your custom code snippet.

Feature 7: Get Quick Help on the Bash Builtin Commands

When you need to read help page for the bash builtins use \hh when the cursor is in the word.

In the following example (Fig 6), the read bash builtin command is
selected and \hh is typed, which displayed the quick-help on the read
command. Use the same method to get quick-help on all bash builtin
commands.

Fig 6: Type \hh to get help about the selected bash builtin command

Feature 8: Featured Commenting

Following commands will add the corresponding keyword comments. For
example, type \ckb to add the BUG comment line inside shell-script.

  • \ckb Keyword BUG
  • \ckt Keyword TODO
  • \ckr Keyword Tricky
  • \ckw Keyword WARNING


Type \ckt to add a comment line with the keyword “# :TODO: mm/dd/yy:: “.
This is basically a comment line that acts as a TODO, where you can
type the items that you would like to get it done later.

Fig 7: Type \ckt to add TODO inside bash shell script


There are lot of powerful features in the bash-support Plugin. Read the
documentation for more information. The documentation is located in the
following location on your system.

Recommended Reading

Vim 101 Hacks, by Ramesh Natarajan.
I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim
editors. Several years back, when I wrote lot of C code on Linux, I used
to read all available Vim editor tips and tricks. Based on my Vim
editor experience, I’ve written Vim 101 Hacks eBook that contains 101
practical examples on various advanced Vim features that will make you
fast and productive in the Vim editor. Even if you’ve been using Vi and
Vim Editors for several years and have not read this book, please do
yourself a favor and read this book. You’ll be amazed with the
capabilities of Vim editor.

Vim ide for shell development的更多相关文章

  1. lua IDE for cocos2d-x development

    原文链接:http://hi.baidu.com/balduc0m/item/648093dad238bd2a39f6f78e lua IDE for cocos2d-x development -- ...

  2. vim IDE平台-打造属于自己的配置

    vim IDE平台-打造属于自己的配置 一.前言 目前工作环境基本以Linux为主,自然用到VIM也很多,很早就对如何提高VIM的使用效率有所研究,限于时间关系,也没做个系统记录和资料积累,时间久了又 ...

  3. vim中执行shell命令小结

    vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如:!ls -l ...

  4. vim之执行shell命令

    vim中执行shell命令,有以下几种形式 (1) :!command 不退出vim, 并执行shell命令command, 将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 (2)   ...

  5. VIM IDE

    打造VIM IDE(针对C语言开发者)   ================================使用vim打造IDE, 针对C语言开发者建议使用gvim================== ...

  6. dotfiles for linux/unix users automatically! (python Vim IDE)

    Here is a brief introduction and package of dotfiles for linux/unix user. I think there are enough i ...

  7. Vim编辑器与shell脚本

      目录                                                          Vim文本编辑器 Shell脚本 Shell编程变量 流程控制语句 计划任务 ...

  8. 4.Vim编辑器与Shell命令脚本

    第4章 Vim编辑器与Shell命令脚本 章节简述: 本章首先讲解如何使用Vim编辑器来编写.修改文档,然后通过逐个配置主机名称.系统网卡以及Yum软件仓库参数文件等实验,帮助读者加深Vim编辑器中诸 ...

  9. 一键打造vim ide 支持python golang shell等高级特性

    1.vim-for-devops github: https://github.com/yxxhero/vim_for_devops 利用vim插件打造支持python.shell.golang的id ...

随机推荐

  1. jquery事件重复绑定解决办法

    一$.fn.live 重复绑定 解决:使用die()方法,在live()方法绑定前,将此元素上的前面被绑定的事件统统解除,然后再通过live()方法绑定新的事件. //先通过die()方法解除,再通过 ...

  2. java基础-操作符

    浏览以下内容前,请点击并阅读 声明 定义:操作符是一种能够对一个,两个或三个操作对象执行特定的操作,并返回一个结果的特定符号. java中操作符的种类和优先级如下表所示,除了赋值运算,所有二元操作符运 ...

  3. BZOJ2981 : [Poi2002]括号

    对于最终加入了括号的序列,对其求中缀表达式,建树. 可以发现$n-1$个运算符DFS序递增,且若一个-上方往左走了奇数次,则它就是+,否则就是-. 所以考虑DP,设$f[i][j]$表示考虑了前$i$ ...

  4. 让mysql有直接写redis能力

    1.文件包下载 http://pan.baidu.com/s/1qW9DHYc 2.安装 gcc -fPIC -Wall -I/usr/local/mysql/include/mysql -I. -s ...

  5. [Java] 利用LinkedHashMap来实现LRU Cache

    http://www.acmerblog.com/leetcode-lru-cache-lru-5745.html https://oj.leetcode.com/discuss/1188/java- ...

  6. 3分钟4个步骤超级简单入门配置lamp

    按照我下面的推荐博客进行3步安装,最后进行一步测试就完成了.环境:我的环境是在windows10中安装的VMware中安装的Ubuntu虚拟机,Windows8,7应该是一样的测试:分别用localh ...

  7. iOS 开发小结

    一,经历 1> 在编写以前有过的类似的新功能时,如果以前的开发人员没有写明明确的注释和开发需求,一定要仔细阅读所有代码,每一句代码都有它存在的意义. 2> 例如,只以为是[self.ful ...

  8. 热烈庆祝华清远见成功自主研发Farsight TV 智能机顶盒

    近日,华清远见研发中心再传喜讯:Farsight TV 智能机顶盒研发成功并投入教学!这是华清远见研发中心继开源平板电脑.智能医疗终端.智能家居终端后独立成功研发的又一智能硬件!至此,开创了华清远见自 ...

  9. ELK_日志分析系统Logstash+ElasticSearch+Kibana4

    elk 日志分析系统 Logstash+ElasticSearch+Kibana4 logstash 管理日志和事件的工具 ElasticSearch 搜索 Kibana4 功能强大的数据显示客户端 ...

  10. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...