Xcode duplicate/delete line

前端 未结 22 1647
不思量自难忘°
不思量自难忘° 2020-12-04 04:59

Coming from Eclipse and having been used to duplicate lines all the time, it\'s pretty strange finding out that Xcode has no such function. Or does it?

I know it\'s

相关标签:
22条回答
  • 2020-12-04 05:16

    As Xcode 4 makes this difficult to implement using key bindings, I have resorted to using Keyboard Maestro (Quickeys should work well too, but it's not fully Lion compatible). For e.g., here is my Keyboard Maestro shortcut (bound to ⌘-⇧-D):

    http://bit.ly/thC6yf

    0 讨论(0)
  • 2020-12-04 05:19

    I know that this thread is quite old but I found another solution using BetterTouchTool, you can execute the Ctrl+A, Ctrl+K, Ctrl+K sequence in one shortcut by configuring like this in BTT:

    enter image description here

    (Use the Attach Additional Action Button to append the second and third shortcut)

    PS: Coming from Eclipse where Cmd+D is delete line ^^

    0 讨论(0)
  • 2020-12-04 05:19

    The solution by damien.flament works great and you may need set output to "Discard Output" see the pic below

    Userscripts setting

    P.S: I don't know how to comment his answer.

    0 讨论(0)
  • 2020-12-04 05:20

    As said above, close XCode and insert following to ~/Library/Application Support/Xcode/Key Bindings/ <user>.pbxkeys inside <dict> in <key>text</key> section:

        <key>^D</key>                                                                                                      
        <array>                                                                                                            
            <string>moveToLeftEndOfLine:</string>                                                                               
            <string>deleteToEndOfLine:</string>                                                                                      
        </array>                                                                                                           
        <key>^K</key>                                                                                                      
        <array>                                                                                                            
           <string>selectLine:</string>                                                                               
           <string>copy:</string>                                                                                     
           <string>moveToEndOfLine:</string>                                                                          
           <string>insertNewline:</string>                                                                            
           <string>paste:</string>                                                                                    
           <string>deleteBackward:</string>                                                                           
        </array>
    

    Start XCode and enjoy CTRL-SHIFT-D and CTRL-SHIFT-K.
    If you want CTRL-D and CTRL-K, use ^d and ^k in key definitions instead. Ensure there are no duplicate key bindings in the file.

    Works fine for me with XCode 3.2

    0 讨论(0)
  • 2020-12-04 05:21

    To delete a line: Ctrl-A to go to the beginning of the line, then Ctrl-K to delete it, and another time Ctrl-K to remove the empty line. (I do not use Xcode very often, but I'm used to that in Emacs and other text inputs with Emacs-like bindings, and it seems to work in Xcode too.)

    And to duplicate a line: I don't know of many programs that have a command for that, but usually I just use Copy+Paste - in Xcode it's CUA-like: Ctrl+A to go to the beginning of the line, Shift+ to select it, Command+C to copy and Command+*V to paste twice (once overriding the line and once appending to it).

    (from a person that types and edits text all the time, so often in different programs, and occasionally gets pissed at having to distract himself with a dumb widget while making a little correction in a text input, that he just cannot avoid remembering these sequences and habits)

    0 讨论(0)
  • 2020-12-04 05:22

    I tried the key bindings solution, but it I couldn't get it to work. However editing my XCode key bindings works like a charm. Here's how I made it.

    This solution does not alter the contents of the Clipboard!

    Open the XCode Key Bindings:

    alt text

    In the Edit User Scripts Dialog:

    1. Duplicate the "Move Line Down" script and rename it
    2. Duplicate the "Move Line Down.scpt" file, rename the script, select file via (double click) in Script Editor
    3. Edit the script (Opens "AppleScript Editor") and remove the "delete (paragraphs startLine through endLine)" passage.
    4. If you do not want to restart XCode, you seem to have to remove and re-add the script. Be sure that you have "Output" set to "Discard Output", otherwise you will have a "(null)" in your source file
    5. I slightly modified the scripts a little bit more to have the right lines selected:

    Duplicate Line Up:

    using terms from application "Xcode"
    tell first text document
        set {startLine, endLine} to selected paragraph range
    
        if startLine > 1 then
            set theText to (paragraphs startLine through endLine)
            set theText to (theText as string)
            make new paragraph at beginning of paragraph (startLine) with data theText
            set selected paragraph range to {endLine + 1, endLine + endLine - startLine + 1}
        else
            beep 1
        end if
    end tell
    end using terms from
    

    Duplicate Line Down:

    using terms from application "Xcode"
    tell first text document
        set {startLine, endLine} to selected paragraph range
        if endLine < (count paragraphs) then
            set theText to (paragraphs startLine through endLine)
            set theText to (theText as string)
            (* delete (paragraphs startLine through endLine) *)
            make new paragraph at beginning of paragraph (endLine + 1) with data theText
            set selected paragraph range to {startLine, endLine}
        else
            beep 1
        end if
    end tell
    end using terms from
    

    alt text

    0 讨论(0)
提交回复
热议问题