Hot answers tagged

30 votes
Accepted

Get content of a buffer

A lot of things in Emacs operate on the current buffer. You need to change the current buffer and restore it when you're done. Use with-current-buffer for simple cases where you just need to do ...
user avatar
24 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)....
user avatar
  • 69.1k
20 votes

How to check in elisp if a string is a substring of another string?

cl-search can do that (and also returns the index of the substring, if found): ELISP> (cl-search "f t" "df tj") 1 (#o1, #x1, ?\C-a) ELISP> (cl-search "ft" "df tj") nil
user avatar
  • 5,786
18 votes
Accepted

How can I turn a string into its literal representation?

You're looking for prin1-to-string. Also, set print-escape-newlines to a true value to get the "\n" behavior you specified. I.e.: (let ((print-escape-newlines t)) (prin1-to-string "foo\nbar")) =&...
user avatar
14 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 "...
user avatar
  • 69.1k
12 votes
Accepted

Which keyboard shortcut to use for navigating out of a string

Emacs 25 or newer You want C-M-u which runs the command backward-up-list. Move backward out of one level of parentheses. This command will also work on other parentheses-like expressions ...
user avatar
  • 5,422
11 votes

How to include a text verbatim in Emacs Lisp?

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 ...
user avatar
11 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 ...
user avatar
  • 11.3k
9 votes
Accepted

Interpolate nil argument to `format` as the empty string?

Depending on your application, concat might be of use: (concat "live long " nil "and prosper") ;; => "live long and prosper" This works because concat acts on sequences, and nil is an empty list.
user avatar
  • 22.3k
8 votes

How can I turn a string into its literal representation?

This is not exactly the purpose it was invented for, but seems to be useful: (require 'json) (json-encode-string "a neat\nstring\" with tab\t and feed \f, also vertical tab \v") "\"a neat\\nstring\\\"...
user avatar
  • 10.8k
8 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 ...
user avatar
  • 29.8k
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 ...
user avatar
  • 4,059
7 votes
Accepted

Set a variable to a Windows path in init file

In Emacs Lisp, backslashes inside strings start escape sequences. For example, \n means a newline, and \a means a bell character. If a string contains an unknown escape sequence, an error is ...
user avatar
  • 5,786
7 votes
Accepted

Heredoc or equivalent multiline string syntax in Elisp?

Ordinary Emacs Lisp strings are multi-line-capable. You can simply put newlines in them. Glancing at cl-heredoc, it sounds like what you are looking for is "raw" strings. There was a proposal to ...
user avatar
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 ...
user avatar
  • 8,536
6 votes

Interpolate nil argument to `format` as the empty string?

The special form or is useful here. This macro returns the value of the first argument, unless it's nil in which case it returns the second. So, assuming the variable you want to check is foo, the ...
user avatar
6 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 ...
user avatar
  • 5,427
6 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")
user avatar
  • 7,069
6 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 ...
user avatar
  • 5,005
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 "{") ...
user avatar
  • 21k
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 ...
user avatar
  • 43.1k
5 votes

Split a string without consuming separators?

Here's some code for you. This is a slightly modified split-string. I've removed trim option for simplicity and added keep-sep option. The diff is basically 2 lines, so you could say that this code is ...
user avatar
  • 13.6k
5 votes
Accepted

Capitalizing letters that follow colons using 'replace-regexp-in-string'

You need to double the backslash in a Lisp string: (replace-regexp-in-string ": \\w" #'upcase ": really") See the Elisp manual, node Syntax for Strings. And a char class has chars inside [], so put ...
user avatar
  • 69.1k
5 votes

How to know output type

Use (type-of EXPRESSION) to determine the type of an expression. In your case you are confusing the value being inserted into a buffer by a function with its return value. shell-command inserts the ...
user avatar
  • 43.1k
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")) ...
user avatar
  • 1,221
5 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. ...
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 " ")))
user avatar
  • 25.2k
5 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 ...
user avatar
  • 920
4 votes
Accepted

Split a string without consuming separators?

Perhaps a shorter example, using cl library: (defun chop (string separator) (cl-loop with seplen = (length separator) with len = (length string) with start = 0 with ...
user avatar
  • 10.8k

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