In emacs you can do M-x cd to change the default directory.
I usually have 5 splits/windows, so the cd I do in the first split does't affect the others. What If I want the cd to affect all my splits/buffers.
Is there an alternative command I can use?
There's nothing built-in but it's not too hard to write the function by hand:
(defun cd-all-windows (dir)
(interactive "Ddirectory: ")
(dolist (window (window-list))
(with-current-buffer (window-buffer window)
(cd dir))))
Put that in your .emacs and you should be able to run M-x cd-all-windows to get the desired effect.
Related
When I start working on my project, I need to run lots of processes. Currently, I have to launch each process in a manual way:
M-x multi-term RET
M-x rename-buffer RET *some-name* RET
cd ~/foo/bar/
python ./task.py
How to write a code on Emacs lisp that does the following steps:
opens new multi-term buffer;
renames it (I know the title I'd like to hardcode it for each process);
runs one or two commands inside terminal
You can start from this template:
(defun python-erica ()
(interactive)
(let* ((default-directory "~/")
(proc (get-buffer-process
(ansi-term "/usr/bin/ipython" "erica"))))
(term-send-string
proc
(concat "import sys\n"))))
You can change:
function name
default dir
python executable
term buffer name
initial commands
Using dired in emacs, i would to open (ie; a .png) any file with a list of viewers (selectable by typing) as 'open-with' way...
How can i do that?
Thank you,
Steve,
You should use & to run the command in async: !
will freeze Emacs while the command is running.
Customize dired-guess-shell-alist-user as a guess list for common extensions:
(setq dired-guess-shell-alist-user
'(("\\.pdf\\'" "evince" "okular")
("\\.eps\\'" "evince")
("\\.jpe?g\\'" "eog")
("\\.png\\'" "eog")
("\\.gif\\'" "eog")
("\\.xpm\\'" "eog")
("\\.csv\\'" "libreoffice")
("\\.tex\\'" "pdflatex" "latex")
("\\.\\(?:mp4\\|mkv\\|avi\\|flv\\|ogv\\)\\'" "vlc")
("\\.\\(?:mp3\\|flac\\)\\'" "rhythmbox")
("\\.html?\\'" "firefox")
("\\.cue?\\'" "audacious")))
The first item on the list will be the default choice, e.g. evince
over okular. You can navigate to the other choices with
M-n/M-p.
If you're on Linux, you can try the command that I'm using for this task:
(defvar dired-filelist-cmd
'(("vlc" "-L")))
(defun dired-start-process (cmd &optional file-list)
(interactive
(let ((files (dired-get-marked-files t current-prefix-arg)))
(list
(dired-read-shell-command "& on %s: " current-prefix-arg files)
files)))
(apply
#'start-process
(list cmd nil shell-file-name shell-command-switch
(format "nohup 1>/dev/null 2>/dev/null %s \"%s\""
(if (> (length file-list) 1)
(format "%s %s"
cmd
(cadr (assoc cmd dired-filelist-cmd)))
cmd)
(mapconcat #'expand-file-name file-list "\" \"")))))
It's better than dired-do-async-shell-command that's bound to
&, because the opened files will persist even if you close
Emacs that opened them. I tend to close Emacs more than usual because
often I'm testing stuff and it's faster to restart than to reset to
the default state.
If you can do without a list of viewers, you can hit ! while point is over a file in dired, and you can type the name of a command (with tab completion if your Emacs is new enough). dired will run that command, with the name of the file added at the end.
If the file name shouldn't be at the end of the command, add * wherever it should be, and dired will put it there instead.
For example, hitting ! over foo.png and typing just bar will run bar foo.png, while typing bar * --baz will run bar foo.png --baz.
For most GNU/linux desktops you can use the mediator package which uses the Freedesktop mime-type specifications to automatically present a list of suitable programs for opening a file of some specific extension.
If I use konsole or other terminal, the terminal tag name can change based on PWD. But in multi-term, the buffer name is *terminal<number>*. This is not very nice. Because when I switch between them, the name is not very informative. So I want to rename it based on PWD.
I find that the Enter key is bind to term-send-raw, so I write a function
(defadvice term-send-raw (around rename-term-name activate)
(progn
(rename-buffer
(concat "⇒ "
(shell-command-to-string "pwd | xargs basename | tr -d '\n'")
(format-time-string " [%M ∞ %S]")))
ad-do-it))
But the problem is pwd command return the PWD of the terminal buffer, while it is not the PWD of the SHELL in that terminal.
The PWD of the terminal buffer is set by defcustom multi-term-default-dir. And it does not change when the PWD change in the SHELL.
(defcustom multi-term-default-dir "~/"
"The default directory for terms if current directory doesn't exist."
:type 'string
:group 'multi-term)
How can I get the PWD of the SHELL in the terminal?
Regards.
AFAIK there is no easy way to retrieve information from a running process.
But if you want to get the current directory you could:
ask the shell to print it
parse and trace the command-line for functions like cd, pushd, popd…
poll /proc/PID/cwd
The first method is described in the header of term.el (M-xfind-libraryRETtermRET).
And now, thank you for your question, you gave me the opportunity to do this:
(defadvice term-send-input (after update-current-directory)
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (file-truename (format "/proc/%d/cwd" pid))))
(cd cwd)))
(ad-activate 'term-send-input)
It's a naive implementation of the third method and it doesn't work if the user uses su or ssh. However, I don't know if it's possible withouth using the first or the second method.
In your case, you can just replace the cd command with whatever you want.
Building off of Daimrod's answer for polling /proc/PID/cwd, I found a way get around the problem that Reed pointed out where the advice doesn't pick up the updated CWD immediately and you have to hit Enter twice.
If you move the CWD update code to its own function and use run-at-time to call it from the advice at a later time, it will pick up the updated CWD correctly. Unfortunately I don't know enough about Emacs' scheduling to explain why this works (any enlightenment would be appreciated).
Here's my code based on Daimrod's. Note I advised term-send-input for line-mode and term-send-return for char-mode. I tested this using multi-term on Emacs 24.3.1:
(defadvice term-send-input (after update-current-directory)
(run-at-time "0.1 sec" nil 'term-update-dir)
)
(ad-activate 'term-send-input)
(defadvice term-send-return (after update-current-directory)
(run-at-time "0.1 sec" nil 'term-update-dir)
)
(ad-activate 'term-send-return)
(defun term-update-dir ()
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (file-truename (format "/proc/%d/cwd" pid))))
(unless (equal (file-name-as-directory cwd) default-directory)
(message (concat "Switching dir to " cwd))
(cd cwd)))
)
Most terminals get their window name from the command echo -en. In zsh you can put this in your ~/.zshenv
precmd() { echo -en "\e]0;`basename ${PWD}`\a" }
and that will get the basename of your PWD environment variable. Ideally multi-term would do something similar and put it in multi-term-buffer-name, which is the variable which holds its buffer name.
Yes, this is not a complete solution. I'm hoping for one too!
Try this:
(defun open-or-jump-to-multi-term ()
(interactive)
(if (string-prefix-p "*terminal<" (buffer-name))
(delete-window)
(progn
(setq bufname (concat "*terminal<" (directory-file-name (file-name-directory (buffer-file-name))) ">"))
(if (get-buffer-process bufname)
(switch-to-buffer-other-window bufname)
(progn
(split-window-right)
(other-window 1)
(multi-term)
(rename-buffer bufname)
)
)))
)
(global-set-key (kbd "C-`") 'open-or-jump-to-multi-term)
This answer gave me the solution I needed. The only problem for me, is that I have to load it, namely fic-mode, manually. More explicitly, whenever I open a c++ file, I have to do M-x fic-mode and then M-x font-lock-fontify-buffer in order to have it really up and running. In my .emacs I have
(require 'fic-mode)
(add-hook 'c++-mode-hook '(lambda () (fic-mode 1)))
but it doesn't do the trick.
Do you have any suggestions how to make it available automatically?
Try the following: create a new file containing the following three lines:
(setq load-path (cons "/path/to/fic-mode-directory" load-path))
(require 'fic-mode)
(add-hook 'c++-mode-hook 'turn-on-fic-mode)
Replace "/path/to/fic-mode-directory" with the absolute path to the directory in which you saved fic-mode.el.
Then from the command line, run
emacs -Q -l /path/to/file
where /path/to/file is the path to the above file.
Now type C-x C-f test.cpp.
Is fic-mode turned on in the resulting buffer?
I would like to see examples of how to setup perforce, using the config file functionality where emacs is used as the diff and merge programs (P4DIFF and P4MERGE settings). Even better if this is on Windows.
I'm also struggling with getting the P4EDITOR to work correctly when using emacsclientw, specifically specifying the alternate-editor functionality.
Any tips, suggestions, example configs are very welcome.
Here's a different trick I used to use. It adds a few command line options to emacs so that you can do diffs and merges in a new emacs instance (again using ediff).
;; -diff
(defun command-line-diff (switch)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff file1 file2)))
(add-to-list 'command-switch-alist '("-diff" . command-line-diff))
;; -merge
(defun command-line-merge (switch)
(let ((base (pop command-line-args-left))
(sccs (pop command-line-args-left))
(mine (pop command-line-args-left))
(merg (pop command-line-args-left)))
(ediff-merge-with-ancestor sccs mine base () merg)))
(add-to-list 'command-switch-alist '("-merge" . command-line-merge))
Just put that in your .emacs file. Then you can set your P4DIFF program to be emacs -diff and your P4MERGE program to be emacs -merge.
I'm assuming you're already using p4.el.
Here's a function that will allow you to set your p4-client-config easily:
(defun p4-go (config)
(interactive
(list (read-file-name "P4 Config file: "
(concat (getenv "HOME") "/etc/perforce/")
""
t)))
(p4-set-client-config (expand-file-name config))
t)
Then I just run M-x p4-go <RET> conf <RET>.
My ~/etc/perforce/conf file looks like:
P4CLIENT=ewarmenhoven-ppd
P4PORT=perforce.netflix.com:1666
P4USER=ewarmenhoven
P4EDITOR=emacsclient
P4DIFF=diff -dupU8
P4MERGE=~/bin/emerge
The emerge merge program is just a short little shell script that calls emacsclient appropriately:
#!/bin/bash
base=$1
sccs=$2
mine=$3
merg=$4
emacsclient -e "(ediff-merge-files-with-ancestor \"$base\" \"$sccs\" \"$mine\" () \"$merg\")"
emacsclient "$merg"
If you're using cygwin it should work just fine.
For doing diffs, if it's running from the shell then I want the output in the shell, hence just using normal diff. If it's not, I use p4-ediff, which is bound to C-x p - by default.
The awesome answer by Eric doesn't work properly in latest emacs because of welcome screen. In order to hide the welcome screen (so that you may get the diff properly) please refer Unable to hide welcome screen in Emacs.
Another nifty setting which opens the diff in regular vertical mode is setting the below config variable
(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(ediff-split-window-function (quote split-window-horizontally)))