如何在 Xcode 8 中创建删除行键盘快捷键?Xcode 3 解决方案不再适用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5834096/
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-09-09 03:55:07  来源:igfitidea点击:

How do I create a Delete-Line Keyboard shortcut in Xcode 8? The Xcode 3 solutions do not work anymore

xcodemacosxcode4xcode8keyboard-shortcuts

提问by Klaas

In previous versions of Xcodeit was possible to create a key binding to delete the current line. There were different solutions and they are described for example here:

在以前的版本中Xcode,可以创建一个键绑定来删除当前行。有不同的解决方案,例如在此处进行了描述:

Xcode: Delete line hot-key

Xcode:删除行热键

Xcode duplicate/delete line

Xcode重复/删除行

http://bigdiver.wordpress.com/2009/09/11/configure-homeend-key-bidings-on-mac-os-x/

http://bigdiver.wordpress.com/2009/09/11/configure-homeend-key-bidings-on-mac-os-x/

http://www.betadesigns.co.uk/Blog/2010/02/03/custom-xcode-shortcuts/

http://www.betadesigns.co.uk/Blog/2010/02/03/custom-xcode-shortcuts/

All solutions include modifying one of these files:

所有解决方案都包括修改以下文件之一:

~/Library/Application Support/Xcode/Key Bindings/*.pbxkeys
~/Library/KeyBindings/DefaultKeyBinding.dict
~/Library/KeyBindings/PBKeyBinding.dict

~/Library/Application Support/Xcode/Key Bindings/*.pbxkeys
~/Library/KeyBindings/DefaultKeyBinding.dict
~/Library/KeyBindings/PBKeyBinding.dict

A good resource for the existing shortcuts in Xcode 4is http://cocoasamurai.blogspot.com/2011/03/xcode-4-keyboard-shortcuts-now.html. There are many listed regarding deletion, but none for "delete line".

现有快捷方式的一个很好的资源Xcode 4http://cocoasamurai.blogspot.com/2011/03/xcode-4-keyboard-shortcuts-now.html。列出了许多关于删除的内容,但没有列出“删除行”。

BUT, these solutions do NOTwork since Xcode 4.

但是这些解决方案不是因为工作Xcode 4

Update: Issue is still the same as of Xcode 5.1.1

更新:问题仍然与Xcode 5.1.1

Update: Issue is still the same as of Xcode 6.1 GM Seed 2

更新:问题仍然与Xcode 6.1 GM Seed 2

Update: Still applies to Xcode Version 7.3 (7D175) as of 26th April 2016

更新:截至 2016 年 4 月 26 日,仍适用于 Xcode 版本 7.3 (7D175)

Update: Six years later Xcode 8.3 has a built-in solution. See answer below.

更新:六年后 Xcode 8.3 有一个内置解决方案。请参阅下面的答案。

采纳答案by Paulo Mattos

Looks like Xcode 8.3finally added the Delete Linecommand :)

看起来Xcode 8.3终于添加了删除行命令:)

By default, no keyboard shortcut is assigned to it, so you will need to add one yourself in:

默认情况下,没有为其分配键盘快捷键,因此您需要自己添加一个:

  1. Open Xcode> Preferences> Key Bindings
  2. Search for Delete Line
  3. Add a keyboard shortcut in the Keycolumn
  1. 打开Xcode>首选项>键绑定
  2. 搜索 Delete Line
  3. Key列中添加键盘快捷

Xcode Key Bindings

Xcode 键绑定

回答by Vasu

After a lot of search here and there, I came to the following solution:

在这里和那里进行了大量搜索后,我得出了以下解决方案:

Press and hold controlkey, and then press AKK.

按住control键,然后按AKK

(Note: second Kdeletes the empty line).

(注意:秒K删除空行)。

Actually it is a combination of two commands: Move to Beginning of Paragraph& Delete to End of Paragraph. You may also use/set other key(s) and also may use/set/change key bindings for other command(s) e.g. Move to Beginning of Line& Delete to End of Line. I used the above (default) key bindings as they suited me.

实际上它是两个命令的组合:Move to Beginning of Paragraph& Delete to End of Paragraph。您还可以使用/设置其他键,也可以使用/设置/更改其他命令的键绑定,例如Move to Beginning of Line& Delete to End of Line。我使用了上述(默认)键绑定,因为它们适合我。

Edit:

编辑:

To delete more than one consecutivelines, first press and hold controlkey, and then pressA(just to go to the beginning of the paragraph/line), then press and hold Ktill all the lines are deleted (this would work as the cursor remains at the beginning).

要删除多于一行的连续行,首先按住control键,然后按A(只是转到段落/行的开头),然后按住K直到删除所有行(这会在光标停留在开头)。

Just to make task easy (for me), I have set commandDfor Delete to End of Paragraphand home/endto go to beginning/end of the paragraph.

只是为了让任务容易(对我来说),我已经设置commandDDelete to End of Paragraphhome/end去开始一段/结束。

回答by Rhubarb

Got it! This works in XCode 4.3 through 4.5 and requires no extra applications and is XCode specific.

知道了!这适用于 XCode 4.3 到 4.5,不需要额外的应用程序,并且是 XCode 特定的。

This solution is basically the same as the Duplicate Line command described here:

此解决方案与此处描述的 Duplicate Line 命令基本相同:

Edit the plist file

编辑 plist 文件

It's at /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

它位于 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKeyBindingSet.plist

Note that:

注意:

  • the linked answer suggests using XCode for this, but I find that this crashes XCode
  • this path is for Lion - it may vary for other OS versions
  • you may have to sudo or change permissions to edit this file
  • close XCode before editing
  • 链接的答案建议为此使用 XCode,但我发现这会使 XCode 崩溃
  • 此路径适用于 Lion - 其他操作系统版本可能会有所不同
  • 您可能需要 sudo 或更改权限才能编辑此文件
  • 编辑前关闭 XCode

Add a new <dict>element

添加新<dict>元素

Add this text just ABOVE the close of the <dict>at the bottom of this file

<dict>在此文件底部的结尾处添加此文本

    <key>Custom</key>
    <dict>
      <key>Delete Current Line In One Hit</key>
      <string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string> 
    </dict>

Note that:

注意:

  • This is insidethe existing <dict>so there is a </dict></plist>after this
  • This associates the named action "Delete Current Line In One Hit" to the three key actions in the string. I played around with different options here to get the right combination. For instance, if you try moveToBeginningOfLine, deleteToEndOfLine instead of vice versa, then using it on empty lines will delete the line andthe entire next line. Which is unpleasant
  • 这是现有的内部<dict>因此</dict></plist>在此之后
  • 这将命名操作“一次命中删除当前行”与字符串中的三个关键操作相关联。我在这里尝试了不同的选项以获得正确的组合。例如,如果您尝试 moveToBeginningOfLine、deleteToEndOfLine 而不是反之亦然,那么在空行上使用它会删除该行整个下一行。哪个不愉快

Save this and open XCode

保存并打开 XCode

Add a new key binding in XCode

在 XCode 中添加新的键绑定

See the screenshots for this in the linked answeror do this:

链接的答案中查看此屏幕截图或执行以下操作:

  1. Open XCode Preferences
  2. Select Key Bindings
  3. Click on All
  4. In the SEarch box type "Delete" and search for "Delete Current Line in One Hit"
  5. Add your new binding.
  1. 打开 XCode 首选项
  2. 选择键绑定
  3. 点击全部
  4. 在搜索框中键入“删除”并搜索“一键删除当前行”
  5. 添加您的新绑定。

This should do it but for one caveat. I could never get XCode to savethe new binding. The same problem is described herethough my workaround had a slight variation:

这应该这样做,但有一个警告。我永远无法让 XCode保存新绑定。尽管我的解决方法略有不同,但此处描述相同的问题:

  1. Create a duplicate of the Default bindings (hit the + button at the bottom left of the Bindings window and create a duplicate)
  2. Set the new binding in the duplicate
  3. Close the preference dialog (that's the variation!)
  4. Open preferences again and make a new duplicate of the old duplicate and delete the old duplicate.
  1. 创建默认绑定的副本(点击绑定窗口左下角的 + 按钮并创建副本)
  2. 在副本中设置新绑定
  3. 关闭首选项对话框(这是变体!)
  4. 再次打开首选项并制作旧副本的新副本并删除旧副本。

What a rigmarole, eh? But it works. Now I have Ctrl+D mapped to delete the entire current line in XCode 4.3 on Lion.

多么啰嗦啊?但它有效。现在我已映射 Ctrl+D 以删除 Lion 上 XCode 4.3 中的整个当前行。

回答by Wooseong Kim

I found a temporary workaround for Xcode 9.

我找到了Xcode 9的临时解决方法。

Open the Xcode/Content/Frameworks/IDEKit.framework/Resources/IDETextK??eyBindingSet.plistand add the lines below.

打开Xcode/Content/Frameworks/IDEKit.framework/Resources/IDETextK??eyBindingSet.plist并添加以下行。

It mimics the action which happens in Xcode 8 with limited support. It only can remove just one line but I'm satisfied somehow. I hope this will be fixed soon.

它模仿 Xcode 8 中发生的动作,支持有限。它只能删除一行,但我以某种方式感到满意。我希望这会很快得到解决。

<key>Customized</key>
<dict>
    <key>Delete Line</key>
    <string>moveToBeginningOfLine:, deleteToEndOfLine:, moveDown:, deleteBackward:</string>
</dict>

After restart Xcode, you can see the customed item in Key Bindingslike below.

重启 Xcode 后,您可以看到Key Bindings如下图所示的自定义项。

enter image description here

在此处输入图片说明

Bind and use it!

绑定并使用它!

Edit) I think below one is better than original one.

编辑)我认为下面的一个比原来的好。

<string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string>

回答by borisdiakur

I use BetterTouchTool, it's pretty cool:

我使用BetterTouchTool,它很酷:

enter image description here

在此处输入图片说明

回答by RabbitHole

Rhubarb's answer is correct and works but one thing, there is more simple command in plist file. Follow all instructions of Rhubarb's but

Rhubarb的答案是正确的并且有效,但有一件事,plist 文件中有更简单的命令。遵循大黄的所有说明,但

instead of

代替

<string>moveToEndOfLine:, deleteToBeginningOfLine:, deleteToEndOfParagraph:</string>

use below

下面使用

<string>selectLine:, delete:</string>

This is more clear and throw away any possibility of malfunction.

这样就更清楚了,并排除了任何故障的可能性。

I want to write this as a comment, but my "reputation" doesn't allow me to do that.

我想把它写成评论,但我的“声誉”不允许我这样做。

回答by Bary Levy

try this:

尝试这个:

  1. First open Xcode->Preferences

  2. Select Key Bindigs

  3. search Delete Paragraph

  4. Change Delete Paragraphto your shortcut

  1. 首先打开Xcode->Preferences

  2. 选择键绑定

  3. 搜索删除段落

  4. 删除段落更改为您的快捷方式

enter image description here

在此处输入图片说明

回答by Paul Waldo

If you are a Keyboard Maestro user, just create a macro that does the following:

如果您是 Keyboard Maestro 用户,只需创建一个执行以下操作的宏:

  • Command Left Arrow
  • Shift Down Arrow
  • Delete
  • 命令左箭头
  • Shift 向下箭头
  • 删除

I assigned it to Command-D, and made it available only in Xcode.

我将它分配给 Command-D,并使其仅在 Xcode 中可用。

回答by Simon Epskamp

An easy solution for XCode 5 and 6:

XCode 5 和 6 的简单解决方案:

Install alcatraz: http://alcatraz.io/then use it to install the "XCodePlus delete line" plugin:

安装alcatraz:http: //alcatraz.io/然后用它来安装“ XCodePlus 删除行”插件:

Window > Package manager

Window > Package manager

There are lots of other awesome pluginsas well.

还有很多其他很棒的插件

回答by onteria_

It looks as though XCode now uses a plist file versus a dict file, and the new format doesn't support adding multiple commands to a key binding. There also doesn't appear to be a way to add custom commands to bind to either. The only way I've found to get something working is by setting a key binding for the separate actions, such as ^Dfor deleteToEndOfLine:, then ^Lfor moveToBeginningOfLine:, giving you ^L+^Dto give you the combined effect.

看起来 XCode 现在使用 plist 文件而不是 dict 文件,并且新格式不支持向键绑定添加多个命令。似乎也没有办法添加自定义命令以绑定到任何一个。我发现让某些事情起作用的唯一方法是为单独的操作设置键绑定,例如^Dfor deleteToEndOfLine:,然后^Lfor moveToBeginningOfLine:,从而为您^L+^D提供组合效果。