Emacs: sh-script package

This article will describe using sh-script package which provides major mode for shell script. This package is built-in emacs.

1 Run sh-script

Open *.sh file or run M-x sh-mode.

2 ${HOME}/.emacs

sh-basic-offset and sh-indentation are tab width. sh-shell-file is default of shebang. If not setting sh-shell-file, SHELL variable will be default.

(setq sh-basic-offset 2)
(setq sh-indentation 2)
(setq sh-shell-file "/bin/sh")

3 Key bindings

Key bindings helps to run and to input syntax. In case of pressing C-c C-w, while statement will be inputed.

condition: [  ]

3.1 Running

Key bindings for running is as below.

C-c : Add shebang and run "chmod a+x" to file"
C-c x Run shell script
C-M-x RUn shell script to selected region

Selected region is done by C-space / C-Shift-space which is used for copy.

0001_Region.png

3.2 Syntax

Key bindings for syntax is as below.

C-c C-w while statement
C-c C-i if statement
C-c C-c case statement
C-c C-f for statement
C-c + `expr $v + 1`

3.3 Temporally variable

Key bindings for temporally variable is as below. This temporally variable will be removed with trap when shell script is over.

C-c C-t Create temporally variable with mktemp and trap