Skip to main content
25 votes
Accepted

How to strip decorations (text properties) from a string?

[Your propertized string looks wrong - perhaps you copied it wrong. foo has only 3 characters, so it is impossible for it to be fontified on chars 0 to 4 (i.e., chars 0, 1, 2, and 3 - that's 4 chars)....
Drew's user avatar
  • 78.5k
17 votes
Accepted

How to wrap a single string literal across multiple lines?

Yes, you can use a literal string that does not include the newline, so that the newline shows only for your code. Use \ to escape the newline, so that it is not included in the string: (setq foo "...
Drew's user avatar
  • 78.5k
17 votes

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

What's the idiomatic (or best) way to trim surrounding whitespace from a string? The built-in library subr-x.el has included the inline functions string-trim-left, string-trim-right, and string-trim ...
Basil's user avatar
  • 12.5k
9 votes

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

There is the string manipulation library s.el where trimming whitespace and newlines at the beginning and the end of a string is implemented as function s-trim. I cite that function here with its ...
Tobias's user avatar
  • 33.5k
9 votes
Accepted

Filter a list of strings to create a separate list of those that match a given prefix

(seq-filter (apply-partially #'string-prefix-p "a") '("apple" "pear" "grape" "apricot")) If you have other similar questions, consult the ...
shynur's user avatar
  • 5,795
8 votes

How to wrap a single string literal across multiple lines?

Edit, it seems multi-line literals are supported, keeping answer since it may be useful still. You can however use concat. eg: (concat "hello" "world")
ideasman42's user avatar
  • 9,027
8 votes

Split line every n characters

For occasional use, you can do with query-replace-regexp (C-M-%). Replace .\{32\} with \&^J. This means, replace all chunks of 32 characters, with the same thing followed by a newline. To type ...
Juancho's user avatar
  • 5,475
8 votes
Accepted

Regex for the last line of a string

$ matches at the end of a line, not the end of a string. If you want to match at the end of a string you need to use the \' operator: (string-match "\n.*\\'" "\n \n \n ") => 4 ...
rpluim's user avatar
  • 5,385
7 votes
Accepted

Single function to return file contents as a string

There is not. You can report-emacs-bug and suggest that one be included. One reason I can think of for not including such a function is that there are different reasonable ways to handle errors. E.g. ...
Tianxiang Xiong's user avatar
7 votes
Accepted

How to programmatically surround a string with escaped double-quote

You might be thinking of prin1-to-string: (prin1-to-string "foo") => "\"foo\"" You probably need some protection though (ensure it's a string, strip text properties, ...
ebpa's user avatar
  • 7,559
7 votes

How to check if a given string is a substring of an element of a list

The easiest way is to use the Common Lisp compatibility layer: (require 'cl-seq) (cl-member "ap" '("foo" "apa" "bar") :test #'string-match) ==> ("apa" "bar") PS. This is not directly relevant to ...
sds's user avatar
  • 6,174
7 votes
Accepted

Difference between (quote string) and "string"

'list and "list" are different datatypes: ELISP> (type-of 'list) symbol ELISP> (type-of "list") string Note that they even show up differently in your example -- it's the difference between ...
zck's user avatar
  • 9,142
6 votes
Accepted

Split a complicated string?

Behold: (defun fancy-split (input) (let (tokens) (with-temp-buffer (insert input) (goto-char (point-min)) (while (not (eobp)) (cond ((looking-at "{") ...
wasamasa's user avatar
  • 22.3k
6 votes
Accepted

Set difference for sets of strings

It is for the same reason that: (eql "foo" "foo") => nil Along with the other cl-lib sequence functions generally, set-difference (aka cl-set-difference) defaults to using eql for its equality ...
phils's user avatar
  • 52.4k
6 votes
Accepted

How can I convert a string form of a list to an actual list?

ELISP> (read "(a b c)") (a b c) ELISP> (read "(9 . 3)") (9 . 3) If by (9 . 3) you mean you'd like a cons, then my answer would work. Note however if you actually would like ...
TerryTsao's user avatar
  • 1,236
5 votes

How to check if a given string is a substring of an element of a list

The function that comes to my mind at least would be cl-member using a string-match-p test. Here's a short example that shows how to use it: ELISP> (setq testing '("an apple" "a pear" "a grape")) ...
Xaldew's user avatar
  • 1,261
5 votes
Accepted

Return nth line from (current) buffer

This could be done with the next function (with or without narrowing): (defun get-nth-line (number) "Get line with NUMBER" (save-restriction (widen) (save-excursion (goto-line ...
Konstantin Morenko's user avatar
5 votes
Accepted

How to concatenate a string and a list?

AFAIK the canonical way to turn a list into a string is with mapconcat, e.g. (defun concat-string-list (str xs) (concat str " " (mapconcat #'symbol-name xs " ")))
Stefan's user avatar
  • 26.6k
5 votes
Accepted

Getting characters from current position to end of line as a substring

I don't believe there is a standard elisp function to do exactly what you ask, but it isn't hard to compose your own. Here is my stab at what you describe: (defun gw-string-to-end-of-line () "...
Gina White's user avatar
5 votes

How to convert string to ascii or unicode hex?

(defun hexify-string (strg) (mapconcat (lambda (c) (format "%x" c)) strg ""))
Phil Hudson's user avatar
  • 1,798
5 votes
Accepted

Emacs 29 docstring single quote escaping rules, compiler level event?

Documentation is: C-hig (elisp)Keys in Documentation C-hig (elisp)Text Quoting Style In short, the text-quoting-style feature introduced in Emacs 25 made it impossible to use certain characters in ...
phils's user avatar
  • 52.4k
4 votes
Accepted

What is the best way to search forward/backward for a token?

You used tag isearch so presumably you want to search interactively, not using Lisp. And you used tag words; you apparently want to search for a whole word. In that case, just use M-s w, which does ...
Drew's user avatar
  • 78.5k
4 votes
Accepted

Interpolate environment variables in string

I think you're looking for the function substitute-env-vars: substitute-env-vars is a compiled Lisp function in ‘env.el’. (substitute-env-vars STRING &optional WHEN-UNDEFINED) Substitute ...
Basil's user avatar
  • 12.5k
4 votes
Accepted

match-data fails to consider only last search with string-match and persists across sessions

The match object you're accessing is global and may be changed by any piece of lisp code. Therefore you should immediately check its state. The following piece of code shows how and exhibits the ...
wasamasa's user avatar
  • 22.3k
4 votes

Sort lines in a string

Here is one possible way to implement this using a temporary buffer: (defun sort-string (string &optional reverse) "sorts an input string, using a temporary buffer" ;;(interactive "sto be ...
jue's user avatar
  • 4,666
4 votes
Accepted

Non-hex digit used for Unicode escape error when setting org-latex-classes

I just found out what's wrong. Elisp requires a backslash in a string (i.e., between double-quotes "") to be escaped in Lisp, so the following code works: (setq org-latex-classes '(("article" ...
Sati's user avatar
  • 825
4 votes
Accepted

Different backslash behaviour between interactive regexps and code regexps

Elisp regexps are represented as strings, which means backslashes are interesting, as they are not only special to regexps, but also when writing strings. Emacs requires a literal \ character to be ...
phils's user avatar
  • 52.4k
4 votes

Colored string in mode line

@NickD provided a good answer: use a face. OP's comment to Nick's answer says that he'll try to write a function that, given a string, returns a propertized string. Such functions already exist: ...
Drew's user avatar
  • 78.5k
4 votes
Accepted

org-babel-tangle error

A good tool for finding errors in Org mode files is org-lint: just visit the file, say M-x org-lint and fix any problems it finds. There is no guarantee that it will find everything, so you might ...
NickD's user avatar
  • 32.5k

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