I have a string variable, specifically the emacs-version variable that can be quite long and I am only using message with the first 4 of those characters.

Is there a way to format the variable in a message statement so that I am only printing the first x number of characters?

Right now I am using substring but it seems like there should be an easier / cleaner way. Please let me know if there is a more efficient way to do this:

(message "You are running version %s old." (substring emacs-version 0 4)))

C-hf format tells you:

For %s and %S, the precision specifier truncates the string to the given width.


(format "%.4s" emacs-version)

In this case, I'd recommend resorting to emacs-major-version and emacs-minor-version:

(message "emacs version %s.%s" emacs-major-version emacs-minor-version)
  • I wish I could select two answers. This is actually what I want to do, but my question was about substrings so I think the other answer more accurately reflects the answer to the question.
    – Startec
    Jul 29 '16 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.