about summary refs log tree commit diff
path: root/init/term-setup.el
blob: e8c74387150c6b5a0db3e39907ea22d255188395 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
;; Configuration for multi-term mode:

(require 'multi-term)

(defun open-or-create-term-buffer (buffer-name)
  "Switch to the buffer with BUFFER-NAME or create a
  new (multi-)term-mode buffer."
  (let ((buffer (get-buffer buffer-name)))
    (if (not buffer)
        (multi-term)
      (switch-to-buffer buffer))))

(defun counsel-switch-to-term ()
  "Switch to a (multi-)term buffer or create one."
  (interactive)
  (let ((terms (counsel-list-buffers-with-mode 'term-mode)))
    (if terms
        (ivy-read "Switch to term buffer: "
                  (cons "New terminal" terms)
                  :caller 'counsel-switch-to-term
                  :require-match t
                  :action #'open-or-create-term-buffer)
      (multi-term))))

(defun term-rename ()
  "Rename the current terminal buffer."
  (interactive)
  (let* ((buffer (get-buffer (buffer-name)))
         (mode (buffer-local-value 'major-mode buffer)))
    (if (equal 'term-mode mode)
        (rename-buffer (format "*terminal<%s>*" (read-string "New terminal name: ")))
      (error "This function is only intended to rename terminal buffers."))))

(global-set-key (kbd "C-x t") #'counsel-switch-to-term)

;; term-mode's attempt to use isearch is not my favourite thing in the
;; world.
(delete '("C-r" . isearch-backward) term-bind-key-alist)
(delete '("C-s" . isearch-forward) term-bind-key-alist)

(add-to-list 'term-bind-key-alist '("C-r" . term-send-reverse-search-history))
(add-to-list 'term-bind-key-alist '("C-c C-l" . term-line-mode))
(add-to-list 'term-bind-key-alist '("C-s" . swiper))
(add-to-list 'term-bind-key-alist '("C-c C-r" . term-rename))

(provide 'term-setup)