Emacs (Application)

From campisano.org
Jump to navigation Jump to search



  • from Debian-like O.S.:
# for terminal version
apt-get install emacs-nox
# or for X version
apt-get install emacs
# shared/opt install schema v1.5.1

#### as common user ####
# setup env vars
export SOFTWARE_PATH="/home/shared/opt/software"
export NAME="emacs"
export VERSION="27.1"
export URL="ftp://ftp.gnu.org/pub/gnu/emacs/emacs-${VERSION}.tar.gz"

#### as root ####
# generic dependencies
apt-get -qq -y install wget coreutils findutils
# dependencies for terminal version
apt-get -qq -y install tar gzip gcc make libtinfo-dev zlib1g-dev libssl-dev libjansson-dev
# or dependencies for X version
apt-get -qq -y install tar gzip gcc make libtinfo-dev zlib1g-dev libssl-dev libjansson-dev libtool texinfo build-essential xorg-dev libgtk-3-dev libjpeg-dev libncurses5-dev libdbus-1-dev libgif-dev libtiff-dev libm17n-dev libpng-dev librsvg2-dev libotf-dev libgnutls28-dev libxml2-dev libxpm-dev libnotify-dev
# paths
mkdir -m 777 -p "${SOFTWARE_PATH}/tmp_install/" "${SOFTWARE_PATH}/${NAME}_${VERSION}/"

#### as common user ####
umask 0027
cd "${SOFTWARE_PATH}/tmp_install"
wget -c --no-check-certificate "${URL}"
tar -xzf "emacs-${VERSION}.tar.gz"
cd "emacs-${VERSION}"
# define compiler flags optimizations
export CFLAGS="-g0 -O2 -mtune=native -pipe"
# configure for terminal version
./configure --prefix="${SOFTWARE_PATH}/${NAME}_${VERSION}" --without-x --with-x-toolkit=no --without-all --without-compress-install --with-file-notification=no --with-zlib --with-gnutls --with-modules --with-threads --with-json
# or configure for X version
./configure --prefix="${SOFTWARE_PATH}/${NAME}_${VERSION}" --with-x --with-x-toolkit=gtk3 --without-gsettings --without-gconf --without-dbus --without-makeinfo --without-compress-install --with-file-notification=inotify --with-zlib --with-gnutls --with-modules --with-threads --with-json
make -s
make install

#### as root ####
chown -R root:users "${NAME}_${VERSION}"
find "${NAME}_${VERSION}" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "${NAME}_${VERSION}" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
rm -rf tmp_install
ln -s -f -T "${NAME}_${VERSION}" "${NAME}"

#### as common user ####


Emacs as default git difftool

git config --global diff.tool emacs
git config --global difftool.prompt false
# the result of the following command is: cmd = emacs -nw --eval \"(progn (setq vc-handled-backends ()) (ediff-files \\\"$LOCAL\\\" \\\"$REMOTE\\\"))\"
git config --global difftool.emacs.cmd 'emacs -nw --eval "(progn (setq vc-handled-backends ()) (ediff-files \"$LOCAL\" \"$REMOTE\"))"'

Emacs as default git mergetool

git config --global merge.tool emacs
git config --global mergetool.prompt false
# the result of the following command is: cmd =  emacs -nw --eval \"(progn (setq vc-handled-backends ()) (ediff-merge-files-with-ancestor \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$BASE\\\" nil \\\"$MERGED\\\"))\"
git config --global mergetool.emacs.cmd 'emacs -nw --eval "(progn (setq vc-handled-backends ()) (ediff-merge-files-with-ancestor \"$LOCAL\" \"$REMOTE\" \"$BASE\" nil \"$MERGED\"))"'

Improve GDB print of STL containers

  • Install libstdc++6
sudo apt-get install libstdc++6
  • libstdc++6 provide a version of python prettyprinter like function

to be used with gdb, however if you got errors like

"Python Exception <type 'exceptions.ValueError'> Cannot find type"

you may want to install a more recent version:

