# How to remove comments from Elixir code?

**URL:** <https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937>\
**Category:** Questions\
**Tags:** ast\
**Created:** [July 12, 2019, 5:46pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937 "2019-07-12T17:46:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chulkilee](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chulkilee/32/6207_2.png) [@chulkilee](https://forum.elixirforum.com/u/chulkilee)\
**Post date:** [July 12, 2019, 5:46pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/1 "2019-07-12T17:46:00Z")

</div>

I found `Code.string_to_quoted/2` returns quoted expression, but couldn’t find a way to rebuild it to code. Also, it has all location information and complicated nest format… so it’s doable but doesn’t seem “easy” at the first glance (e.g. no documentation on the structure? )

I already have a shell script to remove any line starting with `#` (with optional leading white spaces).. but of course that can break things (e.g. `#` in multiline string)

Any tips?

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [July 12, 2019, 5:58pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/2 "2019-07-12T17:58:33Z")

</div>

Maybe **sed**?

> **[Remove comments from files Using sed](https://www.commandlinefu.com/commands/view/10104/remove-comments-from-files)**
>
> sed -e '/^#/d' -e 's/#.\*$//' in - (Remove comments from files Use sed to remove comments from a file.
> In this example the comments begin with #.
> The command '/^#/d' remove line starting with #.
> The command 's/#.\*$//' remove comments at end of...

or **awk**?

```elixir
awk '!/^ *#/ && NF' file

```

---

<div class="post-metadata">

**Author:** ![chulkilee](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chulkilee/32/6207_2.png) [@chulkilee](https://forum.elixirforum.com/u/chulkilee)\
**Post date:** [July 12, 2019, 9:06pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/3 "2019-07-12T21:06:28Z")

</div>

I’m already using `sed` but I’d like to handle it with AST so that it doesn’t remove markdown header from multistring `@doc`, for example

Here is a command I usually run after `mix new`

```sh
sed -i '/^ *#/d' \
  .gitignore \
  config/*.exs \
  lib/**/*.ex \
  mix.exs \
  test/**/*.ex \
  test/**/*.exs

mix format

```

---

<div class="post-metadata">

**Author:** ![Schultzer](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/schultzer/32/4339_2.png) [@Schultzer](https://forum.elixirforum.com/u/Schultzer)\
**Post date:** [July 12, 2019, 9:53pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/4 "2019-07-12T21:53:35Z")

</div>

You are looking for [Macro — Elixir v1.20.2](https://hexdocs.pm/elixir/Macro.html#to_string/2)

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [July 12, 2019, 9:53pm UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/5 "2019-07-12T21:53:40Z")

</div>

You can use `Macro.to_string/1/2`:

```plaintext
iex(1)> """
...(1)> defmodule M do
...(1)> # a function
...(1)> def foo, do: :foo
...(1)> end
...(1)> """
"defmodule M do\n # a function\n def foo, do: :foo\nend\n"
iex(2)> Code.string_to_quoted(v(1))
{:ok,
 {:defmodule, [line: 1],
  [
    {: __aliases__ , [line: 1], [:M]},
    [do: {:def, [line: 3], [{:foo, [line: 3], nil}, [do: :foo]]}]
  ]}}
iex(3)> v(2) |> elem(1) |> Macro.to_string
"defmodule(M) do\n def(foo) do\n :foo\n end\nend"
iex(4)> IO.puts(v(3))
defmodule(M) do
  def(foo) do
    :foo
  end
end
:ok

```

---

<div class="post-metadata">

**Author:** ![chulkilee](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chulkilee/32/6207_2.png) [@chulkilee](https://forum.elixirforum.com/u/chulkilee)\
**Post date:** [July 13, 2019, 12:02am UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/6 "2019-07-13T00:02:29Z")

</div>

Thanks - but I found it does not return same format as mix format for `.exs`

`remove-comment.exs` file

```elixir
# top-level comment

defmodule RemoveComment do
  # in-module comment

  def run(code_string) do
    # in-func comment
    code_string
    |> Code.string_to_quoted!()
    |> Macro.to_string()
  end
end

"./remove-comment.exs"
|> File.read!()
|> RemoveComment.run()
|> IO.puts()

```

* * *

```sh
$ elixir --version
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.9.0 (compiled with Erlang/OTP 22)

$ elixir remove-comment.exs
(
  defmodule(RemoveComment) do
    def(run(code_string)) do
      code_string |> Code.string_to_quoted!() |> Macro.to_string()
    end
  end
  "./remove-comment.exs" |> File.read!() |> RemoveComment.run() |> IO.puts()
)

```

---

<div class="post-metadata">

**Author:** ![Schultzer](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/schultzer/32/4339_2.png) [@Schultzer](https://forum.elixirforum.com/u/Schultzer)\
**Post date:** [July 13, 2019, 12:37am UTC](https://forum.elixirforum.com/t/how-to-remove-comments-from-elixir-code/23937/7 "2019-07-13T00:37:10Z")

</div>

Thats properly because the formatter use [Inspect.Algebra — Elixir v1.20.2](https://hexdocs.pm/elixir/Inspect.Algebra.html)
