Instant Access to Your Shell Init File
A while ago I showed you a way to instantly edit your Emacs init
file. Today
we’ll adapt the original idea for shell init files like .bashrc
and
.zshrc
. The code required is fairly short and simple:
(defun er-find-shell-init-file ()
"Edit the shell init file in another window."
(interactive)
(let* ((shell (car (reverse (split-string (getenv "SHELL") "/"))))
(shell-init-file (cond
((string-equal "zsh" shell) ".zshrc")
((string-equal "bash" shell) ".bashrc")
(t (error "Unknown shell")))))
(find-file-other-window (expand-file-name shell-init-file (getenv "HOME")))))
The shell init file is deduced from your SHELL
env variable. While
there are different shell init files for most shell
(e.g. .bash_profile
, .zshenv
, .zprofile
), here we’re assuming
you’re using the most commonly used files. find-file-other-window
will open the file in a window adjacent to the one you’re currently
in.
(global-set-key (kbd "C-c S") #'er-find-shell-init-file)
This command is available in crux as
crux-find-shell-init-file
. This command is also available in
prelude via the crux package.