82

When I follow all the instructions (including apple's video) to drag text to create a code snippet in xcode, I get a different system behavior: all I see is the selection ibeam... I can't drag. It just ends up selecting text again. Am i missing some obvious Apple-ism? Thanks

TigerCoding
  • 8,710
  • 10
  • 47
  • 72
Jack Bellis
  • 1,177
  • 8
  • 10
  • Have this problem on Xcode 8, but Xcode 9 works – onmyway133 Aug 24 '17 at 11:18
  • OMFG. You need to carefully mouse on top of the actual literal selected text. If you click in empty space, even though it is highlighted, it all deselects. This is extremely stupid. There a billion ways to make a selection go poof. Why make this useful dragging operation so ridiculously hard? – Dirk Bester Dec 18 '22 at 03:19
  • Use the force touch on the trackpad to drag selected text – Mohsen Fard Mar 06 '23 at 15:57

6 Answers6

148

Yes, I ran into the same problem, and Apple-ism seems to be a good term for this issue :)

It works this way:

  • Select (or highlight) the code your want to turn into a snippet
  • EDIT: Click and hold (do not drag!) on any part of the selected text for a short moment (like 2 seconds, sometimes 8 seconds)
  • EDIT: The mouse cursor will change icon (not on El Capitan).
  • Now you can drag the code over the snippets library, where a green + icon will appear. EDIT: On El Capitan the green icon does not appear, but the frame of the code snippets window will become thinner.
