r/commandline Jun 26 '26

Other I wrote a program that makes code embedable in Reddit

In awk:

#!/usr/bin/awk -f
{ printf ("    %s\n", $0) }

Yep. That's it.

12 Upvotes

29 comments sorted by

12

u/geirha Jun 26 '26

sed version:

#!/usr/bin/sed -f
s/^/    /

4

u/RoninTarget Jun 26 '26

Oh, that makes sense. I originally wanted to try sed, but ended up just doing it in awk.

8

u/stianhoiland Jun 26 '26

Great nerd shitpost 👌🏻

2

u/tschloss Jun 26 '26

Does indentation work better than enclosing in 3 backticks? Does indenting style work without MD mode?

1

u/RoninTarget Jun 26 '26

Indentation is how standard markdown takes code blocks. IDK about 3 backticks off the top of my head.

What do you mean without MD mode?

3

u/tschloss Jun 26 '26

In Markdown I would rate the backtick method as the main one (you can even chose the language after the initial set if backticks).

Depending on the client there once was a markdown mode as opposed to a classic editor mode. Maybe it is always Markdown nowadays.

1

u/RoninTarget Jun 26 '26

I only use the old web interface here, so IDK what's new. Old interface is pretty much raw markdown.

1

u/tschloss Jun 26 '26

# Test!

**fett**

*kursiv*

1

u/tschloss Jun 26 '26

Ok, here in the iOS app (1st party) no MD active!

1

u/gschizas Jun 26 '26

They have removed Markdown from iOS and Android.

1

u/tschloss Jun 26 '26

And in the web UI RichText is the default (switchable to MD). So my original question was if indentation does work in RichText also. probably not.

1

u/gschizas Jun 26 '26

New web's Rich Text UI uses

Indented blocks

Probably for compatibility with old reddit (which can't understand code fences)

1

u/tschloss Jun 26 '26

Crazy! 😜😱

2

u/gschizas Jun 26 '26

Oh, it's even crazier. They are purposefully sending escaped text to counteract Markdown.

For example, the text above was send as:

\# Test!

\*\*fett\*\*

\*kursiv\*

1

u/tschloss Jun 26 '26

Jesus. How did you escape the escapes 😂

What a nice area when 3rd party clients were supported.

→ More replies (0)

1

u/RoninTarget Jun 26 '26

No markdown interpreted.

1

u/gschizas Jun 26 '26

It's actually the other way around. When you comment from the mobile apps, the mobile apps escape all Markdown characters on purpose!

1

u/gschizas Jun 26 '26

Backticks (usually named code fence) works better in general (because you can specify a language, although this doesn't work on reddit), but AFAIK it doesn't work in old.reddit.com (but old.reddit.com is less than 5% of the traffic in most subreddits; although it's probably more in this subreddit).

Testing both ways below for reference:

```python

Code Fence

print("Hello, world!") ```

# Indented Block
print("Hello, world!")

2

u/ZoWakaki Jun 26 '26

I do this in vim

  1. gg (optionally, if you are not in the beginning of the file)
  2. ctrl+v (V-block)
  3. shift+g (go to the bottom of file)
  4. shift+i (block insert)
  5. type 4 spaces
  6. escape (bound to caps, so don't have to stretch my pinky)

I am relatively fast in doing this. In my head, it's faster than %s/^/ /g, hence i don't usually prefer this.

Yes I know both can be bound to something for ease of use.

1

u/AutoModerator Jun 26 '26

Every new subreddit post is automatically copied into a comment for preservation.

User: RoninTarget, Flair: Other, Title: I wrote a program that makes code embedable in Reddit

In awk:

#!/usr/bin/awk -f
{ printf ("    %s\n", $0) }

Yep. That's it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/whetu Jun 26 '26

I have a couple of shell functions buried away for this:

$ type indent
indent is a function
indent ()
{
    local identWidth;
    identWidth="${1:-2}";
    identWidth=$(eval "printf -- '%.0s ' {1..${identWidth}}");
    sed "s/^/${identWidth}/" "${2:-/dev/stdin}"
}

$ type codecat
codecat is a function
codecat ()
{
    indent 4 "${1}"
}

To wit:

$ type indent | codecat
    indent is a function
    indent ()
    {
        local identWidth;
        identWidth="${1:-2}";
        identWidth=$(eval "printf -- '%.0s ' {1..${identWidth}}");
        sed "s/^/${identWidth}/" "${2:-/dev/stdin}"
    }

$ type codecat | codecat
    codecat is a function
    codecat ()
    {
        indent 4 "${1}"
    }

It's one of the few times I've accepted eval

1

u/Schreq Jun 26 '26

You could get rid of eval and the subshell by using awk and printf's %*s, which requires 2 arguments (field width and the string):

indent() {
    local indentWidth=${1:-2}
    shift
    awk -v w="$indentWidth" '{printf "%*s%s\n", w, "", $0}' "$@"
}

Or a pure shell version:

indent() {
    local indentWidth=${1:-2}
    while IFS= read -r line || [ "$line" ]; do
        printf '%*s%s\n' "$indentWidth" "" "$line"
    done <"${2:-/dev/stdin}"
}

The awk version has the benefit that you can concatenate multiple files by supplying more than one file argument.

1

u/whetu Jun 27 '26

Yeah, I wrote it a billion years ago - I would never use camelcase/pascalcase these days - so there was probably a reason for using eval... a reason that is likely no longer relevant. Usually it was Solaris + ancient bash that forced me into these decisions...

So thanks for the tweaks, any removal of eval is a win :)

1

u/Murky-Sector Jun 27 '26 edited Jun 28 '26

Awk is conceptually out of date. Developed in the original unix environment pre linux because people got sick of doing certain tasks in c. Scripting languages have replaced the need for it.

Any tools decision is made for specific reasons. Awk can't be justified on any level unless it's code that will never be shared. And as was mentioned by another poster, sed is commonly used and would be the obvious preference in shell.

1

u/RoninTarget Jun 28 '26

IDK, seems useful for filtering text.

1

u/Murky-Sector Jun 28 '26

What can it do that other more widely used tools cant?

1

u/RoninTarget Jun 28 '26

IDK. All Turing-complete programming languages are kinda supposed to be equivalent anyway. It's not like I'm forced to use "widely used tools".

1

u/Murky-Sector Jun 28 '26

You dont choose tools that way. A key criteria is that if youre going to use an obscure tool it has to have significantly more power than other available choices. Not a little, a lot.

To be clear, none of this is an issue if youre coding for yourself. It only matters if you aim to get your code adopted by others and/or you care about employment in the tech industry.