简体   繁体   English

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

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

While coding in Matlab , I had the option of smart indenting my code using the shortcut Ctrl + i . Matlab编码时,我可以选择使用快捷键Ctrl + i智能缩进我的代码。 Is there a similar option for Python using Notepad++ ? 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 ). 另外,我发现使用SE编辑器(键盘快捷键: 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). 我希望这个功能也适用于其他语言(根据我的经验,良好的缩进并没有杀死任何人,无论语言如何)。

Just select the text you want indented and hit Tab . 只需选择要缩进的文本,然后按Tab键 To un-indent, select the text and hit Shift + Tab . 要取消缩进,请选择文本并按Shift + Tab

If you're in "Normal text file" mode, this will give you tabs ( \\t characters). 如果您处于“普通文本文件”模式,这将为您提供标签( \\t字符)。 If, however, you're in Python mode (which you enter either by saving as a .py file or by selecting Python from the Language menu), it should default to using four spaces instead of tabs. 但是,如果您处于Python模式(通过保存为.py文件或从“语言”菜单中选择Python来输入),则应默认使用四个空格而不是制表符。 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). 要配置标签自己的工作方式,请转到设置→首选项→标签设置(对于Python,这应默认为“标签大小:4”,并选中“按空格替换”)。

Select all 3 lines and hit the Tab key. 选择所有3行并按Tab键。 To undo this action you can hold down Shift while pressing the Tab key. 要撤消此操作,您可以在按Tab键的同时按住Shift键。

Pro Tip: As long as you have at least 2 lines partially selected, you can tab in the entire block. 专业提示:只要您至少部分选择了2行,就可以在整个块中进行选项卡。

Example: 例:

在此输入图像描述

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 space option if you want NPP to use spaces for tabs. 在“ 选项卡设置”下 :您可以更改选项卡大小,如果希望NPP为选项卡使用空格,则可以选中“ 按空格替换 ”选项。
  2. Under MISC : you can check-off Auto-indent in case you have that option turned off. MISC下 :如果关闭该选项,您可以勾选自动缩进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM