-8

Whenever I paste any code in Stack Overflow by just copying it from Visual Studio Code and pasting it here in the answer field, it shows that I need to add 4 spaces in every line of code so that it will be formatted as code.

I always manually add 4 spaces in every line of code. Is there any solution to this problem?

4
  • 2
    This question belongs on the Meta site, not here. If you are typing out code, pressing SHIFT + ENTER will automatically indent the next line with 4 spaces. For cutting and pasting, you might have to use some external tool. Feb 3, 2022 at 6:24
  • 1
    You can also use Markdown.
    – ashu
    Feb 3, 2022 at 6:26
  • " Is there any solution to this problem?" - Google. Putting "stack overflow code blocks" into google brings you to the page that this question is closed as a duplicate of.
    – Gimby
    Feb 3, 2022 at 11:50
  • Workaround: Do it in Visual Studio Code: press Tab (the code is already selected), Ctrl + C, Ctrl + Z. (The 'dirty' state of the document is not even affected. And neither is the selection.) It works with the default settings. Feb 3, 2022 at 13:58

2 Answers 2

7

Just copy paste the code and simply wrap the code with ``` (three backticks).

like this

 //random react code

import React from 'react';
import ReactDOM from 'react-dom';

function Hello(props) {
  return <h1>Hello World!</h1>;
}

ReactDOM.render(<Hello />, document.getElementById('root'));
2

Select all your codes by dragging the mouse, and pressing ctrl + K, it will be converted to code block automatically.

or enclose them with ` (the symbol above the tab on the keyboard)

Not the answer you're looking for? Browse other questions tagged .