-1

I've been working with python for a while, and understand how to use tabs correctly, but recently in a project I have started working across Jupyter notebooks and Sublime. When I copy and paste code over, the indentation gets completely messed up. Usually it goes from tabs to 4 spaces and it doesn't like that so I have to manually change every line. It happens on occasion when I copy from one python file to another in just Sublime as well.

Is there some sort of general way, or just setting within those applications, that can prevent this?

plr108
  • 1,201
  • 11
  • 16
  • 1
    Does this answer your question? ["inconsistent use of tabs and spaces in indentation"](https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation) – Georgy May 06 '20 at 13:00

1 Answers1

1

Jupyter Notebooks indent with 4 spaces by default. Not sure where you'd be getting tabs unless you originally copy-pasted tabs.

If you want to keep the tabs in SublimeText, select a Python file, go to "Preferences" --> "Settings - Syntax Specific" and add the line:

"translate_tabs_to_spaces": false

Although, if your sources were already mixed (some cells in jupyter use tabs, some use spaces) then it's better to standardise on paste in sublime, and use:

"translate_tabs_to_spaces": true

Also, when you're pasting in SublimeText, instead of just Paste with Ctrl+V, use Paste & Indent: Ctrl+Shift+V

Btw, Python3 recommends 4 spaces by default, almost as a standard. So maybe set that in your editor. Most of them have settings for "Tabs & Indents" somewhere with code formatting.

aneroid
  • 12,983
  • 3
  • 36
  • 66