Making TextMate2 highlight every occurrence of a variable? - textmate

Is there a way to make TextMate2 highlight every occurrence of a variable when I place the cursor on the variable like in Eclipse?

Not exactly, however it is quite easy to get this selection. Once you have it, TextMate's multiple cursors afford easy refactoring. Lets start by discussing some shortcuts:
Ctrl + s gives you an "Incremental Search" dialog, reduced to a text bar along the bottom of the window, this far simpler than the command+f version. Pressing Escape will select the current result.
Command + e sets search query.
Option + Command + f searches the entire document for your query
So to highlight every instance you could
Select the string
Command + e (set search query)
Option + Command + f (select all instances in the document)
What is really neat about this is that it creates a selection and cursor on each search result, so:
If you wanted to refactor you could simply start typing the new variable name.
The cursors also support multiple selection copying and pasting, and some bundle actions.

Related

Global search in Xcode 10.1

I'm trying to search through my entire project using CMD + SHIFT + F.
When I'm searching for anything that contains a dot (.) in the query the search will not find anything even though I literally just copied it from the code. For example I am searching for this:
self.init
and I get "no results for self.init"
I can't find any option that maybe regular expressions would be activated.
How can I make XCode to find any string I type into the search field (even when it contains dots)?
Press command4, or shiftcommandf to open the left side menu. In there, you can search using regular text search, regex, and more.
When searching for plain text, make sure that you're not matching regex. For this, press the button next to Find and select Text from the dropdown:

Is there a shortcut to search the word under cursor in Xcode?

The functionality I'm talking about is in VI/VIM. ie. When the cursor is over the text of a word like say jiggle in command mode, press * to search for the next instance of jiggle. I use this all the time in VIM. Does such a shortcut exist for Xcode? or can we only double-click to highlight, CMD + C, CMD + F, CMD + V, and hit Enter?
Ok, Phrogz has a good solution, but I found a simpler way to do this...
To search consecutive instances of a word in Xcode:
Double-click the word you would like to find the next instance(s) of, then as Phrogz mentioned press ⌘ + E (Use Selection for Find) and then ⌘ + G (Find Next) to search for the next occurrence(s)
Cmd + Ctrl + T will invoke the menu item "Edit All in Scope". That will highlight all uses of the current identifier and allow you to edit all simultaneously.
Not exactly the same thing, but I find it very useful for the sort of case you're talking about. Even if I don't need to edit an identifier, it's a nice way to quickly see all the places its used.
If you select the word (⌥←,⌥⇧→) you can press ⌘ + E (Use Selection for Find) and then ⌘ + G (Find Next) to search for the next occurrence.
You can combine the command+e, command+g to a shortcut
Find a file IDETextKeyBindingSet.plist in directory "/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/"
Open this file by Xcode
Add a Dictionary named Customized under the Root
Add a String named Move to Next Instance under the Customized
and set the value to selectWord:, useSelectionForFind:, findNext:
IDETextKeyBindingSet.plist
Restart Xcode and set the Key Bindings
Find Move to Next Instance and set the key to option+↓ (or other key you prefered)
Key Bindings
To search a word in the currently opened file using Xcode 5 and above:
Double click the word and then
shift optioncommand E
Now you can move to the next/previous instance using:
command G
or
shiftcommandG

Does anyone know a visual studio keyboard short cut to swap around two sides of a statement?

