如何在 Notepad++ 中缩进 Python 代码块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23305586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:42:41  来源:igfitidea点击:

How to indent a block of Python code in Notepad++?

pythonnotepad++indentation

提问by Shashank Sawant

While coding in Matlab, I had the option of smart indenting my code using the shortcut Ctrl+i. Is there a similar option for Pythonusing Notepad++?

在编码时Matlab,我可以选择使用快捷方式Ctrl+智能缩进我的代码 i。是否有类似的Python使用选项Notepad++

Specifically, I am looking for a solution to the following problem:
I want the block

具体来说,我正在寻找以下问题的解决方案:
我想要块

line 1
line 2
line 3

to be converted to

要转换为

    line 1
    line 2
    line 3

On a side-note, I found this to be surprisingly easy with SE editor (keyboard shortcut: Ctrl+k). I want this feature to be available for other languages as well (in my experience good indentation hasn't killed anyone, regardless of the language).

附带说明一下,我发现使用 SE 编辑器(键盘快捷键:Ctrl+ k)非常容易。我希望此功能也可用于其他语言(根据我的经验,良好的缩进并没有杀死任何人,无论语言如何)。

采纳答案by senshin

Just select the text you want indented and hit Tab. To un-indent, select the text and hit Shift+Tab.

只需选择要缩进的文本并点击Tab。要取消缩进,请选择文本并点击Shift+ Tab

If you're in "Normal text file" mode, this will give you tabs (\tcharacters). If, however, you're in Python mode (which you enter either by saving as a .pyfile or by selecting Python from the Language menu), it should default to using four spaces instead of tabs. To configure the way tabs work yourself, go to Settings → Preferences → Tab Settings (for Python, this should default to "Tab size: 4" with "Replace by space" checked).

如果您处于“普通文本文件”模式,这将为您提供制表符(\t字符)。但是,如果您处于 Python 模式(您可以通过另存为.py文件或从语言菜单中选择 Python 来输入),它应该默认使用四个空格而不是制表符。要自行配置选项卡的工作方式,请转到设置 → 首选项 → 选项卡设置(对于 Python,这应默认为“选项卡大小:4”并选中“按空格替换”)。

回答by Mr. Polywhirl

Select all 3 lines and hit the Tabkey. To undo this action you can hold down Shiftwhile pressing the Tabkey.

选择所有 3 行并按下Tab键。要撤消此操作,您可以Shift在按下Tab键的同时按住。

Pro Tip:As long as you have at least 2 lines partially selected, you can tab in the entire block.

专业提示:只要您至少选择了 2 行,就可以在整个块中使用 Tab 键。

Example:

例子:

enter image description here

在此处输入图片说明

If you want to change tab settings there are two locations in Settings -> Preferences

如果要更改选项卡设置,在设置 -> 首选项中有两个位置

  1. Under Tab Settings: You can change the tab size and you can check off the Replace by spaceoption if you want NPP to use spaces for tabs.
  2. Under MISC: you can check-off Auto-indentin case you have that option turned off.
  1. 选项卡设置下:您可以更改选项卡大小,如果您希望 NPP 使用空格作为选项卡,您可以勾选替换为空格选项。
  2. MISC 下:如果您关闭了该选项,您可以勾选自动缩进