emacs-tangents
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Some nice shell mode commands


From: nitish chandra
Subject: Some nice shell mode commands
Date: Fri, 5 Feb 2016 12:37:44 +0530

I wanted to share some of my most used shell mode commands.

1. Long output commands in shell mode:

Usually in shell mode, it is difficult to use commands that produce a lot of output. I used to run the command as a shell command and view the output in a buffer. Now, I use these commands



       (local-set-key (kbd "M-,") (lambda ()
                                                          (interactive)
                                                          (progn
                                                            (previous-line)
                                                            (move-end-of-line 1)
                                                            (re-search-backward "^.*\\$ ")
                                                            (re-search-forward "\\$[ \\t]*"))))
       (local-set-key (kbd "M-.") (lambda ()
                                                          (interactive)
                                                          (re-search-forward "^.*\\$ ")))

With `M-,', the point moves to the previous prompt and with `M-.', point moves to the next prompt. With these, one can run the command normally in shell and then view the long output with `M-,'. After analysing the output, one can use `M->' as usual to get to the prompt.

I like this approach because now I can use all emacs commands to search or traverse the output without creating a new buffer.


2. Equivalent of bash `C-r' in shell mode:

(defun my-comint-previous-input (original-comint-function arg)
  "Cycle backwards through input history, saving input."
  (interactive "*p")
  (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
     (input (buffer-substring pmark (point)))
     (current-position (point)))
    ;; (comint-previous-matching-input (concat "^" input) 1))
    (if (and comint-input-ring-index
         (or               ;; leaving the "end" of the ring
          (and (< arg 0)        ; going down
           (eq comint-input-ring-index 0))
          (and (> arg 0)        ; going up
           (eq comint-input-ring-index
               (1- (ring-length comint-input-ring)))))
         comint-stored-incomplete-input)
    (comint-restore-input)
      (comint-previous-matching-input (concat "^" input) arg)
      (goto-char current-position))))

(advice-add 'comint-previous-input :around #'my-comint-previous-input)


What this does is, when you press `M-p' (key for `comint-previous-input'), it takes the currently typed part of command after prompt,  searches for the commands in input history that start with what you have typed and puts them after the point. For example, there are two commands `sshfs <user>...' and `sudo mount ...'. To get to the first command, I type `ss' and press `M-p'. If I change my mind, I can then use `<Backspace>u' to change `ss' to `su' and then type `M-p' to get to the sudo command.

Please suggest if there are cleaner ways to do these things.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]