Just wondering if anyone knows the keyboard shortcut to swap around two sides of a statement. For example:
I want to swap
firstNameTextbox.Text = myData.FirstName;
to
myData.FirstName = firstNameTextbox.Text;
Does anyone know the shortcut, if there is one? Obviously I would type them out, but there is a lot of statements I need to swap, and I think a shortcut like that would be useful!
Feel free to throw in any shortcuts you think are cool!
My contribution would be CTRL + E, D - this will format your code to Visual Studio standards! Pretty well known I'm guessing but I use it all the time! :)
UPDATE
Just to let everyone know, using a bit of snooping of the article that was posted, I managed to construct a regular expression, so here it is:
Find:
{.+\.Text = myData\..+};
And replace with:
\2 = \1;
Hopefully people can apply this to their own expressions they want to swap!
I think the following thread is a good place to begin with
Invert assignment direction in Visual Studio
Here's how I would go about doing that without a specific keyboard shortcut:
First, select the text you want to modify and replace
" = " with " = "
(the key here is to add a lot of spaces).
If you hold down Alt and use the mouse, you can select a "block" of code. Use this to select only the text on the right side of the equation (it's helpful to add extra white space here in your selection)
Use the same Alt + Left-Click combination to select the beginning of the left side (just select a blank area). You should be able to paste text into here.
If you added extra white space to the text you just added, just should be able to easily insert an = using the Alt + Click technique. Use the same trick to remove the equal sign that's dangling on the right side of your code block.
While this might not do exactly what you're looking for, I've found these tricks quite useful.
If you're using ReSharper, you can do this by pressing CtrlAltShift + ← or →
The feature is in Resharper. Select the code segment and click the content wizard, which is a pencil icon in the left corner reading View Actions List, then choose Reverse Assignment.
It is done.
swap-word is a VSCode extension which sounds like it would do what you want.
Quickly swap places two words or selections...
But I'm not sure if it is compatible with VS.
Since I was not happy with the answers where I need to enter complicated strings into the Visual Studio search/replace dialog, I wrote myself a little AutoHotkey script, that performs the swaps with only the need to press a keyboard shortcut. And this, no matter if you are in VS or in another IDE.
This hotkey (start it once simply from a textfile as script or compiled to exe) runs whenever Win+Ctrl-S is pressed
#^s Up::
clipboard := "" ; Empty the clipboard
Sendinput {Ctrl down}c{ctrl up}
Clipwait
Loop, Parse, clipboard, `n, `r ; iterates over seperates lines
{
array := StrSplit(RegExReplace(A_LoopField,";",""),"=") ; remove semicolon and split by '='
SendInput, % Trim(array[2]) . " = " . Trim(array[1]) . ";{Enter}"
}
return
Many more details are possible, e.g. also supporting code where lines end with a comma
...and I can put many more hotkeys and hotstrings into the same script, e.g. for my most mistyped words:
::esle::else ; this 1 line rewrites all my 'else' typos
I recommend using the find-replace option in Visual Studio. IMHO the REGEX string is not that complicated, and moreover, you don't need to understand the expression in order to use it.
The following regex string works for most programming languages:
([\w\.]+)\s*=\s*([\w\.]+)
For Visual Studio's you want to use $ argument in the replace text.
$2 = $1
Make sure to enable regex.
To do this in one shot, you can select a section of the document, and click the replace-all option.
Before:
comboBoxAddOriginalSrcTextToComment.SelectedIndex = Settings.Default.comboBoxAddOriginalSrcTextToComment;
comboBoxDefaultLanguageSet.SelectedIndex = Settings.Default.comboBoxDefaultLanguageSet;
comboBoxItemsPerTransaltionRequest.SelectedIndex = Settings.Default.comboBoxItemsPerTransaltionRequest;
comboBoxLogFileVerbosityLevel.SelectedIndex = Settings.Default.comboBoxLogFileVerbosityLevel;
comboBoxScreenVerbosityLevel.SelectedIndex = Settings.Default.comboBoxScreenVerbosityLevel;
After:
Settings.Default.comboBoxAddOriginalSrcTextToComment = comboBoxAddOriginalSrcTextToComment.SelectedIndex;
Settings.Default.comboBoxDefaultLanguageSet = comboBoxDefaultLanguageSet.SelectedIndex;
Settings.Default.comboBoxItemsPerTransaltionRequest = comboBoxItemsPerTransaltionRequest.SelectedIndex;
Settings.Default.comboBoxLogFileVerbosityLevel = comboBoxLogFileVerbosityLevel.SelectedIndex;
Settings.Default.comboBoxScreenVerbosityLevel = comboBoxScreenVerbosityLevel.SelectedIndex;
IMHO: It's better for a developer to learn to use the IDE (Integrated Development Environment), then to create new tools to do the same thing the IDE can do.

Visual Studio - Is there a keyboard combination to select an entire line?

I already know about Ctrl + L to delete an entire line...is there one to just select an entire line (which I can then copy and paste somewhere else...)
You can also use Ctrl + X to cut an entire line. Similarly, you can use Ctrl + C to copy an entire line.
As long as you don't have anything selected, the command will work on the entire line.
Hit
Home
Shift + End
You can do it with Shift + DownArrow.
Yes there is. If you are in the begining of the line press Shift+ End.
If you are in the end of the line press Shift+ Home.
Hope that helps
I believe, if you don't have any selection and press Ctrl + C, it would copy the line.
Shift + End = Select between cursor and the end of the line
It's Home+Home, then Shift+Down for me.
Or you change that setting which makes Ctrl+C with no selection copy the line. But I hate that, so I always turn it off. (Thanks to Bala for providing the link to that setting!)
To cut a line, Ctrl+L works in my keyboard settings.
There's also Alt-Up and Alt-Down to move whole lines. It's two fewer keystrokes than using Ctrl-X, and unlike Ctrl-X, it also moves multiple whole lines at a time if your selection covers multiple lines even partially. It's also nice because the feedback is instantaneous, unlike Ctrl-X where you can never remember whether the pasted line will go above or below your cursor.
I saw this and thought I'd never use the feature. But once I got used to it I use it all the time. There's no easier way to move a block of code than using Shift-Up/Down to select the lines, press Alt-Up/Down a few times to move them, and then use Tab to adjust the indentation.
Of course it only works within the same file though.
Visual Studio macros are another way to do these types of operations if you can't find an existing command. A simple way to create one is:
Use the Record TemporaryMacro option (under Tools/Macros).
Select the line however you prefer (e.g., home, shift, end).
Click Stop Recording (under Tools/Macros).
Choose Save TemporaryMacro (under Tools/Macros).
Then choose Tools/Customize/Keyboard and assign a shortcut to the macro.
It's not specifically a keyboard shortcut, but a triple-click will select a whole line of code.
This works in some other areas of Windows as well. In Chrome, for example, double-click selects a word, but triple-click selects a paragraph.
(This works in Visual Studio 2013 on Windows 7. Not sure about other versions/platforms.)
I use Ctrl + Insert to copy entire line, and Shift + Insert to paste entire line.
Other answers require either using a mouse or hitting more than one combination.
So I've created a macro for those who want a VSCode-like Ctrl+L behaviour. It can select multiple lines.
To use it, install Visual Commander extension for macros: https://marketplace.visualstudio.com/items?itemName=SergeyVlasov.VisualCommander
Then create a new command, select C# as a language and paste this code:
using EnvDTE;
using EnvDTE80;
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
var ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
if (!ts.ActivePoint.AtStartOfLine)
ts.StartOfLine();
ts.LineDown(true, 1);
}
}
Now you can assign a desired shortcut in preferences:
Tested in VS 2022.
Triple-click to select the whole line. Then do what you want.
You can press Home + Shift + End to select the whole line as well.
If you want to copy the whole line then just press Ctrl + C. It will copy the whole line if nothing is selected.

