如何在Xcode中缩进多行?

时间:2022-11-28 09:17:37

When I select multiple lines of code and want to indent them as usual with TAB key, it just deletes them all. I come from Eclipse where I always did it that way. How's that done in Xcode? I hope not line by line ;)

当我选择多行代码并希望像往常一样使用TAB键缩进它们时,它只是将它们全部删除。我来自Eclipse,我总是那样做。怎么在Xcode中完成?我希望不是一行一行;)

18 个解决方案

#1


The keyboard shortcuts are +] for indent and +[ for un-indent.

键盘快捷键为⌘+]表示缩进,⌘+ [表示取消缩进。

  • In Xcode's preferences window, click the Key Bindings toolbar button. The Key Bindings section is where you customize keyboard shortcuts.
  • 在Xcode的首选项窗口中,单击“键绑定”工具栏按钮。 Key Bindings部分是您自定义键盘快捷键的地方。

#2


Select your code to reindent, then Go to

选择要重新启动的代码,然后转到

Editor -> Structure -> Re-Indent

编辑器 - >结构 - >重新缩进

#3


+ [ and + ] are the equivalents to shift+tab in Xcode.

⌘+ [和⌘+]是Xcode中shift + tab的等效项。

#4


Here are the shortcuts, to format the code in XCode

以下是格式化XCode中代码的快捷方式

  1. Format entire code (entire class/controller)

    格式化整个代码(整个类/控制器)

    Select the entire code and press control+I on mac to format your code.

    选择整个代码并按下Mac上的control + I来格式化代码。

  2. Format particular block of code

    格式化特定的代码块

    Select the code and press:

    选择代码并按:

    • +] for right move (indent)
    • ⌘+]用于右移(缩进)

    • +[ for left move (un-indent)
    • ⌘+ [用于左移(非缩进)

Note: as per @JavierGiovannini sugesstion you can do using Editor Menu option

注意:根据@JavierGiovannini sugesstion,您可以使用Editor Menu选项

  1. Select your code and navigate to Editor > Structure > Re-Indent
  2. 选择代码并导航到Editor> Structure> Re-Indent

#5


Another way to quickly reformat indenting is a quick cut and paste. +x and +v. I often find it faster than +[ or +] as you can do it with one hand (versus two) and it will reformat to the correct indent level in one shot.

快速重新格式化缩进的另一种方法是快速剪切和粘贴。 ⌘+ x和⌘+ v。我经常发现它比⌘+ [或⌘+]更快,因为你可以用一只手(相对于两只)来做它,并且它将在一次拍摄中重新格式化为正确的缩进级别。

#6


If you use synergy (to share one keyboard for two PCs) and PC(MAC) in which you are using xcode is slave, and master PC is Windows PC

如果您使用synergy(为两台PC共享一个键盘)和使用xcode的PC(MAC)是奴隶,而主PC是Windows PC

keyboard shortcuts are alt+] for indent and alt+[ for un-indent.

键盘快捷键是alt +]用于缩进和alt + [用于un-indent。

Update:

But from synergy version 1.5 working +[ for indent and +] for un-indent

但是从synergy版本1.5开始工作⌘+ [用于缩进和⌘+]用于非缩进

#7


In Xcode 9, you can finally use Tab and Shift+Tab to indent multiple lines of code. Yay!

在Xcode 9中,您最终可以使用Tab和Shift + Tab来缩进多行代码。好极了!

#8


First, select all code using command+a

首先,使用命令+ a选择所有代码

Second, hold key ctr and then press i

其次,按住键ctr,然后按i

the whole selected code will nicely indent.

整个选定的代码将很好地缩进。

#9


Select "Tab key: Indents always" in Preferences->Text Editing->Indentation Then you can indent a single line or a selection of lines by pressing TAB or SHIFT+TAB Sadly this removes altogether the possibility to insert tabs where you want, and conflict badly with the tab key being used to switch between "autocompletion fields".

在首选项 - >文本编辑 - >缩进中选择“Tab键:始终缩进”然后您可以通过按TAB键或SHIFT + TAB缩进单行或选择行。遗憾的是,这样可以完全删除在所需位置插入制表符的可能性,以及用于在“自动填充字段”之间切换的tab键严重冲突。

I guess we need more tab keys in the keyboard, one is not enough...

我想我们在键盘上需要更多的Tab键,其中一个还不够......

#10


For those of you with Spanish keyboard on mac this are the shortcuts:

对于那些在Mac上使用西班牙语键盘的人来说,这是快捷方式:

+ + [ for un-indent

⌘+⌥+ [用于取消缩进

+ + ] for indent

缩进的⌘+⌥+]

#11


here all the important shortcuts from another question answered on stack overflow 如何在Xcode中缩进多行?

这里所有重要的快捷方式来自另一个问题,在堆栈溢出时回答

#12


To all frenchs Xcoders around, the default keybindings are as follow :

对于所有法国人Xcoders,默认的键绑定如下:

Indent selection:

+ + + )

⇧+⌘+⌥+)

