I have cscope built-in in emacs.
When ever I change the code using emacs. The code change causes cscope to not behave the way I want it to.
eg.
Due to code change If I want to jump to the function definition. cscope does not take me to the definition of the func, instead it takes me to some other line.
Please tell if there is a way to rebuild cscope without closing the emacs window.
You will need https://github.com/dkogan/xcscope.el
and configuration :
(defun my-c-mode-common-hook ()
(require 'xcscope)
(cscope-setup)
(setq cscope-initial-directory "path to the cscope directory"))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
and then
C-c s L (or M-x cscope-create-list-of-files-to-index)
C-c s I (or M-x cscope-index-files) => build or rebuild
Hope this help
I use the following function to build/rebuild cscope database:
(require 'xcscope)
(cscope-setup)
(setq cscope-option-use-inverted-index t)
(defadvice cscope-bury-buffer (after cscope-bury-buffer activate)
"Kill the *cscope* window after hitting q or Q instead of leaving it open."
(delete-window))
(defun cscope-create-database (top-directory)
"Create cscope* files in one step containing, do this before using cscope:
1. C-c s L
2. C-c s I
3. C-c s a
"
(interactive "DCreate cscope* database files in directory: ")
(progn
(cscope-create-list-of-files-to-index top-directory)
(cscope-index-files top-directory)
(setq cscope-initial-directory top-directory)
(sit-for 2)
(delete-windows-on "*cscope-indexing-buffer*")
(kill-buffer "*cscope-indexing-buffer*")
))
(bind-keys*
("C-c s r" . cscope-create-database))
Related
I'm currently using the terminal Terminator,
and i got a bash function
set_title() { printf '\e]2;%s\a' "$*"; }
wich permit me to set the terminator window title
So I would want to know if it's possible to execute this specific shell command (like this):
set_title ##filename
on each opening (or re-opening) of the said file in emacs?
(btw english is not my native language, please be indulgent!)
If you mean that you're running the non-GUI Emacs inside bash inside Terminator and you want Terminator's title to reflect the current file in Emacs, then the fact that bash sits in between is of no use to you: Emacs is in control here.
But you can define an Elisp function that will do the same job as your set_title:
(defun my-set-title (title)
(send-string-to-terminal (format "\e]2;%s\a" title)))
And then you could use it via find-file-hook:
(add-hook 'find-file-hook (lambda () (my-set-title buffer-file-name)))
Note that this will set the terminal's title to the last file Emacs visits, so if you switch back to a previous file-buffer via C-x C-b, the title won't be updated to reflect the current buffer's file name. If you want to do that, you'd need something more like:
(defvar my-last-file nil)
(defun my-keep-file-title ()
(when (and buffer-file-name
(not (equal buffer-file-name my-last-file)))
(setq my-last-file buffer-file-name)
(my-set-title buffer-file-name)))
(add-hook 'post-command-hook #'my-keep-file-title)
As suggested by #Dan, you can do
(add-hook find-file-hook
(lambda ()
(when (string= buffer-file-name "my-file")
(shell-command "printf ...."))))
to call printf when you open "my-file".
However, if what you want is to set the frame title (emacs calls "frame" what window managers call "window"),
you should be setting frame-title-format, e.g.:
(setq frame-title-format
'(buffer-file-name "%b - %f" ; File buffer
(dired-directory dired-directory ; Dired buffer
(revert-buffer-function "%b" ; Buffer Menu
("%b - Dir: " default-directory)))) ; Plain buffer
icon-title-format "%b")
In compilation, occur search, grep search, etc., Emacs will create a new buffer in a separate window to show the results, is there anyway to jump to that window automatically? It is useful because then I can use n and p instead of M-g n and M-g p to move to next and previous items; plus, the buffer can be quit quickly with q. Currently I manually switch to the new buffer every time I run those commands, which is just annoying. Thanks.
You can use advice.
For example to jump to the grep buffer use the following advice:
(defadvice compilation-start (after compilation-start-maximize activate)
(when (equal mode 'grep-mode)
(switch-to-buffer "*grep*")
;; you may want to maximize the buffer
(delete-other-windows)))
For occur you can use the following advice
(defadvice occur-1 (after occur-maximize activate)
(other-window 1))
After some search, I came up with this solution without advising the built-in functions:
(add-hook 'occur-hook (lambda () (pop-to-buffer occur-buf)))
(add-hook 'grep-mode-hook (lambda () (pop-to-buffer (get-buffer "*grep*"))))
(setq help-window-select t)
(add-hook 'compilation-mode-hook (lambda () (pop-to-buffer (get-buffer "*compilation*"))))
It works for *Occur*, *grep*, *compilation* and *Help*.
In Emacs, I often find myself in a situation where I need to jump back and forth between various source files to various terminals. However, I feel like I do not have a good way to do this efficiently and it's clumsy that you can only open one shell in Emacs (shell, eshell, or term).
Moreover, I need an efficient way of juggle between multiple terminals and source files.
How can I achieve this?
You can have as many terminals and shells open at once as you want. Just use M-x rename-buffer to change the name of an existing *term* or *shell* buffer, and the next time you do M-x term or M-x shell, a brand new buffer will be created. In the case of M-x shell, a prefix argument will cause you to be prompted for the name of the new shell buffer, as offby1 noted.
A few years ago I had a job where I had to regularly log in to various production servers named "host01.foo.com", "host02.foo.com", etc. I wrote a little function like this one to make it easier to manage them all:
(defun ssh-to-host (num)
(interactive "P")
(let* ((buffer-name (format "*host%02d*" num))
(buffer (get-buffer buffer-name)))
(if buffer
(switch-to-buffer buffer)
(term "/bin/bash")
(term-send-string
(get-buffer-process (rename-buffer buffer-name))
(format "ssh host%02d.foo.com\r" num)))))
Then I bound this command to (say) s-h (super H), enabling me to just type M-5 s-h. If I didn't already have a buffer named *host05*, it would start a new terminal emulator buffer, rename it to *host05*, and ssh me into host05.foo.com. If buffer *host05* already existed, it would simply switch me to it. Quite handy!
You can certainly have multiple interactive shells open. Try typing C-u M-x shell RET RET.
Try using MultiTerm to open multiple shells.
You can use Emacs Lisp Screen, which emulates GNU Screen and provides easy key bindings to jump to and between a number of different shells.
I use many methods for incorporating my terminal life into Emacs:
elscreen.el is a life saver, if you have a complicated window layout like gdb or have simply become overwhelmed with clutter you just open a new screen. In your case you could dedicate one screen to terminals.
multi-term.el makes managing terminals a bit easier.
shell-pop.el, a great tool for quick terminal access. shell-pop lets you assign a key to opening and closing a specific shell buffer window, if you've used drop-down terminals like tilda you know how incredibly handy this can be:
Here's and example of my shell-pop configuration, I use the key C-t to pop up an eshell:
(require 'shell-pop)
(shell-pop-set-internal-mode "eshell") ; Or "ansi-term" if you prefer
(shell-pop-set-window-height 60) ; Give shell buffer 60% of window
;; If you use "ansi-term" and want to use C-t
;; (defvar ansi-term-after-hook nil)
;; (add-hook 'ansi-term-after-hook
;; '(lambda ()
;; (define-key term-raw-map (kbd "C-t") 'shell-pop)))
;; (defadvice ansi-term (after ansi-term-after-advice (org))
;; (run-hooks 'ansi-term-after-hook))
;; (ad-activate 'ansi-term)
(global-set-key (kbd "C-t") 'shell-pop)
I usually do an an M-x server-start and then use emacsclient --no-wait to open files. I've aliased that to e with some embellishments so that it's a little more convenient.
I do all my work in a single terminal and just "throw" the files I want to edit into Emacs using e. Inside Emacs, I juggle around using iswitchb and it works just fine. YMMV.
I regularly used 10 or so shells in my old workplace. The secret is you have to rename additional shell buffers. I did this automatically though in my .emacs, creating and naming the shells logically (I had projnameRun and projnameBuild for every project). Worked really well together with anything, making it very easy to refind the right shell (you use the end of the project name combined with either r or b for run/build).
Instead of having several terminal windows in emacs, I spawn a different xterm whenever I need a new terminal. This of course is bearable because I use a very lightweight terminal emulator (urxvt) which starts in under 0.2s.
Then I use my window manager to switch between them and emacs frames. A configurable window manager will have plenty of options to tune to switch between windows (extremely) efficiently. Inside emacs, I use windmove and ido-mode, and have bound to C-tab a function that switches to the last buffer (because I use C-x b in that fashion a lot).
So um, not sure how useful it is to you since it's quite different from your use pattern, but this is what works for me.
I had exactly the same problem some years ago, and found nothing that satisfied me; so I wrote my own "toggle shell" function. It toggles between the current frame or window configuration and a system shell buffer. It can also put the shell into a dedicated frame, and inject a pushd to the current buffer directory.
This is an excerpt from my .emacs:
(defvar --toggle-shell-last-window-conf nil "The last window configuration.")
(defvar --toggle-shell-last-buf nil "The last buffer object in case there's no last window configuration.")
(defvar --toggle-shell-last-frame nil "The frame that was selected when opening a shell buffer.")
(defun --toggle-shell-have-conf ()
(window-configuration-p --toggle-shell-last-window-conf))
(defun --toggle-shell-store-last-conf ()
(setq --toggle-shell-last-buf (current-buffer)
--toggle-shell-last-frame (selected-frame)
--toggle-shell-last-window-conf (current-window-configuration)))
(defun --toggle-shell-restore-last-conf ()
(if (--toggle-shell-have-conf)
(progn (raise-frame --toggle-shell-last-frame)
(set-window-configuration --toggle-shell-last-window-conf))
(let ((bufnam (if (bufferp --toggle-shell-last-buf)
(buffer-name --toggle-shell-last-buf) --toggle-shell-last-buf)))
(if bufnam
(if (get-buffer bufnam) (switch-to-buffer bufnam t)
(message "%s: buffer not available" bufnam))))))
(defun --toggle-shell (&optional display inject-cd)
"Toggles between current buffers and a system shell buffer. With prefix-arg
close the shell.
When DISPLAY is 'vertical splits the shell as vertical window; when 'frame uses
a dedicated frame (default: single window). When INJECT-CD executes a `pushd'
to the working directory of the buffer from which you toggled the shell."
(interactive)
(let* ((shell-buf (get-buffer "*shell*"))
(shell-window ; non-nil when currently displayed
(if shell-buf (get-buffer-window shell-buf t)))
(shell-frame
(if shell-window (window-frame shell-window)))
(in-shell (eq (current-buffer) shell-buf))
(vertical (string= display 'vertical))
(popup-frame (or (string= display 'frame)
(and inject-cd (not (bufferp shell-buf)))
(and (framep shell-frame)
(not (eq shell-frame (selected-frame)))))))
;; With prefix-arg close shell, restore windows. Otherwise (no prefix-arg)
;; toggle shell window; restore windows when called twice in a row, or the
;; current buffer is the shell buffer (`in-shell').
(if current-prefix-arg
(if (bufferp shell-buf)
(progn (message "Exiting shell '%s'" (buffer-name shell-buf))
(kill-buffer shell-buf)
(if in-shell (--toggle-shell-restore-last-conf)))
(error "No shell buffer to kill."))
;; If already in shell-buffer toggle back to stored frame-configuration.
(if (and in-shell (not inject-cd))
(progn
(--toggle-shell-restore-last-conf)
;; Recurse to reopen the shell-buffer in a dedicated frame, or
;; close the dedicated frame and reopen the buffer in a window.
(if (and popup-frame (eq shell-frame (selected-frame)))
(--toggle-shell 'frame inject-cd)
(when (and popup-frame shell-frame)
(delete-frame shell-frame)
(--toggle-shell nil inject-cd))))
;; Not in shell buffer. Warp to it or create new one.
(unless in-shell
(--toggle-shell-store-last-conf))
(if popup-frame
(progn (switch-to-buffer-other-frame (or shell-buf "*shell*"))
(raise-frame
(or shell-frame (window-frame (get-buffer-window "*shell*" t)))))
(if (> (count-windows) 1)
(delete-other-windows)))
;; Finally `cd' into the working directory the current buffer.
(let ((new-shell (not (bufferp shell-buf)))
(new-dir ; `default-directory' of `--toggle-shell-last-buf'
(if --toggle-shell-last-buf
(buffer-local-value 'default-directory --toggle-shell-last-buf))))
;; Open shell, move point to end-of-buffer. The new shell-buffer's
;; `default-directory' will be that of the buffer the shell was
;; launched from.
(when vertical
(if (> (count-windows) 1)
(delete-other-windows))
(split-window-vertically) (other-window 1))
(funcall 'shell)
(when new-shell
(message "New shell %s (%s)" (buffer-name (current-buffer)) new-dir)
(if inject-cd (sit-for 2))) ; wait for prompt
(goto-char (point-max))
;; If on a command-prompt insert and launch a "cd" command (assume no
;; job is running).
(when (and inject-cd new-dir)
(save-excursion
(backward-line-nomark) (end-of-line)
(unless (setq inject-cd (re-search-forward comint-prompt-regexp (point-max) t))
(error "Cannot `pushd', shell is busy")))
(when (and inject-cd)
(let* ((cmd (format
"pushd '%s' %s" (comint-quote-filename new-dir)
(if (buffer-file-name --toggle-shell-last-buf)
(format "# '%s'" (file-name-directory (buffer-file-name --toggle-shell-last-buf)))
""))))
;; `shell-process-cd' set new `default-directory' and set
;; `shell-last-dir' to old. (If the pushd command is
;; successful, a dirs is performed as well; >nul discards this
;; output.)
(shell-process-cd new-dir)
(insert cmd)
(comint-send-input)
(message "%s: cd '%s'" (buffer-name --toggle-shell-last-buf) new-dir))
)
)
)
)
)
)
)
--toggle-shell is the function that does the trick. I bind it to F12:
;; F12 toggle between shell buffer and current window configuration
;; SHIFT-F12 like before, but let shell buffer appear in a dedicated frame
;; ALT-F12 inject a pushd to change to directory of current buffer
;; CTRL-F12 `shell-command'
(global-set-key [(f12)] '--toggle-shell)
(global-set-key [(shift f12)] '(lambda()(interactive)(--toggle-shell 'frame)))
(global-set-key [(meta f12)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(meta f10)] '(lambda()(interactive)(--toggle-shell nil t)))
(global-set-key [(control f12)] 'shell-command) ; alias M-!
This is a significant bunch of code to be posted here. But it shall work well.
Semi related - you can quickly run a shell command on selected file with
M+shift+!
It saves a lot of time for smaller commands chmod etc
And maybe my quick pop-up shell also might help you. A quick pop-up shell for emacs
Ecb + eshell will be what you want exactly!
I use vi, but hope this helps. I can open as many terminals as I want by (eg. in Ubuntu 16.04):
ctrl + alt + t
I usually open 2 terminals, and move (position) one terminal to the right by:
ctrl + super + right-arrow
and move the other terminal to the left by:
ctrl + super + left-arrow
so that I have a divided screen by 2 terminals.
Had this working well in Emacs 23.1.x but it appears to have broke in the move to Emacs 23.2
I want to use ediff when comparing working copy of a file with SVN HEAD.
Normally I press C-x v = and ediff runs because of the following configuration in my .emacs
;; Use ediff and not diff
(setq diff-command "ediff")
But, alas I still get the normal vc-diff buffer appearing and no ediff session...
Has anyone else encountered this and know what might be the problem?
Am a bit skeptical that the above setting did what you say it did.
That said, this will bind '=' to use 'ediff-revision:
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'ediff-revision))
I use the command vc-ediff to skip entering the file names: just compare the current modified copy with the base version (HEAD).
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'vc-ediff))
Then C-x v = will bring up the Ediff session.
Found out I could just rebind C-x v = to the following:
(defun ediff-current-buffer-revision ()
"Run Ediff to diff current buffer's file against VC depot.
Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
(interactive)
(let ((file (or (buffer-file-name)
(error "Current buffer is not visiting a file"))))
(if (and (buffer-modified-p)
(y-or-n-p (message "Buffer %s is modified. Save buffer? "
(buffer-name))))
(save-buffer (current-buffer)))
(ediff-load-version-control)
(funcall
(intern (format "ediff-%S-internal" ediff-version-control-package))
"" "" nil)))
This approach means you avoid having to specify the versions to compare as it defaults to comparing HEAD and the current file state.
Source: http://www.groupsrv.com/computers/about152826.html
I am getting more and more used to doing everything from inside emacs, but it seems that eshell, shell and term will only run one instance each. Is there a way to run multiple terminals (preferably term) inside emacs?
Use the command M-x rename-buffer to give the current shell buffer a new name, then you can start a new shell.
You just have to rename the buffer, here's a function to start zsh and prompt for the buffer name:
(defun zsh (buffer-name)
"Start a terminal and rename buffer."
(interactive "sbuffer name: ")
(term "/bin/zsh")
(rename-buffer buffer-name t))
You can always create a new shell with C-u M-x shell
http://www.emacswiki.org/emacs/MultiTerm
You can rename a term and start a new one. I'm using something like that, took it from someone else .emacs.
(require 'term)
(defun visit-ansi-term ()
"If the current buffer is:
1) a running ansi-term named *ansi-term*, rename it.
2) a stopped ansi-term, kill it and create a new one.
3) a non ansi-term, go to an already running ansi-term
or start a new one while killing a defunt one"
(interactive)
(let ((is-term (string= "term-mode" major-mode))
(is-running (term-check-proc (buffer-name)))
(term-cmd "/bin/bash")
(anon-term (get-buffer "*ansi-term*")))
(if is-term
(if is-running
(if (string= "*ansi-term*" (buffer-name))
(call-interactively 'rename-buffer)
(if anon-term
(switch-to-buffer "*ansi-term*")
(ansi-term term-cmd)))
(kill-buffer (buffer-name))
(ansi-term term-cmd))
(if anon-term
(if (term-check-proc "*ansi-term*")
(switch-to-buffer "*ansi-term*")
(kill-buffer "*ansi-term*")
(ansi-term term-cmd))
(ansi-term term-cmd)))))
Or you can have just one and start a screen session in it.
I personally use a screen-like package I wrote, and there's another version available on the wiki here: elscreen. It provides convenient key bindings to jump to/between the different shells.
I modified the accepted answer by Harpo so that it starts a new shell without prompting, shells will be named in the form *shell-1*,*shell-2*,*shell-3* etc.:
(setq bash-counter 1)
(defun bash ()
"Start a bash shell"
(interactive)
(setq bash-counter (+ bash-counter 1))
(let
((explicit-shell-file-name "/bin/bash"))
(shell (concat "*shell-" (number-to-string bash-counter) "*"))
))
Here's a super lightweight little function that you can call to automatically rename the term you're on, and then start a new term:
(defun new-ansi-term ()
(interactive)
(if (string= "*ansi-term*" (buffer-name))
(rename-uniquely))
(ansi-term "/bin/bash"))
Then to bind that within ansi-term, I found this works:
(defvar ansi-term-after-hook nil)
(add-hook 'ansi-term-after-hook
'(lambda ()
(define-key term-raw-map (kbd "C-t") 'new-ansi-term)))
(defadvice ansi-term (after ansi-term-after-advice (org))
(run-hooks 'ansi-term-after-hook))
(ad-activate 'ansi-term)
If you then also bind new-ansi-term to C-t in the normal way, you'll find that when you're not looking at an ansi-term, C-t will focus the ansi-term buffer, and then if you are looking at an ansi-term, C-t will rename it to some unique name, and then open a new ansi-term for you. This works really well in combination with tabbar, which will show you all your opened ansi-terms just above the first line of the buffer. Easy to switch between them ;-)