Vim ide for shell development

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        = ‘[email protected]‘
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 (), [email protected]
#       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.

时间: 2024-08-03 15:04:32

Vim ide for shell development的相关文章

VIM IDE

打造VIM IDE(针对C语言开发者) ================================使用vim打造IDE, 针对C语言开发者建议使用gvim================================ 先上两个截图 # 安装ctags1. 下载地址: http://ctags.sourceforge.net/ # 安装cscope1. 下载地址: http://cscope.sourceforge.net/ 2. 修改源码,使其支持递归搜索文件夹的软链接   修改文件:

emacs vim IDE

原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的emacs学习教程,如果真想学习emacs我觉得还是找个Unix-like的操作系统(MAC OSX和Linux都行)来学习更佳,emacs本身就不是做给windows的. http://www.cnblogs.com/robertzml/archive/2009/09/10/1564108.html e

VIM中执行Shell命令(炫酷)

      我对VIM的认识仅仅是一款源码编辑器,在Linux下用来编辑程序源码或者某些服务的配置文件.最近无意中看到vim中竟然可以执行shell命令.第一次见,说句实话感觉好炫酷!不多说,我查了一些资料,一个一个尝试一下,留个笔记,备查.       vim中执行shell命令,有以下几种形式:       第一种 :!command       不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容.执行:!ls -al命令,如下图

vim中执行shell命令小结

vim中执行shell命令,有以下几种形式 1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如 :!ls -l 特别的可以运行:!bash来启动一个bash shell并执行命令,不需要退出vim 2):r !command 将shell命令command的结果插入到当前行的下一行 例如 :r !date,读取系统时间并插入到当前行的下一行. 3):起始行号,结束行号 !command 将起始行号和结束行

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 informative description about the package. Here is the link: https://github.com/xros/dotfiles Mostly it is very neat for python programming within Vim.

Vim编辑器和Shell命令脚本

Vim编辑器和Shell命令脚本 Vim文本编辑器 Vim文本编辑器内设有三种模式:命令模式.末行模式和编辑模式. 命令模式:控制光标移动,可对文本进行删除.复制.粘贴和查找等工作. 输入模式:正常的文本录入. 末行模式:保存.退出与设置编辑环境. 命令模式和末行模式下的一些快捷键: 末行模式下的命令: Shell命令脚本 Shell终端解释器类似人与计算机硬件的翻译官,作为用户与Linux系统内部通讯的媒介.Shell脚本命令的工作方式有两种,首先是前面所接触的交互方式,即当用户每输入一条命令

vim,grep,shell脚本实例及find用法

vim,grep,shell脚本实例及find用法 1.定义一个对所有用户都生效的命令别名 定义一个对所有用户都生效的命令别名需要更改全局配置文件/etc/bashrc, 例如,我们以root用户编辑/etc/bashrc,在文件的最后一行增加alias like='ls' 当我们新启一个shell进程的时候,列出命令别名,会发现刚定义的别名like [[email protected] ~]# tail /etc/bashrc . "$i" >/dev/null fi fi d

vim中执行shell命令

1):!command 不退出vim,并执行shell命令command,将命令输出显示在vim的命令区域,不会改变当前编辑的文件的内容 例如 :!ls -l 特别的可以运行:!bash来启动一个bash shell并执行命令,不需要退出vim 2):r !command 将shell命令command的结果插入到当前行的下一行 例如 :r !date,读取时间并插入到当前行的下一行.

8.19 打造VIM IDE 静态库 动态库制作

vim配置文件位置: /etc/vim/vimrc ~/.vimrc 打造IDE步骤 ,ta   ,nn 测试 使用大型IDE ,da      生成文档说明 ,dd      生成函数说明 ,jd       跳转函数 ,o         关闭其他窗口 ,bf 显示已经打开的文件列表 gcc参数的使用: linux下制作动态库,静态库,下面是文件结构图: 制作静态库,静态库以 .a 结尾: src里的makefile 生成 静态库文件 libcalc.a: gcc -c *.c ar rcs