10

When editing a bash script file with emacs, and trying to enter <<<, at the second < emacs insert a template for a here document, like in the following example:

<<EOF

EOF

This is not the desired output, because I would enter a literal <<<.

For the moment I resort to entering < < <, and then deleting spaces, but I prefer to being allowed to enter it directly.

1
  • 2
    Not that I care too much, but It would be nice to leave a comment for a downvote.
    – enzotib
    Sep 5, 2011 at 15:08

6 Answers 6

12

There is actually no need for Tom 's custom my-disable-here-document function rebinding the keys. This feature can be enabled and disabled with sh-electric-here-document-mode:

(add-hook 'sh-mode-hook
          (lambda ()
            (sh-electric-here-document-mode -1)))

(It can also be toggled for the active buffer via M-x sh-electric-here-document-mode.)

1
  • Also: sh-mode-hook didn't work for me, but sh-set-shell-hook did. May 20, 2016 at 16:37
6

Bind < to self-insert-command in bash mode and then it will insert only the character.

By default it is bound to sh-maybe-here-document when in bash mode and that function does the auto-insertion.

Here is a way to rebound the key:

(add-hook 'sh-set-shell-hook 'my-disable-here-document)

(defun my-disable-here-document ()
  (local-set-key "<" 'self-insert-command))
3
  • Can you please give some more information on what to exactly insert in the init file? Sorry, but I am a beginner emacs's user.
    – enzotib
    Sep 5, 2011 at 10:04
  • I added a simple example to my answer. It disables the automatic insertion in every kind of shell, not just bash, but probaly that's what you want anyway.
    – Tom
    Sep 5, 2011 at 10:31
  • This solution does not work anymore (tested on emacs 24.3), because < is bound to self-insert-command by default.
    – T. Verron
    Feb 20, 2015 at 15:34
2

If the only reason you want to disable the here-doc behaviour is that it prevents you from inserting a here-string <<<, then binding C-< to a function which contains (insert "<<<") will work, and still allow the auto here-doc template

(defun my-here-string() 
  "Insert <<< (eg. for a bash here-string)" 
  (interactive)
  (insert "<<<"))
(global-set-key (kbd  "C-<") 'my-here-string)
1
  • 1
    Thank you, interesting solution, but I still prefer @Tom's answer, because I consider that completion wrong for a bash script file.
    – enzotib
    Sep 5, 2011 at 16:37
2

I enter <<< as M-3< in the shell mode.

1

Type < C-q < <

1
  • It is shorter than my workaround, and perhaps useful for other situations too, but not exactly what I was looking for. Thank you anyway.
    – enzotib
    Sep 5, 2011 at 11:48
1

After taking a look at: http://web.mit.edu/dosathena/sandbox/emacs-19.28/lisp/sh-script.el I came up with this solution:

;; disable the automatic EOF generation in Shell Mode
(defvar sh-use-prefix nil
  "If non-nil when loading, `$' and `<' will be  C-c $  and  C-c < .")
(defvar sh-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (if sh-use-prefix "\C-c<" "<")
      (local-set-key "<" 'self-insert-command))
   map)
  "Keymap used in Shell-Script mode.")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .