summaryrefslogtreecommitdiff
path: root/lisp/init-eshell.el
blob: ec5066aaf84b35b134e24a7f4fde20c4eda87055 (about) (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(require 'eshell)
(require 'shell-switcher)
(setq shell-switcher-mode t)
(add-hook 'eshell-mode-hook 'shell-switcher-manually-register-shell)
(add-hook 'shell-mode-hook 'shell-switcher-manually-register-shell)
(setq shell-switcher-new-shell-function 'shell-switcher-make-shell)
(setq eshell-history-size 10000)

;; author: KaiGrossjohann on EmacsWiki
(defun eshell/ff (&rest args)
  "Invoke `find-file' on the file.
    \"ff +42 foo\" also goes to line 42 in the buffer."
  (while args
    (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
        (let* ((line (string-to-number (match-string 1 (pop args))))
               (file (pop args)))
          (find-file file)
          (goto-line line))
      (find-file (pop args)))))


;; convenience functions to input the remote root/home dir when in a
;; directory on a remote host
(defun my/tramp-root ()
   "Print root directory on the remote host."
   (interactive)
   (let ((pieces (split-string (eshell/pwd) ":/")))
     (insert-string (if (> (length pieces) 1)
                        (concat (car pieces) ":/")
                      "/"))))

(defun my/tramp-home ()
   "Print home directory path on the remote host."
   (interactive)
   (let ((pieces (split-string (eshell/pwd) ":/")))
     (insert-string (if (> (length pieces) 1)
                        (concat (car pieces) ":~/")
                      "~/"))))

;; use cat as the pager in shell mode, because shell-mode is not an
;; ANSI terminal
(setenv "PAGER" "cat")

;; C-d on an empty line in the shell terminates the process.
(defun my/comint-delchar-or-eof-or-kill-buffer (arg)
  (interactive "p")
  (if (null (get-buffer-process (current-buffer)))
      (kill-buffer)
    (comint-delchar-or-maybe-eof arg)))

(add-hook 'shell-mode-hook
          (lambda ()
            (define-key shell-mode-map
              (kbd "C-d") 'my/comint-delchar-or-eof-or-kill-buffer)
            (define-key shell-mode-map
              (kbd "<up>") 'comint-previous-matching-input-from-input)))

;; TODO: this isn't working
(add-hook 'term-mode-hook
          (lambda ()
            (define-key term-mode-map
              (kbd "C-d") 'my/comint-delchar-or-eof-or-kill-buffer)))

(add-hook 'eshell-mode-hook
          '(lambda ()
             (define-key eshell-mode-map (kbd "C-c /") 'my/tramp-root)
             (define-key eshell-mode-map (kbd "C-c ~") 'my/tramp-home)))