Emacs Snippets 🔗

🗓 In kb/snippets/

#emacs

Introduction

Emacs is my main text processor software. I do a lot of things with it: writing this snippet, managing some projects with org-mode and, of course, coding. Emacs is very powerful and the learn curve is sometimes very tough and difficult. But with time you will learn how to use it efficiently.

I found some tips to improve my use of Emacs and I will share them here...

Moving a part of text left or right

You want to select a part of text (a region) and move it to the right or to the left, increasing or removing spaces:

Add line numbers (when coding)

Add this line to ~/.emacs.d/init.el:

(global-display-line-numbers-mode)

Add syntax/word checking

You can use flycheck which supports more than 50 languages.

Under Debian:

apt install elpa-flycheck flycheck-doc

Add this line to ~/.emacs.d/init.el:

(add-hook 'after-init-hook #'global-flycheck-mode)

Then you need to install a checker program. We will take the example of Python3 with Pylint:

apt install python3-pylint-common

Add this line to ~/.emacs.d/init.el:

(setq flycheck-python-pylint-executable "python3")

Open a Python3 file and: Ctrl-C ! v to verify is everything is well configured. Then, you can use:

Read flycheck manual for more information...

Use copy and paste in Wayland/X11 with emacs-nox

emacs-nox (Emacs compiled without X11 support) can't handle X11 commands for accessing X11 clipboard. My main short time solution: use xclip!

Org-mode always visible stars

Sometimes, things that could be considered simple simply do not work! I faced a problem under org-mode with stars display. I use the indent mode a lot in org-mode (because it is clearer to read). On a particular configuration (work laptop), stars where always displayed, whatever command or variable was configured (indent mode/hidestars/etc.).

After wasting about an hour, I changed the Emacs theme and voila! In org-mode, hiding the stars is just putting an "invisible" color to the leading stars. In the previous theme, this color was white (on a dark background) and was actually very visible. I just installed zenburn theme (elpa-zenburn under Debian) and configured it to fix my problem.

All you have to do is to choose an org-mode aware Emacs theme.

Recently opened files

By default, Emacs can't remember the list of the last opened files. But there is a module for that, called recentf. I've put the following at the end of my .emacs config file to active it by default:

;; Activate recentf
(recentf-mode 1)
;; Max saved items = 25
(setq recentf-max-menu-items 25)
(setq recentf-max-saved-items 25)
;; Ctrl-X Ctrl-r for opening the list of files
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
;; Save the list of files every 5 minutes.
(run-at-time nil (* 5 60) 'recentf-save-list)

With that configuration, simply use Ctrl-x Ctrl-r to list recently opened files, use arrows in the buffer to select a file and press Enter to open it in Emacs.

It is also available via the menu (F10 -> File -> Open Recent).

Use Firefox as default web browser under Wayland for opening org-mode links

You have Firefox and Chromium installed on your system. Firefox is using Wayland environment (with MOZ_ENABLE_WAYLAND variable). Emacs persists to use Chromium. Here is how to fix this...

First, check xdg-settings configuration for web browser:

xdg-settings get default-web-browser

If it is chromium.desktop, you just have to change it to firefox.desktop. But there is another problem to deal with: Wayland!

Use the following desktop application file under ~/.local/share/applications/firefox.desktop:

[Desktop Entry]
Type=Application
Name=Firefox
Exec=env MOZ_ENABLE_WAYLAND=1 /usr/bin/firefox %u
X-MultipleArgs=false
Icon=firefox
Categories=Network;WebBrowser;
Terminal=false
MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/vnd.mozilla.xul+xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;

Then, configure the default web browser:

xdg-settings set default-web-browser firefox.desktop

From now, xdg-open will use your local firefox.desktop file rather than the system one. As org-mode uses xdg-open, you are set up!