问题
Copying this piece of code from sublime :
# Texts Texts Texts Texts Texts Texts
for i in range(10):
#Idented Texxt
print i
results in below, with the line tab in jupyter-notebook. Now if i want to add to the code and do further testing in jupyter, i need to copy this weird arrow instead of hitting tab (else, it shows indentation error). Is there some simple way to change? I can imagine I am missing something really simple and this might not be a problem at all. I tried looking but found no mention of this. I can change all the indentation to space in sublime and this might work while copying but I rather work with tabs, to make codes readable.
Second image with double tabs, how it looks like in notebook.
回答1:
It looks like the reason that this is happening to you is due to a mismatch in the world view of tools (i.e. Sublime and Jupyter in this case) with regards to tabs versus spaces, a perennial dispute indeed.
In particular in Sublime you have translate_tabs_to_spaces
turned off because you want to work with physical tab characters. So pressing Tab in Sublime will insert physical tab characters which are visualized via the tab_size
setting.
On the other hand, according to this issue, Jupyter does not support indentation via tabs when you press the Tab key. As such in Jupyter despite your best intentions, when you press Tab it's inserting spaces instead. It does however support physical tabs if they're pasted in, which is why it appears the way it does above and why you have to manually paste physical tabs to get the input that you need.
Of these two tools, Sublime allows you to choose whether you use physical tabs or space characters, but Jupyter does not and always wants spaces. The most expedient solution in that case would be to not use tabs in Sublime either and then everything would match up.
Presumably that's not an option for you, so the next best solution would be to bridge the gap by having Sublime provide the data to Jupyter in a way that it expects, which is possible with a simple plugin.
In Sublime, select Tools > Developer > New Plugin...
and replace the stub with this code, then save in the default location that Sublime will prompt you with (your User
package) with a recognizable name such as copy_with_spaces.py
:
import sublime
import sublime_plugin
class CopyWithSpacesCommand(sublime_plugin.TextCommand):
"""
Copy the selected text to the clipboard, replacing all tab characters with
spaces based on the tab size in the current view.
"""
def run(self, edit):
self.view.run_command("copy")
tab_size = self.view.settings().get("tab_size", 4)
text = sublime.get_clipboard().expandtabs(tab_size)
sublime.set_clipboard(text)
This implements a new command copy_with_spaces
which will perform a copy but modify the data on the way through so that any physical tab characters are replaced with the appropriate number of white space characters.
With that in place, you can add a custom key binding to use when you're copying code from Sublime to Jupyter to streamline things. An example might be:
{
"keys": ["shift+ctrl+c"],
"command": "copy_with_spaces",
}
回答2:
After copying the scripts to Jupyter cells from sublime:
- Select all the lines with arrows
- Press Tab - This will remove the arrows and add a tab at the beginning of each line
- Press Shift + Tab - This will delete all the added tabs in the step above
来源:https://stackoverflow.com/questions/51723027/identation-coverts-to-weird-arrow-while-copying-code-from-sublime-to-jupyter-not