Skip to content

Latest commit

 

History

History
464 lines (417 loc) · 18.7 KB

File metadata and controls

464 lines (417 loc) · 18.7 KB

Notes on using Emacs

My answer to “How do I get started with Emacs with minimal config”

OP’s Requirements:

Features I want (have no issue trying to add manually, but would like to see it there) -

  • Project Management
  • LSP with Linting
  • Popup/Toggle-based Terminals

My Answer:

I’d be happy to explain / work with you on these requirements. What you are looking for:

  1. project.el : Understanding what a project is (actions like searching in projects, switching projects, opening files in projects etc). IF you mean the other project management (task management) then org-mode which is a beast of it’s own. Both are built-ins.
  2. eglot for LSP. Invoke with M-x eglot from inside whichever project.
  3. I did not understand what this means.

A long time back, I wrote some documents that you might find useful:

  1. What a Programming Editor should be able to do : a list of table-stakes operations, and key-bindings for my setup of Emacs: https://github.com/vedang/emacs-up/blob/master/docs/02_what_a_programming_editor_should_do.org
  2. An Opinionated Emacs Tutorial: Getting started fast. : A presentation I used to give in my previous work-place for getting started with Emacs: https://github.com/vedang/emacs-up/blob/master/docs/01_opinionated_emacs_tutorial.org (This is more philosophy than quick-tips, but might be useful to you)

Emacs Refcards

  • M-x locate <RET> refcard <RET>
  • profit.

Clj -> Elisp Cheat Sheet

map = mapcar

mapc is similar to mapcar, expect that it does not accumulate the results. mapc should be used for side-effects, where we don’t care about the return value.

apply = apply

