My problem is that every line executing on the remote Linux machine seems to be including a trailing ^M (as is shown as ? in the file name apa? below).

Content of my .org document:

* Test
#+BEGIN_SRC sh :dir /plink:pi@raspberry:/home/pi/test
touch apa
touch bepa && echo
ls -al

: total 8
: drwxr-xr-x  2 pi pi 4096 Nov 23 21:35 .
: drwxr-xr-x 10 pi pi 4096 Nov 23 20:07 ..
: -rw-r--r--  1 pi pi    0 Nov 23 21:35 apa?
: -rw-r--r--  1 pi pi    0 Nov 23 21:35 bepa

If I ssh to my Linux machine and type the file name using tab completion I see

pi@raspberry:~/test $ ls apa^M

Running plink directly from a cmd.exe does not add ^M to the filename:

c:\>plink.exe -l pi raspberry "cd test && touch cepa"

I have saved the org document with Unix line endings, but I get the same result.

My current workaround is to add the && echo to eat the ^M, but I'd really like to find a better one...

I'm running Emacs 25.1 and org-mode v8.3.6 on Windows 10. My Linux machine is a raspberryPi running raspian jessie.

  • 1
    You can try to run the delete-trailing-whitespace command
    – Jules
    Nov 23, 2016 at 21:19
  • Running delete-trailing-whitespace did not change my document, nor the resulting filename...
    – Amoork
    Nov 23, 2016 at 21:24

3 Answers 3


It seems that org-babel executes code blocks by dumping them to a temporary file and then loading the file into whatever interpreter is to be used for the designated language (in this case, a shell). When you specify a remote :dir argument, the temporary file is placed on the remote host using TRAMP via with-temp-file.

Unfortunately it seems this file doesn't inherit the coding system of the org buffer (though I guess that wouldn't make sense anyway). Nor is its coding system autodetected from the OS running on the remote host (which probably would make sense).

I don't know what the best way to solve this problem is, but it should suffice to set the global default coding system so that new buffers automatically follow unix EOL conventions:

(setq-default buffer-file-coding-system 'utf-8-unix)

As an Emacs user on Windows, this might prove inconvenient for you in other situations, though.

  • Thanks for the thorough explanation. The suggestion of using (setq-default buffer-file-coding-system 'utf-8-unix) is a much less obtrusive work-around. I tried making a macro to toggle that variable but since it "Automatically becomes buffer-local" I have not figured out how to get the current default-value...
    – Amoork
    Nov 24, 2016 at 15:31
  • 1
    @kini Great suggestion, thanks! Setting buffer-file-coding-system did the trick for me. Aug 16, 2021 at 14:14

Thanks to the explanation from kini I have now created a satisfactory solution by adding a parameter to org-babel-sh-evaluate:

#+BEGIN_SRC sh :file-coding utf-8-unix :dir /plink:pi@raspberry:/home/pi/test
touch apa
touch bepa
ls -al

using the following defadvice:

(defadvice org-babel-sh-evaluate (around set-shell activate)
  "Add header argument :file-coding that sets the buffer-file-coding-system."
  (let ((file-coding-param (cdr (assoc :file-coding params))))
    (if file-coding-param
        (let ((file-coding (intern file-coding-param))
        (default-file-coding (default-value 'buffer-file-coding-system)))
          (setq-default buffer-file-coding-system file-coding)
          (setq-default buffer-file-coding-system default-file-coding))

Does using the DOS cr+lf (^M^J) in an org file actually have a benefit? I guess you will only open it in Emacs anyway. (Other editors should be able to come to terms with unix lf (^J) line endings too.)

To just set the encoding for this one file, do

 M-x set-buffer-file-coding-system <ret> utf-8-unix <ret>

and if necessary remove any ^M with

 M-x replace-string <ret> C-q C-m <ret><ret>

then of course save the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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