Emacs

313 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
151
 
 

Have been using emacs for a while now with doom and have a question to the community:

When you have a project inside of docker with packages instead by pip, how to you have your eMacs and LSP resolve the import statements?

I have heard of TRAMP but this seems like a solution for jacking into a docker container that is running to just modify files.

Thanks, Aaron

152
 
 

I added an embark config from: https://karthinks.com/software/fifteen-ways-to-use-embark/#open-any-buffer-by-splitting-any-window, which works really good, the only problem is that if a file is opened with project-find-file-in, it doesn't respect its own directory.

Steps:

  1. When the default-directory set to ~/, M-x browse-emacs-config to open a file list of ~/.config/emacs in the minibuffer
  2. embark-act on a file lisp/init-lib.el
  3. o and b to open the selected file horizontally
  4. A buffer opened on the right side, but the directory is still using the default-directory in step 1 instead of the buffer's directory, causing the buffer to visit ~/lisp/init-lib.el instead of ~/.config/emacs/lisp/init-lib.el.

Config:

(defun find-file-in-project (&optional dir)
  (interactive)
  (let ((project (project-current nil dir)))
    (project-find-file-in nil nil project)))

(defun browse-emacs-config ()
  (interactive)
  (find-file-in-project "~/.config/emacs"))

(eval-when-compile
  (defmacro my/embark-aw-action (fn)
    `(defun ,(intern (concat "my/embark-aw-" (symbol-name fn))) ()
       ,(format "Open %s buffer selected with ace-window" (symbol-name fn))
       (interactive)
       (with-demoted-errors "%s"
         (require 'ace-window)
         (let ((aw-dispatch-always t))
           (aw-switch-to-window (aw-select nil))
           (call-interactively (symbol-function ',fn)))))))

(define-key embark-file-map (kbd "o") (my/embark-aw-action find-file))
153
154
 
 

I hope my question makes sense.

I am using Doom Emacs for a while now and have become fairly proficient. But I feel like whenever I am browsing emacs content online there are still many topics for me to discover. So I was wondering if there is anything that I might be "missing" yet which might help with my productivity or improve my development skills.

Sofar I what have learned, on top from my head:

  • Org/Org Agenda (refile etc.)
  • Magit
  • Vterm
  • LSP Commands
  • Multiple Cursors
  • Literal Config
  • Navigating Emacs itself (which key, debugging, reading Emacs-Lisp (abit))
  • Using Language specific commands, i.e. send buffer to repl
  • Using Undo with Vundo

Only thing I know that I still need to learn is beeing more proficient with vim keybindings, but with that I know where to start.

I know the question is quite broad, but maybe there some "killer features" worth to explore which I am not aware of yet.

I'd appreciate any input.

155
 
 

I'd like to use the fonts in the following order for East Asia text.

  1. AR PL UKai HK
  2. BabelStone Han
  3. UnGungseo
  4. Noto Sans CJK KR

The lines that follow are the settings I've gathered to enlarge the text for easier reading.

 1 (dolist (charset '(han symbol cjk-misc bopomofo))
 2   (set-fontset-font t charset (font-spec :family "AR PL UKai HK" :size 28 :registry "big5")))
 3 
 4 (set-fontset-font t '(#x2ff0 . #x2fff) (font-spec :family "AR PL UKai HK" :size 28))
 5 
 6 (set-fontset-font t '(#x4dc0 . #x4dff) (font-spec :family "BabelStone Han" :size 28))
 7 
 8 (set-fontset-font t #x21fea (font-spec :family "BabelStone Han" :size 28))
 9 
10 (dolist (x '( #x377f #x456c
11 	      #x53d3 #x53d5 #x5926 #x5b52 #x5f1c #x5387
12 	      #x6056 #x6abf #x6e70 #x6598 #x659a #x6752 #x67bc
13 	      #x7683 #x7a41 #x7c36 #x7e85
14 	      #x887a #x8c38 #x8a9d #x8a9f
15 	      #x9aea #x9af0 #x9f04 #x9f05
16 	      #xfe47 #xfe48))
17   (set-fontset-font t x (font-spec :family "Noto Sans CJK KR" :size 28)))
18 
19 (dolist (x '(#x7232 #x654e #x7d76)) (set-fontset-font t x (font-spec :family "UnGungseo" :size 28)))
20 
21 (dolist (x '(#x37a2
22 						 #x41c2 #x4b6b
23 	     #x5928
24 	     #x7680 #x8c59
25 	     #x2002c #x20447 #x27141 #x22992 #x22994
26 	     #x23a95 #x268fb #x27bab
27 	     #x294a8))
28   (set-fontset-font t x (font-spec :family "BabelStone Han" :size 28)))

Is there a better way to do that?

156
 
 

I found myself wanting to automatically keep a log of every org-roam entry I work on. After struggling with the capture system for a while, I managed to make something work.

(setf (alist-get "u" org-roam-dailies-capture-templates nil nil #'equal)
      '("updated" plain nil
        :target (file+head+olp "%<%Y-%m-%d>.org" "#+title: %<%Y-%m-%d>\n" ("Updated"))
        :unnarrowed t :prepend t))

(defmacro +save-current-buffer (&rest body)
  "Save current buffer; execute BODY; restore original buffer.
I have found that this macro works under some circumstances in
which ‘save-current-buffer’ does not."
  (declare (indent 0))
  `(let ((original-buffer (current-buffer)))
     (unwind-protect (progn ,@body)
       (when (buffer-name original-buffer)
         (switch-to-buffer original-buffer)))))

(defmacro +edit-to-string (&rest body)
  "Eval BODY in a temporary buffer and extract contents to a string."
  (declare (indent 0))
  `(with-temp-buffer
     ,@body
     (buffer-string)))

(defun +org-element-contents (element)
  "Extract contents of ELEMENT from current buffer."
  (buffer-substring (org-element-property :contents-begin element)
                    (org-element-property :contents-end element)))

(defun +org-roam-updated-on-save ()
  "Log current Org-roam buffer as modified."
  (when (org-roam-buffer-p)
    (let* ((original-buffer (current-buffer))
           (id (org-id-get (point-min)))
           (title (org-get-title))
           (link-text (format "%s\n" (+edit-to-string
                                       (org-insert-link nil id title)))))
      (save-excursion  ;; in case we are currently editing the daily note
        (+save-current-buffer
         (org-roam-dailies-goto-today "u")
         (unless (eq original-buffer (current-buffer))
           (let ((elt (org-element-context)))
             (if (not (eq 'plain-list (org-element-type elt)))
                 (insert "\n- " link-text)
               (org-down-element)
               (cl-loop
                for elt = (org-element-context)
                while (eq 'item (org-element-type elt))
                if (equal link-text (+org-element-contents elt)) return nil
                do (org-forward-element)
                finally (insert "- " link-text))))))))))

(add-hook 'after-save-hook #'+org-roam-updated-on-save)

I originally tried to do this using just capture templates (i.e., just an interpolated Lisp expression, no wrapper code), but I'm not sure that that's possible due to some flexibility problems with the Org-roam capture system.

One thing I'm not happy about is having to use my weird homespun +save-current-buffer macro, but the original save-current-buffer macro doesn't switch back for some reason.

I am kind of? happy about having gotten some experience with the loop macro. I'm still not sure I like it, but I understand better why it exists in a language that doesn't have other, more structure looping facilities.

157
 
 

Screenshot: https://0x0.st/Hwru.jpg

In this Screenshot you see the frames of two Emacs processes. The left frame has been started by emacs -q, while the right frame has been started by emacs -Q. So no personal configuration (besides .Xresources) is used. The version is Emacs 29.1., X11, Linux.

The left frame shows the font I'd like to use, and used since many years with Emacs already. But the left frame also shows a "distorted" display of example characters. The important character is . It is clearly visible in this screenshot, that Emacs changes the font (glyph?) to display this character if the character needs to be rendered in bold. (similar things happen to other characters or itallic)

Each frame has 3 windows. The top window shows an org buffer with various characters.

The second from top window is the output of what-cursor-position, of the normal rendered character.

The bottom window shows the output of what-cursor-position with a bold character where Emacs changed the font to display this character. (This lines are much wider and taller, even though they contain the same number of monospaced letters.)

Emacs did not had this behavior of changing fonts to display certain characters in versions v28.x and older (at least I never noticed it). I did not change OS installed packages, fonts, setting and stuff, so I'm pretty sure it's an Emacs v28.x to v29.1 related change.

What can I do to make Emacs behave like in older versions, i.e. not changing fonts to display those characters?

I tried various things (i.e. disabling HarfBuzz), but I am clueless what I need to configure.

I know that the problematic setting is a *font: line in my .Xresources file. But finding a suitable font was some time consuming frustrating process, because I didn't like other fonts for various (mostly readability or size) reasons.

TL;DR: Question: how can I make Emacs v29.1 to not change fonts to display certain characters?

158
 
 
159
160
 
 

I am 100 % a keyboard only user, but sometimes i want to relax and browse dired only with one hand. But the standart behaviour of dired , when clicking on files or directories, is really ass. Thus i wrote some code to gain this functionality:

Left-Mouse-Click: The same as Pressing Enter

Right-Mouse-Click: close current buffer and go to last visible buffer

thus you can left click on entry, then go back with right click which is super fast and handy. Try it out !!!

(I am using evil, but you can easily bind the keys with :

(define-key dired-mode-map .... )

code:

(defun my/dired-jump (event)

(interactive "e")

(mouse-drag-region event)

(dired-find-file))

(defun my/dired-jump-close ()

(interactive)

(kill-current-buffer))

(evil-global-set-key 'normal (kbd "") 'my/dired-jump-close)

(evil-define-key '(normal visual motion emacs) dired-mode-map

(kbd "") 'my/dired-jump)

EDIT: CHECK OUT THIS DEMO

https://imgur.com/a/G6K8JKO

161
 
 

Hey, I was working on this one off and on for a few days after briefly trying out skeleton-mode, yasnippet, and some other stuff, and not really being too happy with them. I find that I have a lot of repetitive editing tasks where I need to do something to a small block of code a lot, but in the process change some names or values in a way that's just a little bit different each time. Normally this is where people would start to reach for yasnippet and auto-yasnippet, which is fine if that works for them, but personally that's just a bit more heavyweight and powerful than what I normally need. What I wanted was just a way to enhance a regular Emacs keyboard macro to support that sort of thing, so I wrote this. If it helps you too, wonderful!

To use, just press C-x Q (that's a capital Q, not a lowercase q) during keyboard macro recording, and press your normal enter/return/minibuffer-exit when you're done. I went through a lot of trouble figuring out how to make the minibuffer exit also exit the sub-macro recording!

;; Keyboard macro enhancement. If you call this, instead of
;; kbd-macro-query, it will prompt the user for a value. This value
;; will then be inserted into the buffer. Every time you call the
;; macro, you can provide a different value.
;;
;; Alternatively, you can call this with a prefix argument. If you do
;; this, you will be prompted for a symbol name. Instead of the value
;; being inserted into the buffer, it will be saved in the symbol
;; variable. You can then manipulate it or do whatever you want with
;; that symbol as part of the keyboard macro. Just, when you do this,
;; make sure you don't use minibuffer history at all when defining the
;; macro, or you can get some unexpected behavior if you save your
;; macro for later use and try it a few hours later!
(defun config:macro-query (symbol)
  (interactive
   (list (when current-prefix-arg
           (intern (read-from-minibuffer "symbol: ")))))
  (cl-flet ((internal-exit ()
              (interactive)
              (exit-recursive-edit)))
    (let ((making-macro defining-kbd-macro)  ;; Save value.
          (temp-map (make-sparse-keymap)))
      ;; Temporarily bind what is normally C-M-c (exit-recursive-edit)
      ;; to RET, so RET will work in the spawned minibuffer.
      (set-keymap-parent temp-map minibuffer-local-map)
      (substitute-key-definition 'exit-minibuffer #'internal-exit temp-map)
      (let ((exit-fn (set-transient-map temp-map (-const t))))
        (cl-flet ((also-quit-minibuffer ()
                    ;; When this is called (advice after
                    ;; recursive-edit), this-command should be
                    ;; whatever was just used to exit the recursive
                    ;; edit / minibuffer. Usually RET. Push that onto
                    ;; the unread commands, and it will immediately
                    ;; get picked up and executed. We also want to use
                    ;; this moment to turn off the transient map.
                    (funcall exit-fn)
                    (when making-macro
                      (setq unread-command-events
                            (nconc (listify-key-sequence (this-command-keys))
                                   unread-command-events)))))
          (advice-add 'recursive-edit :after #'also-quit-minibuffer)
          (unwind-protect
              (let ((input (minibuffer-with-setup-hook
                               (lambda ()
                                 (kbd-macro-query t))
                             (read-from-minibuffer "Value: "))))
                (if symbol
                    (set symbol input)
                  (insert input)))
            ;; Ensure that the advice and minibuffer map goes back to
            ;; normal.
            (advice-remove 'recursive-edit #'also-quit-minibuffer)
            (funcall exit-fn)))))))
(global-set-key (kbd "C-x Q") 'config:macro-query)
162
 
 

I've always been curious about Emacs but never made the plunge. I usually prefer vim/markdown vs org-mode/emacs. I've been using Logseq for some time but find the interface too sluggish. I found "ekg" emacs plugin but it's missing a few features to replace Logseq. I would like for all my notes to be combined into a single graph where they can be directly edited/manipulated without having to open up another buffer. Any other plugins aside from ekg that would give me a Logseq-like experience? (no org-mode, no note titles) Thanks!

163
 
 

I'm looking for a way to preview the results of a grep search within files as well as fold the output when there are multiple matches in the same file.

helm-projectile-ag is my current grepping tool but doesn't seem to offer such a facility, unless I haven't read the docs well enough.

My issues are:

  1. It can present a lot of hits in the same file it but lists each one separately. A good option would be to fold the output when there are multiple matches, then I can use the Tab keep to expand the fold and scan through the previews and even edit them right away.

  2. The search panel is modal. It doesn't create a result buffer which you can save and then return to later to continue to select files to edit. Once you lose focus it is gone. It doesn't even seem to retain the search term so you can use it later.

  3. It would be nice to have a window that shows the actual CLI command passed to the search utility. I see them once in a while but it would be good to show them up front or make them easily accessible.

Do any of the project grep tools or helm-projectile-ag itself offer such features?

164
165
1
c-ts-mode issue (alien.top)
submitted 1 year ago by SS4L1234@alien.top to c/emacs
 
 

When using cs-ts-mode, it incorrectly forces me to use paranthesis. For example:

  if (whatever == -1)

it won't auto-indent the next line unless I use braces {} with the if statement, even if there is just one statement with a semi-colon after the if. Also, as I'm typing, it weirdly indents my functions as I type them.

Any way to fix these?

166
 
 

My only tab-related init.el config is:

(setq-default indent-tabs-mode nil) ;; indentation can't insert tabs
(setq-default tab-width 4) ;; make tab-width 4 (spaces)

but in c-ts-mode I have that the indentation only does and works with 2 spaces (I need to indent-region whole file otherwise to fix it). How can I make this back to 4?

167
 
 

Hello, I have immensely nested directories and very long filenames, some take up 3 lines when full screen.

Is there a way to shorten them or have just the name of the bookmark/file show up in the dashboard?

Sorry if noob question but I am a noob.

168
 
 

Hi everyone. So im new to emacs and im currently using doom emacs on my laptop.

Everything is working fine, but today i decided to install it on my desktop. Im using all the settings from my laptop, but for some reason when i press enter on a link for example, emacs just goes down a line similiar to me pressing the down arrow/j key. This happens on all the links.

Any idea what the issue could be? This doesnt happen on my laptop

Edit: Just to clarify, clicking on the link with my mouse works normally.

Edit 2: Okay so apparently i needed to just do a doom sync since no profile was loaded i think. It said "generating profile" and now it works :) Noob misstake, but ill let this post still be here if anyone runs into the same problem in the future

169
 
 

My only tab-related init.el config is:

(setq-default indent-tabs-mode nil) ;; indentation can't insert tabs
(setq-default tab-width 4) ;; make tab-width 4 (spaces)

but in c-ts-mode I have that the indentation only does and works with 2 spaces (I need to indent-region whole file otherwise to fix it). How can I make this back to 4?

170
 
 

Aside from new language sources that the community can contribute at any time, this package feels more or less feature complete now.

v1.0.0 on GitHub

For those who don't know, treesit-auto enables automatic detection, installation, and usage of tree-sitter grammars and modes in Emacs 29+. Vanilla Emacs exposes some variables and functions that facilitate this behavior, but without treesit-auto it's up to you to check when tree-sitter is available and configure how Emacs behaves when it is (or isn't). This package takes that work away from you and is designed to fit what the vast majority of users would expect from automatic use of tree-sitter in Emacs.

This started out as just my own configuration to treesit-language-source-alist, major-mode-remap-alist, and auto-mode-alist, but now has a lot of features that didn't exist just under a year ago when I first announced it.

  1. Automatic detection and installation for built-in tree-sitter modes, such as rust-ts-mode, yaml-ts-mode, toml-ts-mode, typescript-ts-mode, and go-ts-mode. You can get this behavior through the treesit-auto-add-to-auto-mode-alist function (the name is too long...I know)
  2. Less buggy behavior overall, such as ensuring we switch to the tree-sitter mode after installing it
  3. Detecting and installing grammars required by the current mode, such as TypeScript and TSX requiring each other

This package isn't perfect. It's honestly still a bit of a hack, and many parts of the code are too clever for my own liking. It does its job, though, and I've been a happy, daily user of it for the last 9 months or so. Of course, a big thank you to all who have supported the project and contributed to it along the way!

171
0
Emacs: ediff basics (www.youtube.com)
submitted 1 year ago by geospeck@alien.top to c/emacs
172
 
 

Hey hey.

I use the heck out of tuedachu/ytdl. One thing I would like to improve about it is that when you queue a download with ytdl-download it appears to be a synchronous call and it freezes emacs until youtube-dl/yt-dlp is able to start the actual download.

I've yet to dive into the code to see what synchronous function is being called -- I'm very aware of the "single-threaded" nature of Emacs -- but I use other libraries that are likely shelling and they do not hang like this and so I would like to see if there's a way to I can bring whatever technique they are using over to a PR against tuedachu/ytdl.

Appreciate any advice from the crew here.

173
 
 

I would like to share with you a new Emacs package called project-tasks that I had been working on. With project-tasks, you can effortlessly manage tasks within your project using the power of org-babel.

You can take a look here https://github.com/TxGVNN/project-tasks

I developed and use it a long time, and it helps me a lot of to manage tasks I need to run on a project.

Example: tasks.org

#+name: docker-compose up
#+begin_src compile :name "docker-compose-up" :results none
docker-compose up -d
#+end_src

You can see more examples in tasks.org on repository.

174
 
 

as mind map user I like to jot idea with parent, children, sibling hierarchy.

I am not satisfied with the present method of creating the three above as need to do a lot of adjustments ex option+ enter to create a new sibiling then ,Shift+<, or Shift+> to adapt the sibiling as a parent or sub. As I am in a creative process would like a faster way to achieve this.

For long list of sibilings I resolved writing in plain text the list and then shift+V and jjjj to select the list and then i launch toggle-heading, but this does not work if I add myself spaces in the plain text.

Please do you know a fast way to open a parent/children/sibiling node, I see a shortcut could be a solution, but do not know if is a good idea because should combine more commands

175
 
 

I have a number of edits I need to do to yaml files. I'd like to drive emacs in the following way: mapcar over a list calling a function that opens a file, searches for a match, switches to interactive mode so I can adjust point, inserts some pre-formatted data specific to the match, allows me to edit the insertion to fix the inevitable typos that exist and, when I'm ready, saves the file and moves to the next element. I'm automating the file and buffer navigation as well as the content insertion but doing precise placement and editing by hand. Sadly, I did this once previously years ago but I don't remember how.

Relevant rant: this stupidity's only necessary because yaml's flexibility for too many ways to do the same thing so reading the yaml in, adjusting the data structures and writing it back out make pull requests horrific as minute semantic changes can have massive syntactic ones.

view more: ‹ prev next ›