# Weird output with REPL escape codes

**URL:** <https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772>\
**Category:** General Usage\
**Created:** [September 4, 2022, 8:48pm UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772 "2022-09-04T20:48:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 4, 2022, 8:48pm UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772/1 "2022-09-04T20:48:06Z")

</div>

I’m trying to work with the raw output from a Julia process but I don’t understand even a simple version of it.

I tried `julia --banner=no | tee julia.txt`, then typed `1+2` and enter. The file `julia.txt` then looks like this (I replaced escape characters with `\e` here:

```julia
\e[?2004h
\e[0Kjulia> 
\e[7C
\e[7C1+2
\e[0Kjulia> 
\e[7C1+2
\e[10C
\e[?2004l3

\e[0K
\e[0Kjulia> 
\e[7C
\e[7C\e[?2004h
\e[0Kjulia> 
\e[7C
\e[7C
\e[?2004l

```

However, the visual output is

```julia
julia> 1+2
3

julia>

```

I don’t get how the ANSI escape codes lead to that result. `\e[0K` means “delete from cursor to end of line” and `\e[7C` means "move cursor forward 7 steps. The output has `julia>` two times, but in the raw output it’s 4 times. However, the cursor only ever moves forward, and only ever deletes to the end of the line. How does it work out to the final result then?

One more peculiarity, when I do `cat julia.txt` I get the correct output. However, when I do `echo 'paste the content of the file here'` I get:

```julia
julia> 

       1+2
julia> 
       1+2

3

julia> 

julia> 

```

And this is the output I would expect if I trace the ansi escape code commands in there. Where’s the problem in my logic?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [September 4, 2022, 8:52pm UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772/2 "2022-09-04T20:52:54Z")

</div>

Those are ansi color escape sequences. Try `julia --banner=no --color=no`

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 4, 2022, 8:55pm UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772/3 "2022-09-04T20:55:17Z")

</div>

They are not color sequences, they are cursor movements. The color sequences end in `m`, Julia already disables color by default if you use it like this.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 4, 2022, 10:45pm UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772/4 "2022-09-04T22:45:34Z")

</div>

> [@jules](#):
>
> I replaced escape characters with `\e` here

Didn’t you also replace cariage returns with new lines?  
What I get looks like this (with escape characters replaced by `\e` and carriage returns replaced by `\r`):

```julia
\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C1+2\r\e[0Kjulia> \r\e[7C1+2\r\e[10C
\e[?2004l3

\r\e[0K\r\e[0Kjulia> \r\e[7C\r\e[7C\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C
\e[?2004l

```

This makes more sense, because now the CRs make the cursor move backward to the beginning of the line. The `echo` output also agrees with `cat`:

```julia
shell$ echo -e '\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C1+2\r\e[0Kjulia> \r\e[7C1+2\r\e[10C
> \e[?2004l3
> 
> \r\e[0K\r\e[0Kjulia> \r\e[7C\r\e[7C\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C
> \e[?2004l'
julia> 1+2
3

julia> 

```

However, I don’t know why the prompt is repeated twice (but erased right after it’s printed for the first time). An other peculiarity is how the [bracketed paste](https://en.wikipedia.org/wiki/Bracketed-paste) mode sequences (`\e?2004h` and `\e?2004l`) encompass both prompts in the first line, but only one in the second line.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 5, 2022, 8:30am UTC](https://discourse.julialang.org/t/weird-output-with-repl-escape-codes/86772/5 "2022-09-05T08:30:16Z")

</div>

> [@ffevotte](#):
>
> Didn’t you also replace cariage returns with new lines?

Ha, it turns out _I_ didn’t but _VSCode_ did. I had the file open to copy stuff from it, and VSCode replaced every `\r` with `\r\n`. Thanks for the hint!
