Application emacs

Jump to navigation Jump to search



  • from Debian-like O.S.:
sudo apt-get install emacs24-nox
su -
#### as root
# cleanup
apt-get autoremove --purge emacs* emacsen-common+
# dependencies
apt-get install gcc make libtinfo-dev zlib1g-dev
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="26.3"                                          # Edit when needed
mkdir -p "${DESTINATION}/tmp"
cd "${DESTINATION}/tmp"
curl -kLO "${VERSION}.tar.gz"
tar -xzf "emacs-${VERSION}.tar.gz"
cd "emacs-${VERSION}"
./configure --prefix=${DESTINATION}/emacs-${VERSION} -with-x-toolkit=no --without-x --without-all --with-zlib --with-modules --with-threads --with-compress-install CFLAGS="-O3 -s"
make install
rm -rf tmp emacs
ln -s "emacs-${VERSION}" emacs
chown -R root:users "emacs-${VERSION}"
find "emacs-${VERSION}" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "emacs-${VERSION}" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
#### as final user
SOFTWARE_PATH="/home/shared/opt/software"               # Edit when needed


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:// ~/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 cquery

su -
#### as root
# dependencies
apt-get install g++ cmake git
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="70c755b2e390d3edfb594a84a7531beb26b2bc07"      # Edit when needed
mkdir -p "${DESTINATION}/tmp"
cd "${DESTINATION}/tmp"
git clone .
git checkout "${VERSION}"
git submodule update --init
mkdir build && cd build
cmake --build .
cmake --build . --target install
rm -rf tmp cquery
ln -s "cquery-${VERSION}" cquery
chown -R root:users "cquery-${VERSION}"
find "cquery-${VERSION}" -type d -exec chmod u+rwx,g+rx,g-w,o-rwx {} \;
find "cquery-${VERSION}" -type f -exec chmod u+rw,g+r,g-w,o-rwx {} \;

(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)

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 "<M-up>") 'windmove-up)
(global-set-key (kbd "<M-down>") 'windmove-down)
(global-set-key (kbd "<M-right>") 'windmove-right)
(global-set-key (kbd "<M-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 <