The way to call this is: (apply #’equal ‘(:a :A))

keep = delq nil

= = equal

first = car

rest = cdr

Other Notes:

The function `funcall` can be used to treat the first argument as a function on the rest of the arguments.

Eg: (funcall #’equal :a :A) => nil (funcall #’equal :a :a) => t

Regex replacements

Taken right from the Emacs Manual (I always seem to forget this). http://www.gnu.org/software/emacs/manual/html_node/emacs/Regexp-Replace.html

M-x replace-regexp <RET> c[ad]+r <RET> \&-safe <RET> replaces (for example) ‘cadr’ with ‘cadr-safe’ and ‘cddr’ with ‘cddr-safe’.

M-x replace-regexp <RET> \(c[ad]+r\)-safe <RET> \1 <RET> ‘\d’ in newstring, where d is a digit, stands for whatever matched the dth parenthesized grouping in regexp. Note : The parens making groups need to be escaped.

How to check dependents of a library

(require 'loadhist)
(file-dependents (feature-file 'cl))

Compiling and Running the Emacs master branch on Ubuntu

Set up the prerequisite libraries

This command is created by:
  1. Run the command sudo apt build-dep emacs and copy all the packages it will install. (Don’t actually install the packages)
  2. Remove all the ImageMagick related libraries from this command, we have installed it from source.
  3. Review the output of ./configure command in Emacs. See if there are new options which are not covered by your distribution’s old version of Emacs.
sudo apt install libx11-dev libtiff5-dev libtiff-dev libgif-dev libjpeg-dev libpng-dev libxpm-dev libcairo2-dev libexif-dev libexpat1-dev libglx-dev libgmp-dev libgnutls-openssl27 libgnutls28-dev libgraphite2-dev libharfbuzz-dev libharfbuzz-gobject0 libice-dev libidn2-dev libilmbase-dev libilmbase25 libjbig-dev libjpeg-turbo8-dev libgnutlsxx28 libjpeg8-dev liblcms2-dev liblockfile-bin liblockfile-dev liblockfile1 liblqr-1-0 liblqr-1-0-dev libltdl-dev liblzma-dev libm17n-0 libm17n-dev libgtk-3-dev libncurses-dev libncurses5-dev libopenexr-dev libopenexr25 libopengl-dev libopengl0 libotf-dev libotf0 libp11-kit-dev libpango1.0-dev libpixman-1-dev libpng-dev libpthread-stubs0-dev librsvg2-dev libsm-dev libsub-override-perl libsystemd-dev libtasn1-6-dev libthai-dev libtiff-dev libtiffxx5 libtool libunbound8 libwayland-bin libwayland-dev libwmf-dev libwmf0.2-7 libx11-dev libxau-dev libxaw7-dev libxcb-render0-dev libxcb-shm0-dev libxcb1-dev libxcomposite-dev libxcursor-dev libxdamage-dev libxdmcp-dev libxext-dev libxfixes-dev libxft-dev libxi-dev libxinerama-dev libxkbcommon-dev libxmu-dev libxmu-headers libxpm-dev libxrandr-dev libxrender-dev libxt-dev libxtst-dev m17n-db nettle-dev pango1.0-tools po-debconf postfix quilt sharutils wayland-protocols x11proto-core-dev x11proto-dev x11proto-input-dev x11proto-randr-dev x11proto-record-dev x11proto-xext-dev x11proto-xinerama-dev xaw3dg xaw3dg-dev xorg-sgml-doctools xtrans-dev xutils-dev gnutls-bin graphviz autopoint gsfonts libxaw3dxft8-dev libwebkit2gtk-4.0-dev libgccjit-10-dev libjson-c-dev libjson-glib-dev libjansson-dev

Run the installation commands from inside Emacs source directory

Note: if you are doing this for the first time, you need to run ./autogen.sh as the first command. This command creates the configure script and various Makefiles, which we use in the next commands.

make distclean
./autogen.sh
./configure --with-imagemagick --with-json --with-native-compilation --with-xft --with-xwidgets --with-pgtk --with-tree-sitter
# Alternative configure command:
./configure --without-toolkit-scroll-bars --with-x-toolkit=gtk3 --with-xpm=ifavailable --with-jpeg=ifavailable --with-gif=ifavailable --with-tiff=ifavailable --with-xml2 --with-rsvg --without-pop --with-png --with-mailutils --with-native-compilation --with-cairo --with-harfbuzz --with-tree-sitter --with-sqlite3
# If everything works
make bootstrap
# If everything works
make
# Test the binary
src/emacs -Q
# Install the binary
sudo make install

Notes:

  1. There is no Makefile before you run autogen.sh for the first time.
  2. You need to install libgccjit-<x>-dev where <x> is your gcc version. gcc --version. (for native compilation support)
  3. You need to install libtree-sitter-dev (for tree-sitter support)

Compiling and Running Emacs 29 on master branch (Mac)

I successfully compiled Emacs from source on the M1 Macbook Air right now (from https://github.com/jimeh/build-emacs-for-macos/). Noting down the steps in the hopes it will help others.

Initially, I had a linker failure ld: library not found for -lwebp. I added the line brew 'webp' to the Brewfile to install this dependency. However, later I realized that the problem was because the correct LIBRARY_PATH was not being set. So I’m not sure if this change is needed.

The actual commands I executed were:

brew bundle
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/lib/
export CPATH=$CPATH:/opt/homebrew/include/
./build-emacs-for-macos --git-sha c00ffe263d9ffeb4d2a5e13cc124c786152137db

Once compilation was successful, I extracted the .tbz file created under builds and signed it as explained in https://github.com/jimeh/build-emacs-for-macos/pull/65/files

codesign --force --deep -s - Emacs.app

Org Mode common stuff that I always look for

  • I’m always looking for stuff in Org-Mode Tables, so check there for common tasks first.

Dired Tips

Dired tips from ilemming

Show commits related to specific files

Did you know, you can mark files in Dired buffer and then do

M-x magit-dired-log

and it will show commits but only related to those files?

Find files containing specific phrase

Let’s say you want to recursively find all *.scss files and then mark those that contain “mixin”, so you can then move them or rename, delete etc.
M-x find-name-dired
% g mixin

Recursively display directory data. Peeking

Of course you know about this feature of dired-mode.

You can “peek” into a subdirectory by pressing <i>

But did you know you can “insert” dirs recursively?

Use an argument and -R option.

C-u i -R RET

Dynamically filter directory listing

  • Install dired-narrow

Dired: Send content to external programs

Let’s continue on sending content to external apps.

You can mark files in Dired-mode and pipe them into a program.

  • Mark files - m
  • M-x dired-do-shell-command - ! or X
  • M-x dired-do-shell-command-async - &
  • Command: wc-l to run the command on each marked file, wc -l * to run the command on all the files in one go.

Dired: turn arbitrary shell output to a dired buffer

Did you know that textual representation of a directory can be turned into a legit Dired buffer in @emacs ?
  • get output of “find” or “ls”,
  • edit it,
  • then do `M-x dired-virtual`

if you’re still not convinced that Emacs is dope - stay tuned, I’ll keep posting cool tips

Rename multiple files in Dired

  • Use wdired for this.
  • Activate writable mode in Dired using M-x dired-toggle-read-only or C-x C-q
  • Make all the edits as you would in a normal buffer
  • Exit Wdired and commit the changes, using C-x C-s or C-c C-c

Move files from one directory to another in Dired

  1. Open the target directory in Dired. Position the cursor on the . entry and copy the absolute path using M-0 w.
  2. Open the source directory in Dired. Mark all the files you want to move. Press R to trigger the move and paste the absolute path.
  3. Press Enter, done.

Emacs and SQL

sql-interactive-mode is excellent and my preferred way of interactive with a database that supports SQL.

To start a SQLi buffer + work against a database:

  1. M-x sql-<name-of-db> (eg: sql-postgres). If you have the .dir-locals.el file (see below), values will be auto filled in. This will start an interactive SQL buffer.
  2. Open a .sql file to write SQL in.
  3. In this file, M-x sql-set-product, followed by M-x sql-set-sqli-buffer.

To speed up connection to an SQL database, add the following to a .dir-locals.el file:

((sql-mode . ((sql-product . postgres)
              (sql-user . "pgw")
              (sql-password . "")
              (sql-port . 5432)
              (sql-server . "192.168.33.10")
              (sql-database . "pgw-main"))))

RESTRUCTURED Installing Emacs from source

  • State “RESTRUCTURED” from [2021-10-18 Mon 18:01]
    Outdated now, please refer to Compiling and Running Emacs 28 on master branch (Ubuntu).

Ubuntu 12.04

$ git clone git://git.savannah.gnu.org/emacs.git

$ sudo apt-get install libxaw7-dev libjpeg-dev libgif-dev libxpm-dev libpng12-dev libtiff4-dev libncurses5-dev libtinfo-dev libglib2.0-dev intl-fonts libgtk2.0-dev libxaw3dxft6 librsvg2-dev imagemagick libdbus-1-dev libgconf2-dev libm17n-dev libotf-dev graphicsmagick-libmagick-dev-compat

$ make distclean $ ./autogen.sh $ ./configure –prefix=/opt/emacs/ –with-xft –with-x-toolkit $ make bootstrap $ sudo make install

Mac OS X 10.7

$ brew install emacs –HEAD –cocoa –use-git-head

$ brew linkapps

$ make tags

RESTRUCTURED Compiling and Running Emacs 28 on the native-comp feature branch (Ubuntu).

  • State “RESTRUCTURED” from [2021-10-18 Mon 18:02]
    • Outdated, please refer to Compiling and Running Emacs 28 on master branch (Ubuntu).

The gist of it, building Emacs from Source

./autogen.sh
./configure --with-nativecomp
# If everything works
make
# Test the binary
src/emacs -Q
# Install the binary
sudo make install
  

Problems during Installation

Configure fails because libgccjit fails the smoketest

  • Ensure that libgccjit is installed and at the latest version (at this point in time, the latest version is 10)
  • Ensure that gcc is at the same version as libgccjit (also 10)
  • Install the Ubuntu toolchain PPA for the latest versions of gcc and libgccjit.
  • After updating / installing the latest gcc, libgccjit-* packages, you will need to use update-alternatives to ensure that the correct tools are being picked.
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
    sudo update-alternatives --config gcc
    sudo update-alternatives --config g++
        
  • Ensure that gcc-10 is selected, so that you don’t see the ‘failed smoke test’ error message from libgccjit.

Configure fails because various libraries don’t exist on your system.

  • Emacs needs a number of different libraries during compilation. You should look for an install the -dev version of any such libraries that are missing. (Eg: libxpm-dev, libgif-dev, libtiff-dev)

Problems Post Installation

You need to generate native compiled files for all your emacs-lisp code

  • Add this to your init.el
    (if (and (fboundp 'native-comp-available-p)
             (native-comp-available-p))
        (setq comp-deferred-compilation t)
      (message "Native complation is *not* available"))
        
  • The message in the above code will also tell you whether the Native Compilation binary is correctly built.
  • As a one-time run, you can also execute the following code after M-x ielm
    (native-compile-async "/home/<yourname>/.emacs.d" t)
        
  • This will create all the necessary .eln files for you.

You need to clean install your packages, preferably the latest versions of the packages

  • Best to just reinstall all the packages you depend on, to ensure that they get compiled properly.

Start and profit, huge speed boosts.

  • Very little configuration code broke for me (only one isearch modification broke, which used substitute-key-definition and substituted a function which took optional arguments with another function which took 0 arguments. This seems to not work within native compilation.