Function: bash-ts-mode

bash-ts-mode is an autoloaded, interactive and byte-compiled function defined in sh-script.el.gz.

Signature

(bash-ts-mode)

Documentation

Major mode for editing Bash shell scripts.

This mode automatically falls back to sh-mode if the buffer is not written in Bash or sh.

In addition to any hooks its parent mode sh-base-mode might have run, this mode runs the hook bash-ts-mode-hook, as the final or penultimate step during initialization.

This function has :around advice: sh--redirect-bash-ts-mode.

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/sh-script.el.gz
;;;###autoload
(define-derived-mode bash-ts-mode sh-base-mode "Bash"
  "Major mode for editing Bash shell scripts.
This mode automatically falls back to `sh-mode' if the buffer is
not written in Bash or sh."
  :syntax-table sh-mode-syntax-table
  (when (treesit-ensure-installed 'bash)
    (sh-set-shell "bash" nil nil)
    (add-hook 'flymake-diagnostic-functions #'sh-shellcheck-flymake nil t)
    (add-hook 'hack-local-variables-hook
              #'sh-after-hack-local-variables nil t)
    (setq treesit-primary-parser (treesit-parser-create 'bash))
    (setq-local treesit-font-lock-feature-list
                '(( comment function)
                  ( command declaration-command keyword string)
                  ( builtin-variable constant heredoc number
                    string-interpolation variable)
                  ( bracket delimiter misc-punctuation operator)))
    (setq-local treesit-font-lock-settings
                sh-mode--treesit-settings)
    (setq-local treesit-thing-settings
                `((bash
                   (list
                    ,(rx bos (or "do_group"
                                 "if_statement"
                                 "case_statement"
                                 "compound_statement"
                                 "subshell"
                                 "test_command"
                                 "parenthesized_expression"
                                 "arithmetic_expansion"
                                 "brace_expression"
                                 "string"
                                 "array"
                                 "expansion" ;; but not "simple_expansion"
                                 "command_substitution"
                                 "process_substitution")
                         eos))
                   (sexp-default
                    ;; For `C-M-f' in "$|(a)"
                    ("$(" .
                     ,(lambda (node)
                        (equal (treesit-node-type (treesit-node-parent node))
                               "command_substitution"))))
                   (sentence
                    ,(rx bos (or "redirected_statement"
                                 "declaration_command"
                                 "unset_command"
                                 "command"
                                 "variable_assignment")
                         eos))
                   (text
                    ,(rx bos (or "comment"
                                 "heredoc_body")
                         eos)))))
    (setq-local treesit-defun-type-regexp "function_definition")
    (setq-local treesit-defun-name-function
                (lambda (node)
                  (treesit-node-text
                   (treesit-node-child-by-field-name node "name")
                   t)))
    (setq-local treesit-simple-imenu-settings
                '((nil "\\`function_definition\\'" nil nil)))
    ;; Override regexp-based outline variable from `sh-base-mode'
    ;; to use `treesit-simple-imenu-settings' for outlines:
    (kill-local-variable 'outline-regexp)

    (treesit-major-mode-setup)))