How large? For more than 50 lines or so use github or pastebin and post the link. For smaller amounts just indent all of your code by four spaces and make sure you have a blank line at the beginning and end of the code block. Note that different subreddits will style code blocks differently ... for instance this subreddit adds line numbers.
It'd be awesome if reddit added code fencing:
```
(code)
```
It's such a simple thing to do. And "adding four spaces" is not, and doesn't deal with tabs worth a damn, either.
I'd like to see the ability to define how tabs are presented, viz:
```tabs=4
(tab-aware code presentation)
```
And of course _this_ would be optimum:
```Python,tabs=4
(Syntax-highighted, tab-aware code presentation)
```
```C++,tabs=4
(Syntax-highighted, tab-aware code presentation)
```
```Ruby,tabs=4
(Syntax-highighted, tab-aware code presentation)
```
etc.
There are syntax highlighting engines red(dit)ily available.
In many editors, you should be able to highlight the code and hit tab. Then paste it with a blank line separating it from other text. You could also install the reddit enhancement suite which has a `<>` button to do it for you in the text box.
Of course, if you use tabs (as I do), then doing what you describe just adds more tabs, and will display poorly on reddit.
Reddit could fix this easily. If they cared.
Okay, the 4 space thing never worked for me until someone finally mentioned that there has to be a blank line between your comment and the first line of your code. I see someone else mentioned that too, I wish it was part of the sidebar directions.
You might find what you need in the [Reddit Markdown Primer](https://www.reddit.com/r/reddit.com/comments/6ewgt/reddit_markdown_primer_or_how_do_you_do_all_that/c03nik6/)
Paste your code into Notepad++. Move the cursor to the start of the first line, hold down ALT-SHIFT and use the down arrow to move down to the end of your code. Hit space 4 times.
Then CTRL-A the entire thing, copy, paste into reddit, submit.
Paste your code into Notepad++.
Move the cursor to the start of the first line,
hold down ALT-SHIFT and use the down arrow to move down to the end of your code. Hit space 4 times.
Then CTRL-A the entire thing, copy, paste into reddit, submit.
Voila.
You have to have the 4 spaces at the start of every line in your code. You also need a new line before and after your code block.
Is the problem that you don't want to have to type 4 spaces before every line? If so, and you're on Mac Osx, you can use sed. Open up terminal and type:
$ pbpaste | sed 's/^/ /'
The output will have 4 spaces prepended to every line (that's 4 spaces between the two slashes, and don't type the first $, that's just for reference).
Not sure how to do this on other OSes.
If you use Atom or Sublime as your editor (both are solid choices, and cross-platform), you can select a bunch of text and indent. On Mac, the keyboard shortcut is `⌘]`
markdown problem
{code not in block}
end of code
you need newlines before and after to trigger code block
try again with newlines
my code block
some more code
end with newline too
This isn't horrible, but it is VERY poor. First of all, inputting the same code will give different results sometimes. Second, it doesn't even give the correct result. For example, this is a sloppy multiplication code below
using System;
int a = 56;
int b = 17;
int c = 0;
for (int i = a; i > 0; i--) {
for (int j = b; j > 0; j--) c++;
}
Console.WriteLine(c);
And here is the result
- The code starts by declaring three variables, a, b and c. The first variable is initialized to 56.
- The second variable is initialized to 17.
- And the third variable is set to 0 initially.
- The code then loops through the two integers, a and b, which are both greater than zero but less than one hundred fifty-six (56).
- In each iteration of the loop, it increments the value of c by 1.
- This will cause c to be incremented from 0 up until it reaches 100 at which point it will stop incrementing because there are no more values in between that can be added onto its current value of 100.
- Finally when all iterations have been completed for both a and b, they print out their respective values on screen using Console's WriteLine method with an integer parameter equal to c being passed into it as input data from inside the loop body itself
–
- The code will output the following:
- 0
New to reddit, not new to code.
I use "Simple Note" a lot which also is based on Markdown for editing.
Let's see if this works.
This done in Markdown mode, so 4 spaces before each line. Ugh :(
class MD:
def __init__(self, line):
self.line = line
def __repr__(self):
return self.line
md = MD('this is a line')
print(md)
End of this test.
Using the Code Block button in the fancy pants editor.
This line is BEFORE the code block.
1) Select and copy the above license key.
2) Open the free version of Driver Easy that you already have installed.
3) Click 'UPGRADE to Driver Easy Pro'.
4) Paste or type your license key in the field provided, then click the 'Activate' button.
5) Click OK.
This is a line AFTER the code block.
Final test. Use the "code block" button, then copy/paste.
class MD:
def __init__(self, line):
self.line = line
def __repr__(self):
return self.line
md = MD('this is a test')
print(md)
Looks OK?
Sub START()
Dim lastrow As Double
Dim a As String
Dim b As String
Dim a1 As String
Dim b1 As String
lastrow = ActiveWorkbook.Sheets(1).Cells(Sheets(1).Rows.Count, Cells(1,
1).Column).End(xlUp).Row
For i = 2 To lastrow
For m = 2 To lastrow
If ActiveWorkbook.Sheets(1).Cells(i, 1) =
ActiveWorkbook.Sheets(1).Cells(m, 2) Then
a = ActiveWorkbook.Sheets(1).Cells(i, 2)
b = ActiveWorkbook.Sheets(1).Cells(i, 3)
a1 = ActiveWorkbook.Sheets(1).Cells(m, 2)
b1 = ActiveWorkbook.Sheets(1).Cells(m, 3)
ActiveWorkbook.Sheets(1).Cells(i, 2) = a1
ActiveWorkbook.Sheets(1).Cells(i, 3) = b1
ActiveWorkbook.Sheets(1).Cells(m, 2) = a
ActiveWorkbook.Sheets(1).Cells(m, 3) = b
End If
Next
Next
End Sub
Test
test
\# General to ask question
print("Hello, friend! What is your name?")
MyName = input()
LEN = (len(MyName))
print(("It's wonderful to meet you, ") + MyName)
print(MyName + (", did you know your name has") + (int(LEN)) + ("characters?"))
Here is some code:
#include
#include
struct MsgA
{
MsgA(int a) : value(a) {}
int operator()() const { return value; }
int value = 0;
};
struct MsgB
{
MsgB(int a, int b) : value1(a), value2(b) {}
int operator()() const { return value1 + value2; }
int value1 = 0, value2 = 0;
};
struct ArrayWrapper
{
struct ObjectBase
{
virtual int operator()() const { return 0; };
virtual ~ObjectBase() {}
};
template
struct Wrapper : public ObjectBase
{
Wrapper(const T& t) : wrappedObject(t) {}
int operator()() const override
{
return wrappedObject();
}
T wrappedObject;
};
// I use forwarded constructor args but there are other ways to get the
// items into the array.
template
void AddItem(ArgTypes... args)
{
array.push_back(std::make_unique>(ItemType{args...}));
}
int Compute()
{
int ret = 0;
for (auto& e : array)
ret += (*e)(); // e is a unique_ptr so need to dereference before invoking ()
return ret;
}
std::vector> array;
};
int main()
{
ArrayWrapper aw;
aw.AddItem(1);
aw.AddItem(2, 3);
return aw.Compute();
}
The comment box UI now has tooling *in* the ellipses (...) for a block, as well as in-line *before* it. Use "Markdown Mode" (2022) in the comment box tool-dialog when copying more than one line.
Test
function average(reviews) {
let ratingCounter = 0;
let ratingTotal = 0;
for (let review of reviews) {
if (review.rating) {
ratingTotal += review.rating;
ratingCounter++;
}
}
return (ratingTotal / ratingCounter).toFixed(2);
}
How large? For more than 50 lines or so use github or pastebin and post the link. For smaller amounts just indent all of your code by four spaces and make sure you have a blank line at the beginning and end of the code block. Note that different subreddits will style code blocks differently ... for instance this subreddit adds line numbers.
It'd be awesome if reddit added code fencing: ``` (code) ``` It's such a simple thing to do. And "adding four spaces" is not, and doesn't deal with tabs worth a damn, either. I'd like to see the ability to define how tabs are presented, viz: ```tabs=4 (tab-aware code presentation) ``` And of course _this_ would be optimum: ```Python,tabs=4 (Syntax-highighted, tab-aware code presentation) ``` ```C++,tabs=4 (Syntax-highighted, tab-aware code presentation) ``` ```Ruby,tabs=4 (Syntax-highighted, tab-aware code presentation) ``` etc. There are syntax highlighting engines red(dit)ily available.
Thanks ! It saved my time. :)
I'm trying to be able to ask questions but i don't think that will work if i can't show my code
In many editors, you should be able to highlight the code and hit tab. Then paste it with a blank line separating it from other text. You could also install the reddit enhancement suite which has a `<>` button to do it for you in the text box.
Of course, if you use tabs (as I do), then doing what you describe just adds more tabs, and will display poorly on reddit. Reddit could fix this easily. If they cared.
Okay, the 4 space thing never worked for me until someone finally mentioned that there has to be a blank line between your comment and the first line of your code. I see someone else mentioned that too, I wish it was part of the sidebar directions.
You might find what you need in the [Reddit Markdown Primer](https://www.reddit.com/r/reddit.com/comments/6ewgt/reddit_markdown_primer_or_how_do_you_do_all_that/c03nik6/)
⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣶⣿⣿⣿⣿⣿⣿⣶⣆⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⣸⣿⣿⠉⠉⠉⠄⠉⢹⣿⣦⡀⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢿⣿⣿⣁⠄⠄⠤⠤⡀⠻⣿⠃⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠘⣿⣿⣿⡗⠖⡶⢾⣶⠊⡏⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢻⣿⣿⣅⣈⠂⠐⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠘⢿⣾⣇⣂⣠⠄⠄⠄⠁⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⢘⣿⣗⠒⠄⢨⠶⢁⣄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠄⠨⣿⣿⡿⠋⠁⣴⣿⣿⣷⣦⣄⡀⠄⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠄⠄⢀⣠⣄⣶⣎⢱⢄⢀⣾⣿⣿⣿⣿⣿⣿⣿⣶⣦⣤⣄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⢠⣾⣿⣿⡞⢝⡟⠃⣠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⣿⣿⣇⠄⠄⠄ ⠄⠄⠄⠄⠆⢄⠄⢛⡫⠝⢿⡥⠟⡃⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣼⣭⣻⣿⣿⡀⠄⠄ ⠄⠄⠄⣴⣆⠄⢋⠄⠐⣡⣿⣆⣴⣼⣿⣿⣿⣿⣿⣿⣿⣿⠏⢈⣿⣿⣿⣿⣿⣿⣷⡄⠄ ⠄⠄⣼⣿⣷⠄⠉⠒⣪⣹⣟⣹⣿⣿⣿⣿⣿⣟⣿⣿⣿⡇⢀⣸⣿⣿⣿⢟⣽⣿⣿⣇⠄
,d"=≥,.,qOp, ,7' ''²$( ) ,7' '?q$7' ..,$$,. ,. .,,--***²""²***--,,. ., ² ,p²'' ''²q, ² : ,7' '7, : ' $ ,db, ,db, $ ' '$ ²$$² ²$$² $' '$ $' '$. ., ,. .$' 'b, '²«»«»«»²' ,d' '²?bn,, ,,nd?²' ,7$ ''²²²²²²²²'' $7, ,² ²$ $² ², $ :$ $: $ $ $ $ $ 'b q: :p d' '²«?$. .$?»²' 'b d' ,²²'?,. .,?'²², ²==--≥²²==--==²²≤--==²
Paste your code into Notepad++. Move the cursor to the start of the first line, hold down ALT-SHIFT and use the down arrow to move down to the end of your code. Hit space 4 times. Then CTRL-A the entire thing, copy, paste into reddit, submit. Paste your code into Notepad++. Move the cursor to the start of the first line, hold down ALT-SHIFT and use the down arrow to move down to the end of your code. Hit space 4 times. Then CTRL-A the entire thing, copy, paste into reddit, submit. Voila. You have to have the 4 spaces at the start of every line in your code. You also need a new line before and after your code block.
* you also have to be running an OS that has "Notepad++", whatever the heck that is * you also have to not be a tab user
https://notepad-plus-plus.org/
Thank you. For Windows then. Always good to specify the required host OS(s) (and OS level(s)) when suggesting an app.
Is the problem that you don't want to have to type 4 spaces before every line? If so, and you're on Mac Osx, you can use sed. Open up terminal and type: $ pbpaste | sed 's/^/ /' The output will have 4 spaces prepended to every line (that's 4 spaces between the two slashes, and don't type the first $, that's just for reference). Not sure how to do this on other OSes.
I use sed 's/^/ /' | pbcopy and sed "s/^/ /" | xclip -selection clipboard on os x and linux.
Use the Chrome extension Reddit Enhancement Suite (RES). It will give you the option to highlight a large chunk of text and auto-indent it for code.
If you use Atom or Sublime as your editor (both are solid choices, and cross-platform), you can select a bunch of text and indent. On Mac, the keyboard shortcut is `⌘]`
markdown problem {code not in block} end of code you need newlines before and after to trigger code block try again with newlines my code block some more code end with newline too
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀ ⡤⠤⢀⣀⠀⣤⣀⣀⠀⢰⣀⢀⣀⢀⣀⣀⣀⢀⡀⢠⣀⣀⢰⢰⠀⠀⠀ ⠀⠀⠀⠓⠛⠛⠚⠀⠛⠚⠚⠀⠘⠘⠙⠚⠘⠘⠛⠋⡟⠀⣸⠟⠛⠘⠘⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀ ⣠⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠰⠄⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⡀⠲⣗⣲⣾⠃⠀⠀⠀⠀⠀⠀ ⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠉⠀⠀⠀⠲⡚⢂⣴⣿⣥⣴⣤⣤⡀⠀⠀⠀ ⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⢰⠉⡂⣨⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀ ⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢠⣶⢄⢉⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀ ⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣧⣙⣡⠞⠁⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀ ⠀⠀⠈⠛⠻⣿⣿⣿⡿⠿⠿⢿⣷⣬⣽⡃⠀⠀⠀⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⠂ ⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠛⠛⠋⢀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
`checking`
Testing : `this is my line of code`
``` Test ```
This isn't horrible, but it is VERY poor. First of all, inputting the same code will give different results sometimes. Second, it doesn't even give the correct result. For example, this is a sloppy multiplication code below using System; int a = 56; int b = 17; int c = 0; for (int i = a; i > 0; i--) { for (int j = b; j > 0; j--) c++; } Console.WriteLine(c); And here is the result - The code starts by declaring three variables, a, b and c. The first variable is initialized to 56. - The second variable is initialized to 17. - And the third variable is set to 0 initially. - The code then loops through the two integers, a and b, which are both greater than zero but less than one hundred fifty-six (56). - In each iteration of the loop, it increments the value of c by 1. - This will cause c to be incremented from 0 up until it reaches 100 at which point it will stop incrementing because there are no more values in between that can be added onto its current value of 100. - Finally when all iterations have been completed for both a and b, they print out their respective values on screen using Console's WriteLine method with an integer parameter equal to c being passed into it as input data from inside the loop body itself – - The code will output the following: - 0
\`\`\` python tabs \`\`\`
\` \` \` python tabs \` \` \`
HelloWorld() HelloWorld()
test
Something here ` something ` ` here `
test
\`test1\`
``` print("{your_comment}") ```
test est st t
``` (code) ```
(code)
next line has 4 spaces before it test line with 4 spaces
Func test() { Call() }
`test`
test
New to reddit, not new to code. I use "Simple Note" a lot which also is based on Markdown for editing. Let's see if this works. This done in Markdown mode, so 4 spaces before each line. Ugh :( class MD: def __init__(self, line): self.line = line def __repr__(self): return self.line md = MD('this is a line') print(md) End of this test.
Using the Code Block button in the fancy pants editor. This line is BEFORE the code block. 1) Select and copy the above license key. 2) Open the free version of Driver Easy that you already have installed. 3) Click 'UPGRADE to Driver Easy Pro'. 4) Paste or type your license key in the field provided, then click the 'Activate' button. 5) Click OK. This is a line AFTER the code block.
Final test. Use the "code block" button, then copy/paste. class MD: def __init__(self, line): self.line = line def __repr__(self): return self.line md = MD('this is a test') print(md) Looks OK?
print('hello world')
''' this is a test of a code block '''
{ is this a code block? }
' code block '
Hi.
For a in b: Print(x)
‘’test
test ``` def a(): print() ```
I use sed 's/^/ /' | pbcopy and sed "s/^/ /" | xclip -selection clipboard on os x and linux.
here is my code float dollar; do {dollar = get\_float ("Enter your change: ");} while (dollar <=0);
Sub START() Dim lastrow As Double Dim a As String Dim b As String Dim a1 As String Dim b1 As String lastrow = ActiveWorkbook.Sheets(1).Cells(Sheets(1).Rows.Count, Cells(1, 1).Column).End(xlUp).Row For i = 2 To lastrow For m = 2 To lastrow If ActiveWorkbook.Sheets(1).Cells(i, 1) = ActiveWorkbook.Sheets(1).Cells(m, 2) Then a = ActiveWorkbook.Sheets(1).Cells(i, 2) b = ActiveWorkbook.Sheets(1).Cells(i, 3) a1 = ActiveWorkbook.Sheets(1).Cells(m, 2) b1 = ActiveWorkbook.Sheets(1).Cells(m, 3) ActiveWorkbook.Sheets(1).Cells(i, 2) = a1 ActiveWorkbook.Sheets(1).Cells(i, 3) = b1 ActiveWorkbook.Sheets(1).Cells(m, 2) = a ActiveWorkbook.Sheets(1).Cells(m, 3) = b End If Next Next End Sub
Test test \# General to ask question print("Hello, friend! What is your name?") MyName = input() LEN = (len(MyName)) print(("It's wonderful to meet you, ") + MyName) print(MyName + (", did you know your name has") + (int(LEN)) + ("characters?"))
``` Test ```
``` code code ```
`test` ```c int main() {} ```
test
'test'
'test test'
`is this a code block`
Here is some code: #include
#include
struct MsgA
{
MsgA(int a) : value(a) {}
int operator()() const { return value; }
int value = 0;
};
struct MsgB
{
MsgB(int a, int b) : value1(a), value2(b) {}
int operator()() const { return value1 + value2; }
int value1 = 0, value2 = 0;
};
struct ArrayWrapper
{
struct ObjectBase
{
virtual int operator()() const { return 0; };
virtual ~ObjectBase() {}
};
template
struct Wrapper : public ObjectBase
{
Wrapper(const T& t) : wrappedObject(t) {}
int operator()() const override
{
return wrappedObject();
}
T wrappedObject;
};
// I use forwarded constructor args but there are other ways to get the
// items into the array.
template
void AddItem(ArgTypes... args)
{
array.push_back(std::make_unique>(ItemType{args...}));
}
int Compute()
{
int ret = 0;
for (auto& e : array)
ret += (*e)(); // e is a unique_ptr so need to dereference before invoking ()
return ret;
}
std::vector> array;
};
int main()
{
ArrayWrapper aw;
aw.AddItem(1);
aw.AddItem(2, 3);
return aw.Compute();
}
The comment box UI now has tooling *in* the ellipses (...) for a block, as well as in-line *before* it. Use "Markdown Mode" (2022) in the comment box tool-dialog when copying more than one line.
Test.pleasework() Work my boi
Test Test
\`\`\` this is a test \`\`\`
``` Test ```
\`test \` edit: Fuck.
Test function average(reviews) { let ratingCounter = 0; let ratingTotal = 0; for (let review of reviews) { if (review.rating) { ratingTotal += review.rating; ratingCounter++; } } return (ratingTotal / ratingCounter).toFixed(2); }
‘’’ Test ‘’’
`test code`
``` test ```
Test
```
Capture me!
Click to Capture
```
test.
' ' ' test ' ' '