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:
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."
(defun insert-line-above ()
"Insert an empty line above the current line."
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
or asking Emacs itself.
To see what a particular keybinding does, you can use C-h k
(describe-key). You were ...
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 ...
The interactive special form provides the easiest way to get input from a user.
(defun td (variable)
(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 ...
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 ...
An example of what you are trying to do is contained in the manual.
You need either with-current-buffer or save-current-buffer:
(insert (propertize (format-time-string "%H:%M:%S")
'face '(:height 4.0 :inverse-video t))))
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."
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 ...
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 ...
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.
Bind this to some key.
(defun foo ()
"Replace sexp before point by result of its evaluation."
(let ((result (pp-to-string (eval (pp-last-sexp) lexical-binding))))
(delete-region (save-excursion (backward-sexp) (point)) (point))
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
See also (info "(elisp) Writing to Files").
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.
I am using org-entities to input non-...
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.
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 ...
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 ...
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 ...
You could use this version of the function:
(defun journal-entry ()
(let* ((daily-name (format-time-string "%Y-%m-%d)"))
(journal-path "~/Dropbox (Personal)/journal/")
(journal-file (concat journal-path daily-name ".md")))
(unless (file-exists-p journal-file)
(insert "JOURNAL" "\n" ...
Using quail-define-rules is overkill (note the rules, plural, in the function name) — an alternative is quail-defrule (one rule...)
Directly from my scratch buffer
(quail-defrule "á" ["something"])
— note that it's not (quail-defrule "á" "something"), to do what you want the string must be an element of a vector (ref. C-h f quail-defrule RET)...
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 ...
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)
(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 ...
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)))
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 ()
(lambda (char shifted-char)