My .emacs.d configurations
- Use
moe-theme
for the color scheme
See the section below
Maximize the use of ⌘ (super) key in Emacs according to personal behavior.
On top of out-of-box GNU Emacs.app in macOS, we use a server-client model for running GUI / terminal text editing. The following behavior is desired according to personal preferences:
- The emacs server is alive within an active session of
Emacs.app
, i.e. the Emacs icon is shown in the dashboard. - Terminal editing of a file is done by a wrapped version of
emacsclient
. If the emacs server is not alive, activate the GUIEmacs.app
- No
launchctl
process should be involved as the user could not have elevated privilege.
To do this, we require both settings on server and client sides:
- When opening
Emacs.app
, theinit.el
script will automatically enable emacs server using(server-start)
. - The window close button only hides the application but not quitting it, making further client sessions doable. (Solution from: https://lists.gnu.org/archive/html/help-gnu-emacs/2016-01/msg00236.html)
- REAL quitting is done by
(save-buffers-kill-emacs)
or ⌘+Q
On the client side, the desired behavior is run the text editor by simply as:
ec FILE_NAME1 FILE_NAME2 ...
where ec
is a function wrapper of emacsclient
that automatically determines if the server on Emacs.app
has started.
The following snippet should be loaded by the .bash_profile
or any
related shell init scripts:
function ec_wrapper {
GREEN='\033[0;32m'
NC='\033[0m'
buffers=$@
cmd="emacsclient -nqc"
# Excecute command
$cmd $buffers &> /dev/null
return_code=$?
if (( $return_code != 0))
then
pid=$(pgrep -ai "emacs")
# If emacs current running?
if (( ${#pid} == 0 )) # No pid returned
then # No emacs running, then open the GUI
emacs --chdir $(pwd) $@ & &> /dev/null
suc=$?
return $suc
else
echo "There is a running Emacs instance but I can't find find the server."
echo -e "Run ${GREEN}M-x server-start${NC} in Emacs to enable it."
fi
else
return 0 # Success
fi
}
alias ec=ec_wrapper
s-w
: delete current window (no kill buffer). Equivalent toC-x 0
s-<left>
: Move to the beginning of lines-<right>
: Move to the end of lines-<up>
: Move to beginning of buffer. Equivalent to<home>
(Fn + Left on Mac keyboard)s-<down>
: Move to end of buffer. Equivalent to<end>
(Fn + Right on Mac keyboard)M-s-<up>
: Move to upper windowM-s-<down>
: Move to lower windowM-s-<left>
: Move to left windowM-s-<right>
: Move to right window
The original undo/ redo functionalities of Emacs are fancy but also
complicated when implementing redo. We use undo-tree
mode to manage
all the undo / redo history in Emacs.
The following key bindings are used by convention:
s-z
: undo usingundo-tree-undo
s-Z
: redo usingundo-tree-red
Note in the undo-tree
mode, the key C-x u
is binded to visualize
the undo tree. I personally don’t prefer this approach and set C-x u
to the usual undo
binding defined by Emacs.
The default behavior of “kill” in emacs is a fancy feature but not so appealing to users like me (coming from background of Sublime, XCode etc.) The annoying part of the default behavior comes for the following scenario:
- Copy some text from another buffer to the kill-ring
- Go to the buffer that you want to edit, delete the regions with
keys like
C-<backspace>
,s-<backspace>
etc. - Now the last kill-ring becomes the text you deleted (killed) during step 2, and you have to go explicitly to the kill-ring to find out what needs to be pasted
This default behavior is avoided by explicitly invoking the delete
functions (see settings/editor.el
for details). In brief, the following key-bindings will not save the deleted region to kill-ring:
s-<backspace>
: delete all contents of the line before the position. Will not greedily delete when invoking multiple times.
s-<kp-delete>
: delete all contents of the line after the position. No greedy deletion.C-<backspace>
,M-<backspace>
: backward delete a word or subword. Whensyntax-subword-mode
is present, use thesyntax-subword-forward
method to locate subword; otherwise the normalsubword-forward
.C-<kp-delete>
,M-<kp-delete>
: backward delete a word or subword.
Use convention from other text editors.
s-l
: select current lines-/
: comment current line / region, enabled with Emacs version
>25.0
The usual key-binding M-;
(comment-dwim
) is used when you want to
append some comment at the end of a line.