Questions tagged [string]
A string is a sequence of characters, which can be user data or used internally in Emacs. For example, string to search, replace, save in a register and so on, file name, etc.
155
questions
38
votes
3
answers
13k
views
How to check in elisp if a string is a substring of another string?
How to check if a string s1 is a substring of another string s2?
For example (test-substring "f t" "df tj") --> t,
(test-substring "ft" "df tj") --> nil.
28
votes
1
answer
6k
views
How to strip decorations (text properties) from a string?
By a "decorated string" I mean something like
#("foo" 0 4 (fontified t face font-lock-function-name-face))
...as opposed to the "plain string"
"foo"
(If Elisp code reads a string directly from a ...
22
votes
1
answer
7k
views
Get content of a buffer
So buffer-string gets the content of the current buffer. But it doesn't allow specifying other buffers.
How can I get around that? Do I need something like save-window-excursion to make it work?
17
votes
2
answers
3k
views
Heredoc or equivalent multiline string syntax in Elisp?
In common-lisp we have the library cl-heredoc, is there an equivalent in EmacsLisp?
14
votes
3
answers
7k
views
How to wrap a single string literal across multiple lines?
Is it possible to write a single line for an Elisp string:
"hello world"
As:
"hello
world"
Without inserting a newline character? (wrapping a string literal).
I read that adding a tilde at the ...
12
votes
2
answers
562
views
How do i get rid of "default" (last used) string in some emacs functions?
I have the following problem : when i use 'C-x r t' (string-rectangle), i am prompted to enter some string, to replace the selected rectangle. Lets say i type ";;" (to comment out the rectangle). This ...
11
votes
3
answers
4k
views
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
I'm working with strings which may have any number of prefix and suffix spaces, tabs, newlines, etc. Currently I have this:
(replace-regexp-in-string
"^[^[:alnum:]]*\\(.*\\)[^[:alnum:]]*$"
"\\1" ...
9
votes
2
answers
3k
views
How can I turn a string into its literal representation?
(mystery-function "a neat\nstring\"")
=> "\"a neat \\nstring\\\""
This question was asked on freenode#emacs by user dropdrive. How could I write (or use) a function that takes a string literal ...
9
votes
4
answers
595
views
Which keyboard shortcut to use for navigating out of a string
When I'm in the middle of a looooong string, like the following
(setq Emacs-beta "Which keyboard shortcut to use for navigating out of a string")
Can I skip out of it, just before the first " (after ...
9
votes
2
answers
1k
views
Interpolate nil argument to `format` as the empty string?
I understand that this is trivial with an if, but is there an option, like %S or %s that interpolates nil as no string at all?
Example:
(format "%?.el" nil) ; ".el"
(format "%?.el" "beginner") ; "...
8
votes
3
answers
333
views
Split a complicated string?
Q: how do I split a complicated string when whitespace
delimiters aren't discriminating enough?
Background
I'm working with BibTeX files. I want to split an author string
of the form "first-name ...
7
votes
4
answers
3k
views
How to check if a given string is a substring of an element of a list
According to documentation, member checks if a a given element is an element of a list. For example if the list, say my_list consists of apple and orange, (member "apple" my_list) returns true.
Is ...
7
votes
2
answers
2k
views
Split a string without consuming separators?
I frequently need to split strings, while keeping the separator. Researching the elisp manual, I am not finding a way to split a string according to a separator, without consuming the separator itself....
6
votes
1
answer
2k
views
Single function to return file contents as a string
I'm wondering if there's a terse (i.e. single function) approach for reading a file's content given its path and returning it as a string.
My go-to has been:
(with-temp-buffer
(insert-file-...
6
votes
1
answer
2k
views
How can I convert a string form of a list to an actual list?
I would like to convert these two strings
"(a b c)"
"(9 . 3)"
to these
(a b c)
(9 . 3)
I'd had some luck with the first one evaluating this,
(mapcar 'intern (split-string (string-...
5
votes
1
answer
290
views
Set difference for sets of strings
Why the following in emacs-lisp works as expected:
(set-difference '(1 2) '(1))
=> (2)
But if strings used it doesn't:
(set-difference '("foo" "bar") '("foo"))
=> ("foo" "bar")
How do can I ...
5
votes
0
answers
144
views
Paired string delimiters?
Red and Rebol have multiline strings that start with { and end with }. Reading through Elisp docs I don't see any simple way to define this in the syntax table. The string quotes class matches the ...
4
votes
2
answers
399
views
Filter a list of strings to create a separate list of those that match a given prefix
If I have a list, (“apple” “pear” “grape” “apricot”) how do I create a new list with elements starting with the letter “a”? The new list will be: (“apple” “apricot”).
4
votes
2
answers
2k
views
Emacs 29 docstring single quote escaping rules, compiler level event?
I've seen the following warning from the Emacs 29 compiler:
In s-format:
s.el:614:2: Warning: docstring has wrong usage of unescaped single quotes (use
\= or different quoting)
I haven't been ...
4
votes
1
answer
2k
views
Regex for the last line of a string
I'm trying to search for the last line in a string (lines separated by \n) using string match but I cannot make it work as intended. The code below evaluates to 0 while I would have expected 4. I ...
4
votes
2
answers
309
views
How do I format dates without padding the values?
I was not very statisfied with the leading zero that I got in my insert-date function.
It looks now as 09 jun 2015, instead 9 jun 2015.
To solve this, I looked into the source code of s.el and ...
4
votes
1
answer
743
views
Interpolate environment variables in string
I am trying to write a function that will interpolate the enviroment variables stored in process-environment in a string. For example, if I have the string "This is $Foo and ${Bar}_none and '$skip'" ...
4
votes
1
answer
160
views
Width of string as if it were displayed
From the documentation of string-width string:
This function returns the width in columns of the string string, if it were displayed in the current buffer and the selected window.
For me this ...
4
votes
1
answer
1k
views
prettify-symbols-mode and multi-character replacements?
With prettify-symbols-mode it is possible to replace strings by a character, or a character composition, e.g.
;; \lambda → λ
(push '("\\lambda" . ?λ) prettify-symbols-alist)
Optionally, it allows ...
4
votes
0
answers
291
views
truncate-string-to-width doesn't pad/print the string, as documented
I am referring the documentation for truncate-string-to-width from here.
It says,
(truncate-string-to-width "\tab\t" 12 4)
will output,
ab
But what i get in my machine (emacs versioned 25.1.50.1)...
3
votes
3
answers
864
views
How to modify a string without altering its text properties
If I propertize a string and save it to a variable, how can I change the string within that variable without altering its text properties? For example:
(setq myvar (propertize "testing"
...
3
votes
2
answers
2k
views
Split line every n characters
I have a long binary string that I would like to split every 32 characters. Is there a command that can do this for me?
I have tried word wrapping, but it splits per word. It has no effect since a ...
3
votes
4
answers
208
views
Replace all literal substrings from a list in a string
My goal is to replace all literal sub-strings in a given string.
(defun translate (str)
(concat (mapcar (lambda(cons)
(replace-in-string (car cons) (cdr cons) str))
'(...
3
votes
1
answer
2k
views
Set a variable to a Windows path in init file
I want to use python in spacemacs on window OS, and need to tell it where to find python shell. So I added one line of
(setq-default python-shell-interpreter "C:\Anaconda\Scripts\ipython.exe")
to ...
3
votes
2
answers
1k
views
Use emacs for batch-processing files
I'm a newbie elisp user. I would like to process a few hundred xml files and call an elisp function for each line which does an query-replace to fix a problem in these files.
My question is: how do I ...
3
votes
2
answers
399
views
How to split the output of a `printenv` shell command?
Basically, I want to split a multiline string at the regular expression that, in Perl, would be specified with \n(?=[^\W\d]\w*=).
Note that the Perl regexp features a "zero-width lookahead match" ((?=...
3
votes
1
answer
1k
views
Non-hex digit used for Unicode escape error when setting org-latex-classes
When I try to set org-latex-classes with the following code:
(setq org-latex-classes
'(("article"
"
\\documentclass[12pt,a4paper]{article}
\usepackage{xeCJK}
%\usepackage[heading]{...
3
votes
2
answers
405
views
What is the best way to search forward/backward for a token?
Sometimes I want to search for a specific word but I don't want to find words which have my search string as a substring.
For example, if I search for "pan" (bread, in Spanish), I don't want to find ...
3
votes
1
answer
1k
views
Regular expression matching group replacement not working
I want to convert
.waitForVisible('.someselector')
.click();
Into:
.waitAndClick('.someselector');
Automatically in a lot of files. So I wrote:
(defun hhaamm-replace-regexp-in-buffer (from to)
(...
3
votes
1
answer
48
views
Strange behaviour involving `display` property
I noticed a rather peculiar behavior when using the display property (using emacs 28.2). I tried to boil things down to a minimal example: The code
(defun finsert ()
(insert (propertize ".&...
3
votes
2
answers
307
views
Evaluate parts inside text-property string using read syntax?
I just discovered that it is possible to define strings with text properties using the special read syntax. Now I'm trying to evaluate a value of a property within the string definition so something ...
3
votes
1
answer
92
views
Strange behaviour of match-string/string-match
I have this code
(defun string-match-test ()
(interactive)
(string-match "12345" (buffer-string))
(setq STRING (match-string 0))
(read-string STRING)
(goto-char (point-max))
(insert ...
3
votes
1
answer
251
views
Match regexp groups or entire text
According to match-string help:
Value is nil if NUMth pair didn't match
In fact:
(string-match "\\(hullo\\)" "hello")
(match-string 1 "hello")
gives nil
Also, a NUM
Zero means the entire ...
3
votes
2
answers
406
views
Convert a word into an acronym
I have a buffer with this line of text:
This is a random test
I want to convert test in T.E.S.T., like this:
This is a random T.E.S.T.
So far, I came up with this function:
(defun mu-convert-...
3
votes
1
answer
110
views
Alternative to `file-name-extension` that will include `~`
I am looking for an alternative to file-name-extension that will include an ending ~ if there is one. For example, if there is a file named foo.el~, the result should be el~.
Rather than using the ...
3
votes
1
answer
444
views
Normalize string to make it file/path safe
Often I find my self doing this
C-x C-f to create a file, while in the mini buffer prompt,
paste a url like "https://github.com/seamusabshere/cache_method/issues/15" from the clipboard.
Since "https:...
3
votes
1
answer
259
views
modify syntax table for strings in perl modes
My company uses a version of Perl where you can define by binary number using something like my $var = 'b010110110; etc. Problem is both perl-mode and cperl-mode interpret ' as the start of the string ...
3
votes
3
answers
255
views
Is there a way to autoindent single quoted lines automatically for python code?
The python.el mode has several ways to fill docstrings, but I would like to fill single quoted lines. Is there a function that would change this:
variable = {
'key':
' this is a very long ...
3
votes
0
answers
137
views
Calculate current-column including display properties
I am trying to calculate the width of text within either a buffer or a string after it has been displayed, i.e. after taking into account the width of any display properties, such as embedded images, ...
2
votes
3
answers
734
views
Insert one character 2 chars before end of string
For creating Zim wiki pages, I need to generate a timestamp that ends with a time zone in ±hh:mm format. format-time-string has a %z format string that can insert it without the colon. Right now I use ...
2
votes
2
answers
1k
views
How to include a text verbatim in Emacs Lisp?
How to programmatically insert a text verbatim?
The following function inserts ocumentclass and not \documentclass.
(defun foo () (interactive)
(insert "\documentclass"))
Of course one can use
(...
2
votes
1
answer
282
views
Difference between (quote string) and "string"
Experiment on interative ielm
ELISP> (print 'list)
list
list
ELISP> (print "list")
"list"
"list"
Any differences with the two results? by the way, one extra times printed.
2
votes
1
answer
2k
views
How to programmatically surround a string with escaped double-quote
It seems that my question should be common knowledge, and I thought I knew it (at some point in the distant past), but the answer escapes me tonight. I'd like to take a string like "foo" and ...
2
votes
3
answers
533
views
How can I check whether a string represents a number in Elisp?
I want to check whether the string variable s is a number (integer or float) formatted as a string. I thought it could be done using string-to-number like this
(defun string-number-base-p (s)
(...
2
votes
1
answer
134
views
How to get the string representation of a keymap event?
I'd like to get the string representation of a keymap event. For instance 19 should be printed as C-s, 1 as C-a etc...
I could make my own function to do that but since describe-keyis capable of ...