Table of Contents

Shell Scripts layer

Table of ContentsClose

fish.png

1. Description

This simple layer adds support for shell scripting.

Supported scripting files:

Note: For Windows scripting see the layer windows-scripts

1.1. Features:

2. Install

2.1. Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add shell-scripts to the existing dotspacemacs-configuration-layers list in this file.

2.2. Linting

In order to enable sh scripts linting, install shellcheck.

2.3. Style checking

In order to enable sh scripts style checking, install bashate.

2.4. Format

To support formatting of buffers you need to install the application shfmt. This can be done like shown below

go install mvdan.cc/sh/v3/cmd/shfmt@latest

2.5. Format on save

To enable automatic formatting on save, set the layer variable shell-scripts-format-on-save to t:

(shell-scripts :variables shell-scripts-format-on-save t)

2.6. Backends

You have the choice between two different backends with different setup instructions and different capabilities.

The LSP backend is automatically selected when the lsp layer is used and is the recommended one.

2.6.1. LSP

This backend uses an external server to provide the various IDE integrations and a more modern UI integration in spacemacs.

However it only supports working with bash scripts.

It requires installing the external server via:

npm i -g bash-language-server

You can find further information about the project at its GitHub page.

Set the variable shell-scripts-backend to select the LSP backend explicitly. This will result in the lsp layer being loaded.

(shell-scripts :variables shell-scripts-backend 'lsp)

2.6.2. Shell-script-mode

This is the more basic alternative for working with shell scripts in emacs. It works by using an emacs major mode and is slower and less complete than the LSP alternative.

It supports bash and fish shell scripts.

Set shell-scripts-backend to nil explicitly to use this simple backend.

(shell-scripts :variables shell-scripts-backend nil)

3. Key bindings

Key binding Description
SPC m \ insert end-of-line backslashes to the lines in the region
SPC i ! insert shebang in a script file
SPC m i ! insert shebang in a script file
SPC m i c insert switch case statement if supported by shell
SPC m i i insert if statement if supported by shell
SPC m i f insert function definition if supported by shell
SPC m i o insert for loop if supported by shell
SPC m i e insert an indexed for loop if supported by shell
SPC m i w insert while loop if supported by shell
SPC m i r insert repeat loop if supported by shell
SPC m i s insert select loop if supported by shell
SPC m i u insert until loop if supported by shell
SPC m i g insert a getopts while loop if supported by shell
SPC m = format the buffer using shfmt

Author: root

Created: 2024-06-14 Fri 18:51

Validate