Emacs Redux

Return to the Essence of Text Editing

Instant Access to Your Shell Init File

| Comments

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:

1
2
3
4
5
6
7
8
9
(defun 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.

1
(global-set-key (kbd "C-c S") 'find-shell-init-file)

find-shell-init-file is available in Prelude(but with a prelude- prefix).

Comments