How to share code snippets online

You've got a chunk of code that needs to land in someone else's screen. Slack mangles indentation. Email strips formatting. Here's how to share code online properly.

Pick the right tool for the job

The right destination depends on three questions: how long does the snippet need to live, who can see it, and does it need syntax highlighting?

Format code so it's actually readable

Most messaging platforms (Slack, Discord, GitHub, Reddit, Stack Overflow) accept Markdown-style fenced code blocks. Triple backticks, the language name, then your code, then triple backticks:

```python
def hello(name):
    print(f"Hello, {name}")
```

The language hint enables syntax highlighting. Don't skip it.

What to strip before pasting anywhere public

This is where people get burned. Before you paste, scan for:

Once a secret hits a public paste, assume it's compromised. Rotate it. There are bots scraping pastebin all day, every day.

How much context to include

If you're asking for help, paste enough that someone can reproduce or understand the problem — but not your entire codebase. A good help post has:

  1. What you're trying to do (one sentence)
  2. The relevant function or block (10-50 lines)
  3. The error message or unexpected output, in full
  4. What you've already tried

Stack traces: keep the top, drop the middle

A 500-line Java stack trace is unhelpful. Keep the first 20 lines (the cause) and the last few (where it bubbled up to). Strip framework noise in the middle.

Sharing between your own devices

If you just need to move code from a laptop to a phone or another machine you own, you don't need a public paste at all. Use:

Sharing code in interviews

Don't use a pastebin. Use CoderPad, CodeSandbox, or a shared VS Code Live Share session. You want execution, not just display.

Related reading: pastebin alternatives compared · markdown editor