5

I'm basically a level 0 emacs/orgmode user, trying to get the hang of Emacs/Orgmode. Doing this like for the 5th time already, as most Emacs beginners obviously. I have this feeling I'll like and be happy with the Emacs world, but oh well it takes a lot of patience. So I'm trying to do things little by little.

So I'm trying to do something that seemingly should be quite doable but can't understand how to make it work. The idea is to reuse predefined text within a buffer.

I think that what I want is to define kind of "text variables" or "snippets" and reuse that in my text later on.

Let's say I'm writing some specification for my laptop. I'd like to something like define a term in buffer (don't pay attention to syntax, it's just my interpretation), let's say "Thinkpad T470p, black, with dedicated graphics card, XYZ".

So I'd define this term at the beginning of my buffer.

The term, presumably, would have a label/name (not sure if this is the correct term) and some value. Let's say, the value is as above and label is "MyThinkPad".

Then when writing text, I'd like to start writing "MyThinkPad" and that would auto-insert the previously predefined text "Thinkpad T470p, black, with dedicated graphics card, XYZ" at the cursor position.

AND what is most important - if I change value of this "variable" in the buffer , that should auto-update throughout the buffer.

I'm not sure what is the right direction to look. I have a feeling this has to do with some of these "#+NAME" or "#+LABEL", or citations, or local variables, or something of that sort.

After reading online resources and trying out various ideas, I'm stuck and turning to community help. Really looking forward to your suggestions. Thanks in advance!

  • 2
    If it wasn't for the auto-update requirement, that would sound a lot like the generic abbrev-mode facility, but the persistent auto-update part makes things more interesting. It sounds like the actual text of the file should remain as "MyThinkPad" everywhere that was typed, and for display purposes only the instances should be changed. There are some generic features which would handle this as well, but finally I am imagining that you want this translation to be performed automatically during org exports? Could you confirm that one way or the other? – phils May 9 '18 at 9:42
  • 2
    You may have a look at macro replacement. – Juancho May 9 '18 at 15:08
  • @phils - well, I have a feeling that we're digging deeper than necessary. Or maybe I just haven't yet grasped the Emacs/Orgmode way of doing things. – Lauris May 10 '18 at 10:37
  • Basically I'd like to define like a "constant" within a buffer and then reuse it. Not sure if those should be name/value pairs. Maybe it's sufficient to define the whole phrase in the buffer, and then have something like "references" or "links" in text to that defined phrase. And if there is such reference, the buffer would understand that the whole phrase has to be shown. And since that would be a reference, it would auto-update if the defined phrase is changed. – Lauris May 10 '18 at 10:46
  • Somehow I'd like to think that this should be a regular thing, especially in technical writing. But probably I'm just dreaming here. – Lauris May 10 '18 at 10:46
1

Tl;dr Use Yasnippet

Hi, from the use case you describe, Yasnippet could provide a solution for you. On the EmacsWiki page for this project it is explained that:

[Yasnippet] allows you to type an abbreviation and automatically expand it into function templates.

In your case, the function template would just be a constant string.


Detailed Example

I could not find a simple example on how to make yasnippet work on my machine so I decided to add a little bit more detail to this answer myself.

  1. I downloaded the yasnippet package to ~/.emacs.d/lisp/yasnippet and put the following lines in my ~/.emacs.d/init.el:

    (require 'yasnippet)
    (setq yas-snippet-dirs '( "~/.emacs.d/snippets" ))
    (yas-global-mode 1)
    

    This tells yasnippet to look in the folder ~/.emacs.d/snippets for your personal snippets.

  2. Inside the ~/.emacs.d/snippets directory, I have created another directory called org-mode. The important point to notice is that yasnippet apparently expects the name of the folder to be equal to the name of the (minor-) mode you wish to use later.

  3. In the ~/.emacs.d/snippets/org-mode directory create a file test with the following content:

    # name: thinkpad
    # key: thinkpad
    # --
    "Thinkpad T470p, black, with dedicated graphics card, XYZ"
    

Now, whenever you are editing a file with orgmode, just type thinkpad+<TAB> and it will automatically expand the string thinkpad to "Thinkpad T470p, black, with dedicated graphics card, XYZ" as desired. Obviously you can later change the string to something else if you like.

| improve this answer | |
  • Will that cope with the requirement "AND what is most important - if I change value of this "variable" in the buffer , that should auto-update throughout the buffer."? I thought this method would just insert the literal text of the snippet (as it existed at that point in time), and then forget about it. – phils May 7 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.