I was looking at this on StackOverflow and was wondering if there was a nice way built-in to edit strings of text stored in variables as if they were buffers? M-x set-variable doesn't really cut it when you have a long string of LaTeX options inside a variable.

I'm looking for behavior similar to the package string-edit, but in order to edit emacs variables.

  • I'm envisioning something along the lines of string-edit but which recognizes variables.
    – wdkrnls
    Dec 1 '14 at 17:53
  • What do you mean by "recognizes variables"?
    – Malabarba
    Dec 2 '14 at 2:05
  • 1
    If you download string-edit from melpa, you'll see how that lets you edit a string as if it were in its own separate buffer. I'd like to (edit-variable-at-point) and have it bring up a buffer containing a representation of the contents of the variable, especially if that variable is a string.
    – wdkrnls
    Dec 2 '14 at 2:36

Combining string-edit with a bit of custom elisp can provide the feature you want. For example, the function below will enable you to edit the value of a variable in string-edit-mode, provided that this variable is string-valued.

(defun my/edit-string-variable-at-point ()
  "If the symbol at point is a string-valued variable, update it
using `string-edit' to provide the new value."
  (let ((variable (intern (thing-at-point 'symbol t))))
    (with-current-buffer (get-buffer-create "*edit-variable*")
      (print `(setq ,variable ,(symbol-value variable))
      (goto-char (- (point-max) 3))
      (if (string-edit-at-point)
          ;; Variable value is a string
          ;; Overrides C-c C-c so that the new value is evaluated after `string-edit-conclude'
          (let ((oldmap (cdr (assoc 'string-edit-mode minor-mode-map-alist)))
                (newmap (make-sparse-keymap)))
            (set-keymap-parent newmap oldmap)
            (define-key newmap (kbd "C-c C-c")
              #'(lambda ()
            (make-local-variable 'minor-mode-overriding-map-alist)
            (push `(string-edit-mode . ,newmap) minor-mode-overriding-map-alist))
        ;; Variable value was not a string
        (message "`%s' is not a string!" variable)

If they are user options, then M-x customize-option.

If they are defvars, not options, then edit the value in the source buffer or in a copy of the defvar in another buffer.

Or use M-: (setq THE-VAR "edit the text in the minibuffer).

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.