Fork me on github

Jupyter Notebook禁用自动缩进

在探索AI Agent操作Jupyter Notebook(例如OpenAI Computer Use)的过程中,自动缩进会影响模型的输出被正确地填充至Cells中。但是Jupyter Notebook未提供我们禁用换行时自动缩进的选项。这需要我们自行编写JavaScript代码禁用。

在打开custom.js文件(Windows在C:\Program Files\Anaconda3\Lib\site-packages\notebook\static\custom\custom.js下,Linux在~/.jupyter/custom/custom.js下),添加下面的代码到文件末尾,并重新加载Jupyter Notebook,即可禁用自动缩进:

var editor = document.querySelector('.CodeMirror').CodeMirror;
editor.setOption("extraKeys", {Enter: cm => cm.replaceSelection("\n")})

参考文献:
[1] How to turn off auto-indentation of a new line?

posted @ 2025-04-02 16:30  fang-d  阅读(62)  评论(0)    收藏  举报