brainray
  • 12,512
  • 11
  • 67
  • 116
  • If you don't see your snippet appear, scroll all the way down to the bottom. Click it, and then an edit button can be used to configure it. – TigerCoding Jul 13 '15 at 01:16
  • This still works in Xcode 6. ...just in case anyone was wandering. – Andrej Jul 31 '15 at 08:46
  • 3
    Following these instructions I fail more often than I succeed. I don't understand how it can be so hard to drag and drop some text. Also the user interface for creating code snippets could be much improved. This is an extremely awkward way of managing your code snippets. – Erik B Nov 23 '15 at 15:57
  • 1
    if you use a trackpad you can use the three finger drag gesture (https://support.apple.com/en-us/HT204609) in place of steps 2 and 3 – flochtililoch Dec 05 '15 at 17:18
  • 4
    On a Macbook with Force Touch trackpad running 10.11 El Capitan and 7.2 Xcode, the solution of selecting text, click and hold, then drag to snippets library works, but with the following caveat: you must click in the blank space to the right of your text within the selection, and when you hold, the cursor will not change. I count until 3 and then drag. It's pretty reliable, although I forget about clicking in the blank space pretty frequently ;) – Dribbler Jan 09 '16 at 14:30
  • no it doesn't work, captan 10.11 XCode 7.2, and I enabled three fingers dragging - still nothing works. – Injectios Jan 20 '16 at 09:03
  • Definitely works on El Capitan, just tried. But the cursor icon doesn't seem to change when the selection changes to be draggable. – brainray Jan 21 '16 at 12:54
  • @brainray, its great that you're still updating your answer after such a long time. Cheers! +1 – Hemang Jan 21 '16 at 12:58
  • After trying for a few minutes without success, I discovered there was an build error in the file I was trying to drag the code. After I fixed the build error, Xcode allowed the snippet to be dragged after 2 seconds holding the mouse down. – Gui Moura Feb 06 '16 at 00:59
  • 1
    My problem was that I wasn't waiting long enough. Rather than the usual 2 seconds, it was taking like 8 seconds before the cursor would change. – Chris Garrett Mar 05 '16 at 12:54
  • 1
    I don't understand the user experience here, if I select and drag (to move) or alt+drag (to copy) is to speed up my coding, waiting 8 seconds to be able to do it have no sense (is faster to copy + paste and you exercise more your fingers) – Jorge Arimany Oct 09 '16 at 11:49
  • @JorgeArimany I think Chris (and others) are talking about _creating_ a snippet. _Using_ the snippet works as advertised. – brainray Oct 09 '16 at 12:09
  • 4
    In xcode 8 / macos sierra it takes me 10 seconds to activate! The cursor changes from an "I-beam" to a regular arrow cursor. WTF, this must surely be the most hidden UI interaction Apple ever dreamed up. Why not just make a "create snippet" button and let me paste the code in? – mpoisot Nov 11 '16 at 16:35
14

This happened to me as well. The given solution didn't work but still needed to create snippets. This is a workaround although not the best option, it works.

  1. From Xcode, select the snippet window, and leave it selected.
  2. Open a text editor app.
  3. From Xcode, select and copy the code you want to use to create your snippet.
  4. Paste it into your text editor and then select all of it.
  5. Now drag&drop normally from the text editor into the snippets window in Xcode.

As said, not the best option but it works.

(Note for Xcode 8.1) Sometimes selecting the text and maintain the mouse button clicked for 3..8 seconds the cursor turns into an arrow and it's possible to D&D. Too bad it only works sometimes...

MLBDG
  • 1,357
  • 17
  • 23
  • 1
    ML, if you're a Windows user, I'd suspect, despite your comment, that the problem is the following, because the explanations above are ever so slightly lacking. The drag technique requires what I'm told is the perfectly routine and expected (!) Mac method for drag-and-drop: after 1) selecting (highlighting) some text, and then 2) clicking it in anticipation of dragging, one must wait approximately 1/4 second before actually moving the mouse to drag. I believe the display gives some feedback cue. In Windows there is no dependency on doing that 1/4 second delay. Please report back. Thanks – Jack Bellis Sep 09 '15 at 19:51
  • 1
    Jack, I'm a Mac user. There could be a bug which is still not fixed by Apple. Some users (me included) can use "Drag&Drop" without problem in their system. However, specifically inside Xcode can happen that Drag&Drop does not work, even "Selecting the text, wait until the cursor changes to arrow etc". So I believe this workaround, as I said, not the best option but functional until this bug is fixed. – MLBDG Sep 09 '15 at 20:04
  • Using a second app was the only way that worked for me. Why no buttons Apple? Shortcut maybe, drag and drop... not necessary. – WCByrne May 02 '17 at 03:18
9

On High Sierra 10.13.3 and XCode 9 I faced the same problem. The decision that works for me is selecting code on a blank space before the first symbol and then dragging it to the snippets library.

Important: you need to drag only from the blank space (look at the screenshot below). Otherwise you will reselect your code again.

The cursor changes only when you start dragging, so don't be confused and don't wait until it changes.

enter image description here

joliejuly
  • 2,127
  • 1
  • 21
  • 24
2

I've run into this problem with Xcode 7.2 on Mac OS X 10.11.2 (El Capitan). Click and hold on the selection and the cursor never changes from the I-beam to the arrow for dragging.

Although MLBDG's answer is a usable workaround, I found that quitting and restarting Xcode fixed the problem. This might not work for everyone, but it's worth a try if you're having difficulty.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0
  1. just highlight your text
  2. copy in filter text field that u can see on your bottom of snippet library
  3. paste the text
  4. now select the text and drag from there to snippet library
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

The answer is really !undisobvious. You need to select a chunk of text. Then click on actual text characters, but without moving the mouse. Only then will the gods of XCode bequeath unto you a draggable snippet of code to do with as you please. Should you dare to miss the text and click in empty but selected space, the demons of XCode will strike down your selection and make you start from scratch. The unfortunate will not even realise this feature actually exists.

Hold option to duplicate it. For instance to make a duplicate case statement without destroying your copy buffer, then replace the case you cleverly have ready to paste during your search.

As a UI designer it is not obvious to me why this has such an unreliable twitchy implementation.

Dirk Bester
  • 1,791
  • 19
  • 21