Hot answers tagged

15

There are some problem with the code: put-text-property is applied to an object. In this case your string. You need to pass it as as the last parameter. put-text-property starts counting at zero. If font-lock-mode is enabled, it will strip any text of the face property. The following piece of code works, if font-lock mode is disabled: (let ((current-...


10

C-<RET>, org-insert-heading-respect-content Other options are listed in the manual


10

The special form which saves and restores the current point and buffer is save-excursion. So you could write your functions as: (defun insert-line-below () "Insert an empty line below the current line." (interactive) (save-excursion (end-of-line) (open-line 1))) (defun insert-line-above () "Insert an empty line above the current line." (...


9

Perhaps something like: (with-current-buffer "*scratch*" (insert "Test")) is what you want.


9

The interactive special form provides the easiest way to get input from a user. (defun td (variable) (interactive "sVariable:") (insert (format "std::cout << \"%s is: \" << %s << std::endl;" variable variable))) Here "sVariable:" consists of the "s" code character (read a string) and the prompt. (See Using interactive in the Emacs ...


9

C-q 377 RET inserts the character with octal code 377 (aka LATIN SMALL LETTER Y WITH DIAERESIS). If you want to insert a byte instead of a character, you can do it with: M-: (insert (unibyte-string #o377)) RET As @legoscia mentioned, Emacs will probably ask you to use another coding system after inserting such a character, but you can choose utf-8 at that ...


9

You may be misinterpreting what's going on. The insert function inserts its argument verbatim. The problem is that the string you've included in your program is not \documentclass but ␡ocumentclass where ␡ is ASCII character number 127 (which is unprintable). The string literal "\documentclass" represents the string ␡ocumentclass. Notice how two things ...


8

An example of what you are trying to do is contained in the manual. You need either with-current-buffer or save-current-buffer: (with-current-buffer destination-buffer (insert (propertize (format-time-string "%H:%M:%S") 'face '(:height 4.0 :inverse-video t))))


7

The insert function will do what you want. Below is a simple function that takes care of some housekeeping and inserts the buffer's name at the beginning of the buffer: (defun do-my-thing () "Insert the name of the buffer at the beginning of the buffer." (save-excursion (save-restriction (widen) (goto-char (point-min)) (insert (...


6

Short version: yes Instead of C-x C-e to evaluate the expression, give it a prefix argument. C-u C-x C-e will print the output to the buffer. How I found this information You can investigate how Emacs does these things by looking in the manual, or asking Emacs itself. To see what a particular keybinding does, you can use C-h k (describe-key). You were ...


5

You can always insert a character literally by typing C-q first (quoted-insert). Many “electric” characters (that's what Emacs usually calls characters whose insertion has extra effects such as inserting extra braces, reindenting, etc.) are designed to revert to a plain insertion if you pass a numeric prefix argument, i.e. type ESC 1 _ or M-1 _ to insert ...


5

global-set-key is just a thin wrapper around define-key, ignoring some error checking it is (defun global-set-key (key def) (define-key (current-global-map) key def)) The documentation for define-key says that the "def" can be a number of things, including a string (treated as a keyboard macro), So then there is the question about what something like ...


5

After some trial and error, I managed to convert ÿ to the byte 377 using M-x recode-region, specifying that it was really in raw-text but was interpreted as latin-1. When saving the file, Emacs didn't want to save it as UTF-8, offering to save it as raw-text instead, which seems to have had the desired effect.


5

Like this (defun example(name) (interactive "sWhat's your name: ") (message "Hello, %s" name)) The key part is the "s" prefix. Use s for plain strings, b for buffers, etc.


5

Bind this to some key. (defun foo () "Replace sexp before point by result of its evaluation." (interactive) (let ((result (pp-to-string (eval (pp-last-sexp) lexical-binding)))) (delete-region (save-excursion (backward-sexp) (point)) (point)) (insert result)))


5

You can use the append-to-file and write-region functions, for example, ~ $ echo hello > file.txt ~ $ emacs --batch --eval '(append-to-file "xyz\n" nil "file.txt")' ~ $ cat file.txt hello xyz ~ $ See also (info "(elisp) Writing to Files").


4

You can use insert-char for interactive usage and (cdr (assoc-string INPUT (ucs-names) t)) for usage in elisp programs. Thereby, INPUT is the character name string, e.g., (setq INPUT "GREEK SMALL LETTER LAMBDA"). Please, see the doc strings of insert-char, ucs-names, and assoc-string for more information. Remark: I am using org-entities to input non-...


4

If your function inserts text into a buffer, it should be called insert-something and its return value should not be passed to format and friends. I.e., rename insert-random-uuid.* to get-random-uuid.* and insert-dateutc.* to get-dateutc.* and remove the insert call from the latter.


4

The reason it doesn't work is: (a) the minibuffer has its own local keymap, (b) M-p is bound in that local map to a different command, and (c) a local keymap takes precedence over the global keymap. To have your command work for M-p in the minibuffer, you need to bind it to M-p in one or more of the minibuffer keymaps. It is probably sufficient to bind it ...


4

What is going on is that string in Emacs have historically been used in this context for 2 different purposes: sequence of characters. sequence of events. In your case, you're writing what you think as a sequence of characters, but it's used in a context where Emacs expects a sequence of events. Since Emacs-19 added support for GUIs, events have become a ...


4

You could use this version of the function: (defun journal-entry () (interactive) (let* ((daily-name (format-time-string "%Y-%m-%d)")) (journal-path "~/Dropbox (Personal)/journal/") (journal-file (concat journal-path daily-name ".md"))) (find-file journal-file) (unless (file-exists-p journal-file) (insert "JOURNAL" "\n" ...


4

Rather than ask M-: to insert the output of the Elisp code, just write the Elisp code that inserts the text you want: Try M-: (insert (format-time-string "%m/%d/%Y")) RET


3

There are several ways. A key difference is how close H-g a is to typing an actual character α would be if you had that key on your keyboard. For example, you can make H-g a a macro that inserts the string α: (define-key global-map (kbd "H-g a") "α") But then H-g a differs from inserting a character in several ways which may or may not be desirable. For ...


3

I'm not sure whether there's an Evil-specific way to do this. However, you can write a short Elisp snippet that does this: (defun my/insert-char (char count) (interactive "c\np") (insert-char char count)) When you map insert-char to a key directly, it will prompt you for the character, which is why I wrapped it in my/insert-char. The function can be ...


3

Here's another solution: (defun my-org-return (&optional arg indent) (interactive "*p") (dotimes (number arg) (org-return indent))) (define-key org-mode-map (kbd "<return>") 'my-org-return)


3

If you just want to insert newlines, without any automation for indentation or support for soft newlines, then you can use quoted-insert (C-q), which lets you enter any character including control characters. A newline character is C-j, but the Return key sends C-m, so you need to type C-u 1 0 C-q C-j (insert a newline, times 10). If you want to call the ...


3

If you want to do an arithmetic operation and insert the value into the buffer, but don't care where you do the operation, then you can also do C-u M-: and type the operation info the minibuffer. This blog has (defun eval-and-replace (value) "Evaluate the sexp at point and replace it with its value" (interactive (list (eval-last-sexp nil))) (kill-...


3

For Emacs setting: (delete-selection-mode t) ; delete seleted text when typing For lisp code, add following line before insert: (if (region-active-p) (delete-region (region-beginning) (region-end)))


2

Keys defined by global-set-key are shadowed by any local binding. In your case using key-translation-map seems more appropriate. Also, when defining keys, you can use a string (interpreted as a keyboard macro) instead of a command. This would greatly simplify your code: (defun my-bind-symbols-to-crtl () (cl-mapcar (lambda (char shifted-char) (...


2

Here's a solution implementing what I've suggested in my earlier comment: (defvar my-last-inserted-character nil "Last character inserted, buffer-local.") (make-variable-buffer-local 'my-last-inserted-character) (defun my-update-last-inserted-character () (setq my-last-inserted-character (concat (this-command-keys)))) (add-hook 'post-self-insert-hook '...


Only top voted, non community-wiki answers of a minimum length are eligible