Xcode auto-completion - replace text keystroke?

Say we have a TestClass with the 2 methods, -getSomeString and -getAnotherString, and we are editing the following code and the cursor is in the location shown:
NSString *aString = [TestClass get<cursorIsHere>SomeString];
Say I want to change it to use -getAnotherString. If I bring up the auto-completion popup and select the other method via hitting enter or tab, I'm left with:
NSString *aString = [TestClass getAnotherStringSomeString];
i.e., it doesn't replace the existing text but rather just inserts.
Is there a special keystroke to make it replace the remaining text?
See IntelliJ for reference.
I don't think that there is a one step operation to achieve this. My suggestion would be similar to Thomas Templemann, but rather than two steps of forward word select and then Delete, I would expand to the desired autocomplete, by bouncing on Control + . and then hit Option + forward delete, which kills to the end of the word.
I don't think so. I have always used the following
double click on getSomeString
press Escape (or your autocomplete key)
find replacement method
that double click step has never really bothered me, but I would be interested if anyone knows better!
My work-around for this problem is this: Since the cursor will be right after the inserted text, I just press Shift-Option-Rightcursor, which selects the word past the cursor, then I hit the Delete key.
You can use the Tab key to perform "replace" instead of "insert" when choosing the method from the auto-complete popup in IntelliJ 9 (don't know if it's available in previous versions).

Resources