How do I create a Delete-Line Keyboard shortcut in

2019-01-16 03:40发布

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

Xcode: Delete line hot-key

Xcode duplicate/delete line

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/

All solutions include modifying one of these files:

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

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

BUT, these solutions do NOT work since Xcode 4.

Update: Issue is still the same as of Xcode 5.1.1

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

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

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

13条回答
smile是对你的礼貌
2楼-- · 2019-01-16 04:04

try this:

  1. First open Xcode->Preferences

  2. Select Key Bindigs

  3. search Delete Paragraph

  4. Change Delete Paragraph to your shortcut

enter image description here

查看更多
forever°为你锁心
3楼-- · 2019-01-16 04:06

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

Press and hold control key, and then press AKK.

(Note: second K deletes the empty line).

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.

Edit:

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

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

查看更多
时光不老,我们不散
4楼-- · 2019-01-16 04:09

First open Xcode->Preferences

Select Key Bindigs

Then search Delete

Change the Delete The End Of Line to your shortcut(I prefer CMD + D)

Thats AllHere is the screenshot

查看更多
地球回转人心会变
5楼-- · 2019-01-16 04:12

seems as though it can't be done now... feel free to correct me if I am wrong... but here is my new solution...

go to xcode preferences, then the key bindings tab. duplicate the default set, so that you wont be mad at me when the suggestion that i make breaks something that you will use.

find 2 keys that are adjacent and not used for any commands that you use... ";","'" perhaps.(I don't use command n for new file, so I picked n,m)

set the first key to "Move to Begging of Line" and the second key to "Delete to end of line"

then use that sequence to delete a line...

alternate would be set something to select line, then just use delete.

查看更多
乱世女痞
6楼-- · 2019-01-16 04:13

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 ^D for deleteToEndOfLine:, then ^L for moveToBeginningOfLine:, giving you ^L+^D to give you the combined effect.

查看更多
小情绪 Triste *
7楼-- · 2019-01-16 04:14

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

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

Edit the plist file

It's at /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

Add a new <dict> element

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

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

Note that:

  • This is inside the 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 and the entire next line. Which is unpleasant

Save this and open XCode

Add a new key binding in XCode

See the screenshots for this in the linked answer or 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.

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

  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.

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.

查看更多
登录 后发表回答