how to change the current working directory of emacs?
set default-directory
doesn't not affect the emacs process's current working directory, which can be verified via lsof -p pid
.
I think the only way Emacs offers to control the cwd
of the process is the --chdir
command line argument. Once the process is started, there is currently no way to modify it.
M-x cd
command does change the current working directory. It even makes find-file
to iniially suggest the given directory to look for files in.
Commented
Dec 20, 2021 at 21:48
M-x cd
does not change the working directory of the Emacs process (which can be seen in /proc/<PID>/cwd
under Linux, for example), it only changes the directory used by ELisp code (and subprocesses it may spawn).
M-x cd
This should solve your problem.
cd
will "Make DIR become the current buffer's default directory." I would presume that it's a per-buffer value and wouldn't apply to other existing buffers or to newly created ones.
C-h f cd
:
cd
is an interactive compiled Lisp function infiles.el
.
(cd DIR)
Make
DIR
become the current buffer’s default directory.If your environment includes a
CDPATH
variable, try each one of that list of directories (separated by occurrences ofpath-separator
) when resolving a relative directory name. The path separator is colon in GNU and GNU-like systems.
And please explain why changing default-directory
does not also change the "current directory" for you: (setq default-directory "/my/favorite/dir")
.
Maybe not an answer to the question but I had a similar issue. The setup was emacsclient over tmux session and had to split the window horizontally and open a new tmux tab with the current Emacs' working directory. Here is a workaround:
init.el:
;;; Tmux integration (See tmux.conf for details).
(defun id/tmux-split-horisontal ()
"Split tmux pane horisontally."
(interactive)
(ignore-errors (call-process "tmux" nil nil nil "split-window" "-h" "-c"
default-directory)))
(global-set-key (kbd "C-q %") #'id/tmux-split-horisontal)
(defun id/tmux-new-tab ()
"Create new tmux tab."
(interactive)
(ignore-errors (call-process "tmux" nil nil nil "new-window" "-c"
default-directory)))
(global-set-key (kbd "C-q c") #'id/tmux-new-tab)
tmux.conf:
## Use C-q as a prefix key.
unbind C-b
set -g prefix C-q
bind C-q send-prefix
## Handle [C-q %] and [C-q c].
is_emacs='echo "#{pane_current_command}" | grep -iqE "emacs"'
# Let Emacs handle [C-q %] if active.
bind-key -T prefix % if-shell "$is_emacs" "send-prefix ; send-keys %" "split-window -h -c \"#{pane_current_path}\""
# Let Emacs handle [C-q c] if active.
bind-key -T prefix c if-shell "$is_emacs" "send-prefix ; send-keys c" "new-window -c \"#{pane_current_path}\""
In this way, if you're under emacs or emacsclient the control is passing to Emacs, thus Emacs sends tmux command with default-directory argument. Otherwise, tmux handles it directly, so the user doesn't see the difference.
M-x report-emacs-bug
and request it as a new feature.