Unindent selection:

+ + + (

⇧+⌘+⌥+(

#13


If you want to remap the standard Xcode's +[ to TAB, you can do the following:

如果要重新映射标准Xcode的⌘+ [到TAB,您可以执行以下操作:

  1. Install a program called "Karabiner".

    安装一个名为“Karabiner”的程序。

  2. Go to Preferences ~> "Misc and Uninstall"

    转到首选项〜>“其他和卸载”

  3. In "Custom Setting" section click on the "Open private.xml" button

    在“自定义设置”部分中,单击“打开private.xml”按钮

  4. Open the file and replace its content with the following code:

    打开文件并使用以下代码替换其内容:

    <?xml version="1.0"?> 
    <root>
        <item>
            <name>Tab instead of Command+[</name>
            <identifier>private.swap_tab_and_cmnd+[</identifier>
            <autogen>
                __KeyToKey__
                KeyCode::TAB, ModifierFlag::NONE,
                KeyCode::BRACKET_RIGHT, ModifierFlag::COMMAND_L | ModifierFlag::NONE
            </autogen>
        </item> 
    </root>
    
  5. Go to Preferences ~> "Change Key" ~> click on "Reload XML" button

    转到首选项〜>“更改密钥”〜>单击“重新加载XML”按钮

  6. Put a tick in the checkbox which is called "Tab instead of Command+["

    勾选一个名为“Tab而不是Command + [”的复选框

  7. Enjoy

如何在Xcode中缩进多行?

PS: Please note that this setting works globally on your machine, not just in Xcode. However, I am sure that google might help you to find a solution how to customize it even further, applying the changes only to Xcode.

PS:请注意,此设置在您的计算机上全局运行,而不仅仅在Xcode中。但是,我确信谷歌可能会帮助您找到解决方法,如何进一步自定义它,仅将更改应用于Xcode。

#14


Danish Keyboard

Unindent: + + 8

Unindent:⌘+⌥+ 8

Indent: + + 9

缩进:⌘+⌥+ 9

#15


Basically [ for left multiple indent and ] right multiple indent.

基本上⌘[对于左多个缩进和⌘]右多个缩进。

For TR keyboard you can left multiple indent with 8 and right multiple indent with 9.Because both 8 and 9 are ALT characters of the these nums.Or if you have a keyboard which has [ or ] characters are belong to ALT() property so you can use KEYBOARD CHARACTER

对于TR键盘,您可以使用⌘⎇8保留多个缩进,使用⌘⎇保留右多个缩进.9。因为8和9都是这些nums的ALT字符。或者如果您的键盘具有[或]字符属于ALT (⎇)属性,因此您可以使用⌘⎇KEYBOARDCHARACTER

#16


Since I didn't see an update to this question for the current version of Xcode, I thought I'd add that in Xcode 9.3, Tab works for indenting selected line(s) of text as well as moving from one autocomplete field to another.

由于我没有看到当前版本的Xcode对此问题的更新,我想我会在Xcode 9.3中添加它,Tab用于缩进所选的文本行以及从一个自动完成字段移动到另一个。

#17


Tab for Indent SHIFT + Tab Re-indent

缩进SHIFT + Tab重新缩进的选项卡

#18


In Xcode 4.2 auto-indenting is pretty good. You can now indent a selection of code by pressing the Tab key. I find that Xcode generally formats code really well automatically, and you rarely have to move things around yourself. I find it faster to select a piece of code, right-click and choose Structure -> Re-indent if some code looks messy.

在Xcode 4.2中,自动缩进非常好。您现在可以通过按Tab键缩进选择的代码。我发现Xcode通常可以很好地自动格式化代码,而且你很少需要自己动手。我发现选择一段代码会更快,右键单击并选择Structure - > Re-indent,如果某些代码看起来很乱。

#1


The keyboard shortcuts are +] for indent and +[ for un-indent.

键盘快捷键为⌘+]表示缩进,⌘+ [表示取消缩进。

  • In Xcode's preferences window, click the Key Bindings toolbar button. The Key Bindings section is where you customize keyboard shortcuts.
  • 在Xcode的首选项窗口中,单击“键绑定”工具栏按钮。 Key Bindings部分是您自定义键盘快捷键的地方。

#2


Select your code to reindent, then Go to

选择要重新启动的代码,然后转到

Editor -> Structure -> Re-Indent

编辑器 - >结构 - >重新缩进

#3


+ [ and + ] are the equivalents to shift+tab in Xcode.

⌘+ [和⌘+]是Xcode中shift + tab的等效项。

#4


Here are the shortcuts, to format the code in XCode

以下是格式化XCode中代码的快捷方式

  1. Format entire code (entire class/controller)

    格式化整个代码(整个类/控制器)

    Select the entire code and press control+I on mac to format your code.

    选择整个代码并按下Mac上的control + I来格式化代码。

  2. Format particular block of code

    格式化特定的代码块

    Select the code and press:

    选择代码并按:

    • +] for right move (indent)
    • ⌘+]用于右移(缩进)

    • +[ for left move (un-indent)
    • ⌘+ [用于左移(非缩进)

Note: as per @JavierGiovannini sugesstion you can do using Editor Menu option

注意:根据@JavierGiovannini sugesstion,您可以使用Editor Menu选项

  1. Select your code and navigate to Editor > Structure > Re-Indent
  2. 选择代码并导航到Editor> Structure> Re-Indent

#5


Another way to quickly reformat indenting is a quick cut and paste. +x and +v. I often find it faster than +[ or +] as you can do it with one hand (versus two) and it will reformat to the correct indent level in one shot.

快速重新格式化缩进的另一种方法是快速剪切和粘贴。 ⌘+ x和⌘+ v。我经常发现它比⌘+ [或⌘+]更快,因为你可以用一只手(相对于两只)来做它,并且它将在一次拍摄中重新格式化为正确的缩进级别。

#6


If you use synergy (to share one keyboard for two PCs) and PC(MAC) in which you are using xcode is slave, and master PC is Windows PC

如果您使用synergy(为两台PC共享一个键盘)和使用xcode的PC(MAC)是奴隶,而主PC是Windows PC

keyboard shortcuts are alt+] for indent and alt+[ for un-indent.

键盘快捷键是alt +]用于缩进和alt + [用于un-indent。

Update:

But from synergy version 1.5 working +[ for indent and +] for un-indent

但是从synergy版本1.5开始工作⌘+ [用于缩进和⌘+]用于非缩进

#7


In Xcode 9, you can finally use Tab and Shift+Tab to indent multiple lines of code. Yay!

在Xcode 9中,您最终可以使用Tab和Shift + Tab来缩进多行代码。好极了!

#8


First, select all code using command+a

首先,使用命令+ a选择所有代码

Second, hold key ctr and then press i

其次,按住键ctr,然后按i

the whole selected code will nicely indent.

整个选定的代码将很好地缩进。

#9


Select "Tab key: Indents always" in Preferences->Text Editing->Indentation Then you can indent a single line or a selection of lines by pressing TAB or SHIFT+TAB Sadly this removes altogether the possibility to insert tabs where you want, and conflict badly with the tab key being used to switch between "autocompletion fields".

在首选项 - >文本编辑 - >缩进中选择“Tab键:始终缩进”然后您可以通过按TAB键或SHIFT + TAB缩进单行或选择行。遗憾的是,这样可以完全删除在所需位置插入制表符的可能性,以及用于在“自动填充字段”之间切换的tab键严重冲突。

I guess we need more tab keys in the keyboard, one is not enough...

我想我们在键盘上需要更多的Tab键,其中一个还不够......

#10


For those of you with Spanish keyboard on mac this are the shortcuts:

对于那些在Mac上使用西班牙语键盘的人来说,这是快捷方式:

+ + [ for un-indent

⌘+⌥+ [用于取消缩进

+ + ] for indent

缩进的⌘+⌥+]

#11


here all the important shortcuts from another question answered on stack overflow 如何在Xcode中缩进多行?

这里所有重要的快捷方式来自另一个问题,在堆栈溢出时回答

#12


To all frenchs Xcoders around, the default keybindings are as follow :

对于所有法国人Xcoders,默认的键绑定如下:

Indent selection:

+ + + )

⇧+⌘+⌥+)

Unindent selection:

+ + + (

⇧+⌘+⌥+(

#13


If you want to remap the standard Xcode's +[ to TAB, you can do the following:

如果要重新映射标准Xcode的⌘+ [到TAB,您可以执行以下操作:

  1. Install a program called "Karabiner".

    安装一个名为“Karabiner”的程序。

  2. Go to Preferences ~> "Misc and Uninstall"

    转到首选项〜>“其他和卸载”

  3. In "Custom Setting" section click on the "Open private.xml" button

    在“自定义设置”部分中,单击“打开private.xml”按钮

  4. Open the file and replace its content with the following code:

    打开文件并使用以下代码替换其内容:

    <?xml version="1.0"?> 
    <root>
        <item>
            <name>Tab instead of Command+[</name>
            <identifier>private.swap_tab_and_cmnd+[</identifier>
            <autogen>
                __KeyToKey__
                KeyCode::TAB, ModifierFlag::NONE,
                KeyCode::BRACKET_RIGHT, ModifierFlag::COMMAND_L | ModifierFlag::NONE
            </autogen>
        </item> 
    </root>
    
  5. Go to Preferences ~> "Change Key" ~> click on "Reload XML" button

    转到首选项〜>“更改密钥”〜>单击“重新加载XML”按钮

  6. Put a tick in the checkbox which is called "Tab instead of Command+["

    勾选一个名为“Tab而不是Command + [”的复选框

  7. Enjoy

如何在Xcode中缩进多行?

PS: Please note that this setting works globally on your machine, not just in Xcode. However, I am sure that google might help you to find a solution how to customize it even further, applying the changes only to Xcode.

PS:请注意,此设置在您的计算机上全局运行,而不仅仅在Xcode中。但是,我确信谷歌可能会帮助您找到解决方法,如何进一步自定义它,仅将更改应用于Xcode。

#14


Danish Keyboard

Unindent: + + 8

Unindent:⌘+⌥+ 8

Indent: + + 9

缩进:⌘+⌥+ 9

#15


Basically [ for left multiple indent and ] right multiple indent.

基本上⌘[对于左多个缩进和⌘]右多个缩进。

For TR keyboard you can left multiple indent with 8 and right multiple indent with 9.Because both 8 and 9 are ALT characters of the these nums.Or if you have a keyboard which has [ or ] characters are belong to ALT() property so you can use KEYBOARD CHARACTER

对于TR键盘,您可以使用⌘⎇8保留多个缩进,使用⌘⎇保留右多个缩进.9。因为8和9都是这些nums的ALT字符。或者如果您的键盘具有[或]字符属于ALT (⎇)属性,因此您可以使用⌘⎇KEYBOARDCHARACTER

#16


Since I didn't see an update to this question for the current version of Xcode, I thought I'd add that in Xcode 9.3, Tab works for indenting selected line(s) of text as well as moving from one autocomplete field to another.

由于我没有看到当前版本的Xcode对此问题的更新,我想我会在Xcode 9.3中添加它,Tab用于缩进所选的文本行以及从一个自动完成字段移动到另一个。

#17


Tab for Indent SHIFT + Tab Re-indent

缩进SHIFT + Tab重新缩进的选项卡

#18


In Xcode 4.2 auto-indenting is pretty good. You can now indent a selection of code by pressing the Tab key. I find that Xcode generally formats code really well automatically, and you rarely have to move things around yourself. I find it faster to select a piece of code, right-click and choose Structure -> Re-indent if some code looks messy.

在Xcode 4.2中,自动缩进非常好。您现在可以通过按Tab键缩进选择的代码。我发现Xcode通常可以很好地自动格式化代码,而且你很少需要自己动手。我发现选择一段代码会更快,右键单击并选择Structure - > Re-indent,如果某些代码看起来很乱。