# download
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python ~/gdb/python
# configure
cat >> ~/.gdbinit << EOF
import os
import sys
sys.path.insert(0, os.environ['HOME'] + '/gdb/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
  • Configure GDB print behaviour
cat >> ~/.gdbinit << EOF

# C++ related beautifiers (optional)
set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

C++ navigation and completation with language server

First install one of such language servers:

Then, config a language server package for emacs like eglot or lsp-mode, the follow is a config sample for lsp-mode:

(use-package lsp-mode
  :ensure t
  (add-hook 'prog-mode-hook #'lsp)

(use-package lsp-ui
  :ensure t
  (add-hook 'lsp-mode-hook #'lsp-ui-mode)

(use-package company
  :ensure t
(use-package company-lsp
  :ensure t
  (setq company-lsp-enable-recompletion t)
  (add-to-list 'company-backends 'company-lsp)

Java navigation and completation with Eclipse JDT Language Server

# dependencies
su -c "apt-get install curl tar gzip" # NOTE: jdk and maven are required too
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="d30acc4c7cf961a2234b19b5b9e8dfe3d7402987"      # Edit when needed
umask 0027
mkdir tmp
cd tmp
curl -C - -kLO "${URL}"
unzip "${VERSION}.zip"
cd "eclipse.jdt.ls-${VERSION}"
./mvnw clean verify
cd ../..
su -c "chown -R root:users tmp;\
 find tmp -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;;\
 find tmp -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;;\
 mv tmp/eclipse.jdt.ls-${VERSION} ${DESTINATION};\
 rm -f ${DESTINATION}/eclipse.jdt.ls;\
 ln -s eclipse.jdt.ls-${VERSION} ${DESTINATION}/eclipse.jdt.ls;\
 chmod 2777 ${DESTINATION}/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/config_linux"
  • configure
(use-package lsp-java
  :ensure t
  :after lsp
  (setq-default lsp-java-server-install-dir "/home/shared/opt/software/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/")

  (require 'dap-java)

  ;; (require 'lsp-java-boot)
  ;; ;; to enable the lenses
  ;; (add-hook 'lsp-mode-hook #'lsp-lens-mode)
  ;; (add-hook 'java-mode-hook #'lsp-java-boot-lens-mode)

  (add-hook 'java-mode-hook #'lsp)
  (add-hook 'java-mode-hook 'flycheck-mode)
  (add-hook 'java-mode-hook 'company-mode)

Emacs commands

Modifier keys


  • C stands for the <Control> modifier, key [Ctrl]
  • M stands for the <Meta> modifier, key [Alt] usually
  • S stands for the <Shift> modifier, ket [Shift]

Basic Emacs commands


Stop current command            C-g
Open file                       C-x C-f
Save file                       C-x C-s
Save file as new                C-x C-w
Close file                      C-x k


Select all                      C-x h
Auto-indent selected code       TAB
Copy                            M-w
Cut                             C-w
Paste (Yank)                    C-y
Undo                            C-/
Redo (unexistent: undo of undo) C-g C-/
Search                          C-s
Replace                         M-%
Replace with newlines           M-% <string to replace> RET C-q C-j RET  # where C-q C-j is the newline character
Go to line (emacs >= 23.2)      M-g g
  • rectangle
Put a marker                                        C-SPACE
# OR
Visual rectangle selection                          C-x SPACE
# then
Delete text rectangle (from marker)                 C-x r d
Cut text rectangle (from marker)                    C-x r k
Copy text rectangle (from marker)                   C-x r M-w
Paste (Yank) text rectangle (from first line)       C-x r y
Insert text in each line of rectangle (from marker) C-x r t <string> RET


Buffer navigation (i.e. open file)

Previous buffer                 C-x Left
Next buffer                     C-x Right
List buffers                    C-x C-b

Window navigation

It is not default. You can enable default keybinds (see (windmove-default-keybindings)) or you can explicit define yours like follows:

;; set keybindings to move focus to near windows
(global-set-key (kbd "C-c C-<up>") 'windmove-up)
(global-set-key (kbd "C-c C-<down>") 'windmove-down)
(global-set-key (kbd "C-c C-<right>") 'windmove-right)
(global-set-key (kbd "C-c C-<left>") 'windmove-left)

;; set keybindings to manage splitted windows size
(global-set-key (kbd "M-S-<up>") 'shrink-window)
(global-set-key (kbd "M-S-<down>") 'enlarge-window)
(global-set-key (kbd "M-S-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "M-S-<right>") 'enlarge-window-horizontally)

Source navigation

When available (when some package (like cquery above) allows emacs to understand the source code), you can go:

Find definitions                M-.
Find references                 M-?


Split window horizontally       C-x 2
Split window vertically         C-x 3
Focus to next window            C-x o
Enlarge window                  C-x ^
Close window                    C-x 0

Projectile commands

Alternate header source         C-c p a
Search file in project          C-c p f


Open Magit interface            C-x g
 quit                           q
 git add                        s
 git commit message             c c
  git completing commit         C-c C-c
 diffing with ediff             e


Start debugger                  M-x gdb (select the binary file)
Set a breakpoint                C-x C-a C-b
Delete a breakpoint             C-x C-a C-d
Step into next statement        C-x C-a C-s
Step out until previous stack   C-x C-a C-f
Step next current stack stat.   C-x C-a C-n
Continue                        C-x C-a C-r
Go to inner stack               C-x C-a >
Go to parent stack              C-x C-a <