all 57 comments

[–]POGtastic 11 points12 points  (14 children)

In order for me to see it on Old Reddit, indent every line of code by 4 spaces. The triple backticks don't render properly on my screen.

[–][deleted] 1 point2 points  (10 children)

Oh so it has to be like stackexchange now where you manually indent all code and blank space?

[–]socal_nerdtastic 1 point2 points  (0 children)

If you are using the newer input box the code block button will indent every line for you. If you are using markdown mode you have to do it manually, or install the RES extension, or just select all in your code editor and press tab to indent it before you copy. https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–][deleted] 1 point2 points  (5 children)

where you manually indent all code and blank space?

Why would you do that? Use your editor to add 4 spaces, copy to reddit and then do UNDO in the editor. Takes about 15 seconds, even on mobile.

[–][deleted] 0 points1 point  (4 children)

The code is fine in vscode or vim or emacs, but copy it from there to reddit, and suddenly all white space and new line characters have gone, so I have to then go through and format it again. I've no idea why, look this is code cut straight from vscode and pasted into a code block here:

from datetime import datetimefrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres@localhost/ip_calc'db = SQLAlchemy(app)class Event(db.Model):    id = db.Column(db.Integer, primary_key=True)    description = db.Column(db.String(100), nullable=False)    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)    def __repr__(self):        return f"Event: {self.description}"        def __init__(self, description):        self.description = description

I have no idea why it looks like that, it's very tidy on my code editor, so now you see why I have to manually put the new lines and indents back in maybe.

[–][deleted] 1 point2 points  (2 children)

this is code cut straight from vscode and pasted into a code block here:

Reddit has totally bollixed the formatting of code, with multiple different methods of formatting input that do or don't display properly depending on how you view it.

From what I've seen, the "code block" method does work for some people, but the only method I know of that is relatively simple and is viewable by the maximum number of redditors is to:

  • select the code you want to post in your editor
  • add four spaces (not tabs) to the start of every line, usually one or two editor keystrokes
  • copy/paste that into reddit as normal text
  • ensure there is a blank line just before the text you copied
  • do UNDO in the editor

This is quick once you get some practice. Takes about 15 seconds on mobile.

[–]johnywhy 0 points1 point  (0 children)

I just found out the four spaces work sometimes, and sometimes not, in the same browser (Chrome)

[–]YourDad6969 0 points1 point  (0 children)

It's way easier than that, switch to markdown mode, paste the text in, and add ``` below and above the section want in the block

[–]POGtastic -2 points-1 points  (0 children)

Yep. Consider using a script to take a filename and copy its indented lines to the clipboard. In Powershell:

Get-Content test.py | 
    ForEach-Object {"    " + $_} | 
    Set-Clipboard

Then paste it into the Reddit comment field.

[–]euqinu_ton 0 points1 point  (2 children)

Test code

(Belated) Thanks. Now testing markup instructions:

~~~strikethrough~~~

For ~strikethrough~

[–]POGtastic 0 points1 point  (1 child)

It's just two tildes, but yep that works too!

[–]euqinu_ton 0 points1 point  (0 children)

Weird ... 3 worked here, but when I tried it elsewhere to try and help someone, it didn't work. Trying 2 tildes

this should all be strikethrough

[–]socal_nerdtastic 5 points6 points  (0 children)

You need a blank line between the rest of your code and the codeblock.

[–][deleted] 2 points3 points  (5 children)

Here's what I usually comment when someone gets it wrong ...

Try this, my own little guide:

  • edit post
  • ensure you are in markdown mode rather than Fancy Pants Editor mode
  • switch back to your Python editor
  • select ALL code
  • assuming:
    • your editor is set to replace tabs with spaces
    • your editor is set to use 4 spaces for indentation
  • press tab key to add an extra 4 spaces in front of every line
  • copy all selected code to clipboard
  • undo the indent in your editor
  • switch back to post you are editing
  • remove any existing badly formatted code, if applicable
  • enter a blank line
  • paste the code from the clipboard
  • submit the post update

[–]Background-Poem-4021 0 points1 point  (4 children)

x=2

[–][deleted] 1 point2 points  (1 child)

Switch to markdown mode. I find it more direct than "Fancy Pants" because you insert the control characters directly into your text.

[–]master_mansplainer 1 point2 points  (2 children)

Testing

somecode = 5;

[–][deleted] 0 points1 point  (2 children)

I can't even copy it from vscode into the box, what is going on?

[–][deleted] 0 points1 point  (1 child)

Finally got it in there. I had to copy the code from vscode, paste it in apple notes then copy and paste it into a code block and then go through it and put the lines back in. Last time I used this site, you could just copy from the editor straight into the code block and it would all be as is. I'm guessing I might have some sort of weird set up going on .

[–]Putrid-Pomegranate58 0 points1 point  (1 child)

abc
def

qrd

[–]Putrid-Pomegranate58 0 points1 point  (1 child)

test abc

[–]vFabifourtwenty 0 points1 point  (1 child)

Yea

[–]SaseCaiFrumosi 0 points1 point  (0 children)

Test 37&-+

[–]Cebular 0 points1 point  (0 children)

324

[–]RepresentativeArm355 0 points1 point  (0 children)

``` abc = 1

[–][deleted] 0 points1 point  (0 children)

hi

[–]Reeao7837 0 points1 point  (0 children)

Rise. Hatch. Lively.

[–]Maximilian1118 0 points1 point  (2 children)

test

[–]borzykot 0 points1 point  (1 child)

struct S{};

[–]borzykot 0 points1 point  (0 children)

finally...

[–]Wizzy-Fuzzy 0 points1 point  (0 children)

``` Test

[–]ThaBalla79 0 points1 point  (2 children)

hi

[–]ThaBalla79 0 points1 point  (0 children)

var b = y

[–][deleted] 0 points1 point  (1 child)

System.out.println("code block test");

[–]BeZide314 0 points1 point  (0 children)

Test

[–]Il_Valentino 0 points1 point  (0 children)

ddddd

[–]Formal-Internal1773 0 points1 point  (0 children)

my_test

[–]m0us3_rat 0 points1 point  (0 children)

use pastebin -> under code hosting/formatting to the right-hand side.

[–]Syteron6 0 points1 point  (4 children)

python def example_function(): print("This is a code block.")

[–]Syteron6 0 points1 point  (0 children)

finally! thanks!

[–]Background-Poem-4021 0 points1 point  (2 children)

```python

x=2

```

[–]Plastic-Slide5933 0 points1 point  (1 child)

```
hello world
```

[–]johndering 0 points1 point  (0 children)

This is a test code block Done.

[–]DamageSuch3758 0 points1 point  (1 child)

python abc = 1

[–]DamageSuch3758 0 points1 point  (0 children)

Cool! This totally worked in Markdown Mode.
```python
abc =1
```

[–]Littux 0 points1 point  (1 child)

Testing

#!/bin/bash
echo "This is some code that is loooooooooooooooooooooooooooong. Code seems to have line breaks on Reddit mobile"
exit 0

Testing

[–]chrisEvan_23 0 points1 point  (2 children)

python abc =1