New files with the `.sh` extension will load the shebang and strict-mode options. Also: - Note that short message settings do not completely remove 'Hit Enter to continue' messages.
@@ -0,0 +1,6 @@
+" Template for shell scripts.
+" Removes comments after line 1, places cursor at end of file.
+autocmd BufNewFile *.sh 0r ~/dotfiles/vim/templates/bashscript | 2,$ g/^#/d | normal G
+
+" Make shell scripts executable on save.
+autocmd BufWritePost *.sh if getline(1) =~ "^#!/bin/" | silent !chmod +x <afile> | endif
@@ -0,0 +1,20 @@
+#!/bin/bash
+#
+# The following comments are all removed when loading this template:
+# The Unofficial "Bash Strict Mode"
+# -e Failed commands exit
+# -u Unbound variables exit
+# -o pipefail Do not hide piped-command failures
+# IFS as newline and tab are better for loops (and name with spaces).
+# Tips:
+# - Options can be switched off and on as required:
+# - Toggle `-e` if previous command's return value is needed (`$?`).
+# - Toggle `-u` if sourcing another non-conforming script.
+# - Use default values for positional parameters i.e. `${1:-}`.
+# - Intentionally undefined variables should be set to "" rather than not set.
+# - Learn about "exit traps" to perform `try-catch-finally`-like cleanup tasks
+set -euo pipefail
+IFS=$'\n\t'
@@ -41,7 +41,7 @@ set winminheight=0 " Windows can be 0 line high
set wildmenu " Show list instead of just completing
set modeline " Support vim modelines at the top of files
set whichwrap=b,s,h,l,<,>,[,] " Backspace and cursor keys wrap too
-set shortmess+=filmnrxoOtT " Abbrev. of messages (avoids 'hit enter')
+set shortmess+=filmnrxoOtT " Abbrev. of messages (reduces 'hit enter')
set scrolloff=5 " Minimum lines to keep above and below cursor
set sidescroll=1
set sidescrolloff=10