I'm editing YAML files where I have to frequently encode and decode base64 values in a buffer. base64-encode-region and base64-decode-region are perfect for this, but very long strings get encoded as multiline base64, breaking the YAML syntax.

According to the description of the command, one can somehow disable this behaviour:

base64-encode-region is an interactive built-in function in ‘C source

(base64-encode-region BEG END &optional NO-LINE-BREAK)

Base64-encode the region between BEG and END.
Return the length of the encoded text.
Optional third argument NO-LINE-BREAK means do not break long lines
into shorter lines.

How can I pass this argument when calling the function with (helm-)M-x?


You must add this function to your initialization file (either .emacs.d/init.el or .emacs):

(defun my/base64-encode-region-no-break ()
  (base64-encode-region (mark) (point) t))

This creates a new command named my/base64-encode-region-no-break that you can call with M-x or helm-M-x.


If you copy the following lisp code to your init file and restart emacs you can call base64-encode-region with universal prefix argument C-u to get the base64 encoded string without linebreaks:

  1. Select the region you want to base64 encode
  2. Call C-u M-x base64-encode-region RET
(defun base64-encode-region-prefix-arg (&rest _args)
  "Pass prefix arg as third arg to `base64-encode-region'."
  (interactive "r\nP"))

(advice-add 'base64-encode-region :before #'base64-encode-region-prefix-arg)

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.