Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

22
votes
2answers
4k 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.
15
votes
1answer
2k 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 ...
12
votes
4answers
1k views

Named string substitution?

I often have to make several substitution of the same string: (format "%s %s %s" "a" "a" "a") ;; gives: "a a a" (it's just a dummy example, in this case it's better to glue "a" with a whitespace, ...
12
votes
2answers
344 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
1answer
3k 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?
10
votes
2answers
1k views

Heredoc or equivalent multiline string syntax in Elisp?

In common-lisp we have the library cl-heredoc, is there an equivalent in EmacsLisp?
9
votes
4answers
425 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 ...
7
votes
2answers
473 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") ; "...
7
votes
3answers
83 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 ...
6
votes
2answers
1k 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 ...
6
votes
3answers
866 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 ...
6
votes
3answers
2k 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 ...
5
votes
2answers
822 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....
4
votes
1answer
78 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
1answer
152 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 ...
4
votes
1answer
475 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) (...
4
votes
0answers
73 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
1answer
903 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
2answers
201 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
2answers
306 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
1answer
58 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
3answers
142 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
0answers
60 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 ...
2
votes
3answers
491 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
2answers
482 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 ...
2
votes
2answers
272 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
4answers
153 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)) '(...
2
votes
1answer
593 views

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

I want to capitalize letters that follow colons. Here is the code: (replace-regexp-in-string ": \w" 'upcase ": really") The result I expected is : Really. Instead I get : really. Similarly, this ...
2
votes
2answers
61 views

How can I use a char as a counter?

I have this LaTeX code: \begin{enumerate}[a] \affiliation{...} \affiliation{...} \affiliation{...} ... \affiliation{...} \end{enumerate} and, using a script written in Emacs Lisp, I want to ...
2
votes
1answer
159 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'" ...
2
votes
2answers
85 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 ...
2
votes
2answers
703 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 ...
2
votes
1answer
89 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:...
2
votes
1answer
103 views

Concat in Org Easy Templates [duplicate]

I have a variety of org easy templates. I have a new file template which includes a #+DATE header that I'd like to populate with the current date [at the time the template is called]. The ...
2
votes
2answers
79 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 ...
2
votes
1answer
48 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 ...
2
votes
1answer
71 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 ...
2
votes
1answer
72 views

Upcase whole buffer but ignore quoted strings

I want to type everything in lowercase, but have a command to turn the whole buffer into uppercase except for quoted strings. What is the command to tell emacs to ignore quoted strings?
2
votes
2answers
103 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 ...
2
votes
1answer
24 views

repeat string-inflection command

I would like to repeat M-x string-inflection-camelcase on several words. For example in: foo_bar baz qux-quux quuz CORGE_GRAULT GARPLY I place my cursor on foo_bar and then enter M-x string-...
2
votes
2answers
38 views

Arbitrary length lists as argument for interactive function

Say I have a defun with a dolist inside that I want to be able to pass a string that is then converted to a list with a length equal to the number of words in said string via the minibuffer, so ...
2
votes
1answer
272 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 ...
2
votes
1answer
196 views

how to parameterize prompt for interactive/command?

background I had elisp code (in this very small git repo) like (defun bulk-replace-file (filepath) (interactive "FPath to file to bulk-replace: ") (bulk-replace-file-non-interactive filepath) ) ...
2
votes
0answers
50 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
0answers
29 views

Collapse xml-node-children from `xml.el` into a string

An API I have used for years (via an elisp package) recently started inserting formatting tags (particularly for <i></i> italics) into the 'ArticleTitle' field of entries. The assumption ...
2
votes
0answers
293 views

In Emacs Lisp, how to convert roman numerals to an integer?

In Emacs Lisp, how to write an Emacs Lisp function to convert a natural number from roman numerals representation to the equivalent Emacs Lisp integer value? The Emacs Lisp function should be callable ...
1
vote
3answers
100 views

Simple way to test if string contains float eg. “8.3”

I thought (defun string-integer-p (string) was cool, I found it in the emacswiki. Is there a variant of it I can use to test if string contains float eg. "8.3"
1
vote
3answers
61 views

Function that does `M-x replace-regexp` or `M-x occur` with a fixed pattern and replacement strings

I often use M-x replace-regexp and M-x occur with some fixed regexp pattern and replacement string on many files. Doing it manually by opening each file in an Emacs buffer and hitting the keys is ...
1
vote
1answer
61 views

How to know output type

I've got this code: (defun insert-random-uuid () (interactive) (shell-command "uuidgen" t)) which produces a uuid that looks like this ffc715d2-1e10-4faf-b1f6-f85cae88a7d7. However, I believe it'...
1
vote
1answer
131 views

Is there a string manipulation difference between Emacs 24 and 25.1?

I'm test-driving Emacs 25.1 and have noticed that a simple concat command, which used to work in v.24 (and v.23), doesn't work in v.25.1: (setq yas-snippet-dirs (concat user-emacs-directory "snippets"...