How do I build an Xcode shortcut for frequently used code snippet? [duplicate] - xcode

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

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.

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.
From Xcode, select the snippet window, and leave it selected.
Open a text editor app.
From Xcode, select and copy the code you want to use to create your snippet.
Paste it into your text editor and then select all of it.
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...

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.

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.

just highlight your text
copy in filter text field that u can see on your bottom of snippet library
paste the text
now select the text and drag from there to snippet library

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.

Related

Xcode 11 Won't Open New Editor Pane

So Xcode 11 changed the way editor panes are added and removed. Ok, fine, I got used to it soon enough. But a few days after using the Xcode 11 GM build, the option to add an editor is suddenly disabled.
Could be a bug, could be a 'feature' that I've inadvertently triggered. Neither the menu, toolbar button nor shortcut (^⌘T) will give me another editor (though they all did with the same build yesterday).
Any ideas?
(Note: this screenshot has the Navigator pane hidden for simplicity. Its presence/absence doesn't help with this issue)
Ok, this is embarrassing. I was in a 'focus' mode and could only add editors after I had 'hidden focus'. There are a few ways to do this:
View > Editor > Hide Focus
Tap the 'arrows pointing at each other' icon in the top left of the editor toolbar
^⇧⌘↩︎
ps. I was seriously tempted to delete my question when I realised how dumb I'd been, but I'm leaving it up as I suspect this UI quirk might also trip up others (let me know if it does, it'll help me feel like less of a fool)

Why would Xcode stop drawing tabs when indent level is greater than three?

For some reason Xcode 4.5 and 4.4.1 have both stopped displaying indented text at the proper level if there are more than three Tab characters of leading whitespace on the lines.
Easier to show than describe:
There are five leading tabs on the line the arrow is pointing to, but Xcode is drawing it as if there were only three. Using the left and right arrow keys confirms the Tabs are there.
I have deleted the derived data associated with my projects, deleted the project.xcworkspace file and xcuserdata directories within the project package, mucked around with various combinations of Indentation settings in Xcode's Preferences, all to no avail.
Newly created projects exhibit the same behavior. Changing the Source Editor font preferences have no effect either. This is in OS X 10.8.1.
Help -- my brain can't understand the code when it doesn't "look right"! What else can I try?
So, I filed a Radar, then heard back from a friend of mine who was having the same problem. Are you using the new SourceCode Pro font from Adobe? Your screenshot makes it look like you might be. Try switching back to a different theme and restarting Xcode? That's what I did, and it seems to be fine.
Eventually I removed (after quitting Xcode) the entire UserData folder in ~/Library/Developer/Xcode and that seems to have fixed the problem.
Hopefully it won't take more than another half hour to restore all the key equivalents and syntax coloring that I am accustomed to.
Leaving the question 'alive' in case it happens to someone else.

Xcode Code Completion "flicker"

I've recently upgraded from Xcode 4.3 to 4.4 and I'm about to bash my screen in with frustration.
The problem I'm having is with the code completion. As I type a variable or method name, the completion window pops up with the suggestions like it's supposed to. But if I attempt to use the cursor keys to scroll between the suggestions, the suggestions disappear and the cursor moves in the code, not between the suggestions. If I press enter to accept the default suggestion, it again dismisses the suggestions and inserts a return into the code taking the cursor to the next line.
This doesn't happen every time I attempt code completion, but it does occur more often than not. For a given name I'm trying to complete, it either works or it doesn't (e.g. if I press escape to get the suggestions back, it will disappear just as quickly as the first time it disappeared for that line/name I was trying to complete). When I get to the next line of code, it's a whole new bet whether it will work or not. It seems that the closer to the actual completion of the name I am, the more likely it is to actually work and accept the suggestion or move between the suggestions. Sometimes, if I act quickly enough, I can move the highlighted suggestion one or two lines before the suggestions disappear. I'm aware that if you don't select a suggestion quickly enough that it's supposed to eventually remove the suggestions (on the order of several seconds), but this behavior is not what I'm describing. I'd be VERY happy at this point if I had a single second to actually respond and select a suggestion from the pop-up.
So far I've tried a clean of the project, deleting all of the project's derived data, and restarting xcode. All of which don't seem to affect the behavior. Any ideas? I've searched stackoverflow and google and haven't found any other people reporting this problem ...
Thanks for any ideas,
Will
UPDATE:
After upgrading to the XCode 4.6 release version (and 4.5 somewhere in the middle there), this problem is still present ... and making edits in the left/main editor window is still a work-around.
I am seeing the same with Xcode 4.4.1. It seems to be related to where the completion window pops up. Debugger pane open or closed does not change anything here.
When in the Assistant Editor trying in the right pane the completion windows flickers. Same line of code in the left window works fine. Single editor works, too.
Try upgrading to Xcode 4.4.1. I used to have this problem, but I don't seem to have it after upgrading.
If this doesn't solve the problem, a crude workaround seems to be to put a lot of blank lines at the bottom of each file - the problem seems to occur from frequently when the file is scrolled all the way to the bottom.
There is a fix (at least Xcode 4.6.3): re-create the affected file by deleting it. Now create a new file and copy the content to it and drag it into the project - done. Fixes the issue here. It seems to be tied to certain files.

XCode 4 Cursor Jumps To First Line When Auto Suggesting

I have a problem in XCode 4.2 when I have the autosuggest feature turned on. Whenever an autosuggest (autocomplete) pops up, the cursor will jump to the top of the file, to the second character of the first line. This happens regardless of whether or not I select the autosuggest or take any other action. As soon as I turn off the autosuggest feature, this behavior stops happening.
I have turned off all key binding associated with jumping to the top of the page and have deleted all derived data for the project as per some suggestions I have heard, but have not been able to solve the problem.
Anybody have a solution?
I had similar problem Xcode cursor jumped randomly. It was triggered by an editor theme using an italic font. I changed the font to a regular one, and the problem was resolved.
Simple solution (or workaround):
Open Preferences.
Open Fonts & Colors tab.
Select Basic theme.
I got the hint from this page.
Just in case there is a bounty awarded :) let me repeat my "solution" from above: I suggest moving aside your /Developer folder and re-installing Xcode. Simply running the installer w/o removing the old /Developer folder may not replace all the necessary files. Good luck.
I have had a similar problem where the cursor jumped to the top and I couldn't scroll down. I don't know if it is the same problem that you have but there is no real solution. Its just one of Xcode's bugs there are a lot more. Open another file in the project and go back to the file you were working on, close and reopen the project or restart Xcode. Thats the only solutions I found.
If thats not working, then you can always re-install Xcode

Xcode: How to get page up and page down to move cursor?

This is a mac thing (because text edit does the same) but I just want to change this in xcode.
I'd like the cursor to move when I hit the page-up and page-down button. Kind of like the way windows or linux work. I know I can hold down the "option" button and then go page-up, page-down and it works - but I don't want to hold the option button.
I tried fiddling around with key bindings in xcode but couldn't get it, anyone know how?
In preferences, go to key bindings, type "page" in the search box, look for Page Down (Selection) / Page Up (Selection), click there and hit your pgdn/pgup keys.
Delete the pgdn/pgup keys from Scroll Page Up/Down.
Whit Xcode 6.1 (and >) is quite different, but similar to Colin's answer.
Xcode > Preferences > Key Bindings
Search for "Move Page Up": by default it's bindend with Alt+PgUp
You can change your binding simply with PgUp.
There may be conflicts, resolve it is suggested.
Than repeat the same for "Move Page Down".
Install KeyRemap4MacBook and life will be good. If you have a PC/Mac/Linux environment the MacOS keyboard inconsistencies can be a real pain. We have all of our Macs setup to work as PC keyboards work. Now switching from one system to the other several times a day is not a problem.

Resources