Simultaneous paste and copy of selection text - ide

Background
I want to be able to select some text, hit a keystroke that pastes what is on the clipboard over that selection, but at the same time copying that selection to the clipboard. I often find myself doing this operation when switching variables, etc from place to place.
Example
First sentence here, I need to switch it with the second sentence below. (ctrl-c)
...
Second sentence here, I am going to put this where the first one is.
///////
First sentence here, I need to switch it with the second sentence below.
...
First sentence here, I need to switch it with the second sentence below. (ctrl-"vc" after selecting second sentence, first sentence pasted, second sentence copied now)
///////
Second sentence here, I am going to put this where the first one is. (ctrl-v)
First sentence here, I need to switch it with the second sentence below.
My question
Does anyone know if any IDE/software supports such a paste/copy functionality? Has anyone ever run into this?
More specifically, does any one know how to set up a keyboard shortcut to do this in sublime text 2?

You can do it with a plugin. I threw this together rather quickly. I don't do anything special for multiple cursors (though it should take the content of multiple cursors as well as paste to all the proper locations).
import sublime
import sublime_plugin
class PasteAndCopyCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
current_content = []
cursors = view.sel()
for cursor in cursors:
current_content.append(view.substr(cursor))
for cursor in cursors:
view.replace(edit, cursor, sublime.get_clipboard())
sublime.set_clipboard("\n".join(current_content))
After you save the plugin, use paste_and_copy as the the command for your key binding or command palette entry.

Related

Moving the code one coloumn to the left

I wrote a code and now my excel template has changed (the first column is no longer in need), is there a way to move the entry code 1 column to the left instant of correcting it step by step?
For example(This is the old code):
.Range("I1:J1") = Array("CHECK", "KEY")
Now I need to change it to:
.Range("H1:I1") = Array("CHECK", "KEY")
But it's a very long code and I want to know if there's any way do to it easier.
Thanks.
I'd suggest simply using the built in Search & Replace function ( Ctrl + f ). Put something like
.Range("I1:J1")
into the "Find What" field and
.Range("H1:I1")
into the "Replace With" field.
If you're really lazy you could hit the Replace All button, but this can be dangerous as in possibly changing parts you didn't want to change. However, using the Replace button and going through all entries can be fairly fast even in a longer code, and this way you can check with each entry if it's really correct to change it.

How to select multiple lines of code and space them all?

This may be a fairly simple answer, or relatively easy; though I haven't been able to find the correct command/way to do so from anywhere.
As the title states, I'm trying to figure out the way as to space multiple lines of code.
Here's an example of what I'm looking for:
testing = "Testing"
work = "Work"
answer = "Answer"
Now I'd like to add spaces to the first two lines (selected together) at the same time, as in:
testing = "Testing"
work = "Work"
answer = "Answer"
Hold CTRL and click at the beginning of each line.
This will create two cursor points and every command will be iterated at every point.
Another tip:
You can highlight a selection of text, and CTRL + D will highlight all matches for that text and activate a cursor next to it. Very useful for mass editing in things like tables or forms.

How to select all instances of a variable and edit variable name in Sublime

If I select a variable (not just any string) in my code, all other instances of that variable get a stroke (white outline) around them:
Is there a keyboard shortcut that will let me select all of those instances of the variable and edit them all at once?
Things I've Tried:
⌘D, ⌘K, and ⌘U lets me select them one-by-one, but I have to manually exclude the non-variable string matches:
And using Ctrl⌘G simply selects all the string matches:
Clearly, Sublime is able to differentiate between variable and string matches. Is there no way to select just the variable matches?
Put the cursor in the variable.
Note: the key is to start with an empty selection. Don't highlight; just put your cursor there.
Press ⌘D as needed. Not on a Mac? Use CtrlD.
Didn't work? Try again, making sure to start with nothing selected.
More commands:
Find All: Ctrl⌘G selects all occurences at once. Not on a Mac? AltF3
Undo Selection: ⌘U steps backwards. Not on a Mac? CtrlU
Quick Skip Next: ⌘K⌘D skips the next occurence. Not on a Mac? CtrlKCtrlD
Sublime Docs
I know the question is about Macs, but I got here searching the answer for Ubuntu, so I guess my answer could be useful to someone.
Easy way to do it: AltF3.
Despite much effort, I have not found a built-in or plugin-assisted way to do what you're trying to do. I completely agree that it should be possible, as the program can distinguish foo from buffoon when you first highlight it, but no one seems to know a way of doing it.
However, here are some useful key combos for selecting words in Sublime Text 2:
Ctrl⌘G - selects all occurrences of the current word (AltF3 on Windows/Linux)
⌘D - selects the next instance of the current word (CtrlD)
⌘K,⌘D - skips the current instance and goes on to select the next one (CtrlK,CtrlD)
⌘U - "soft undo", moves back to the previous selection (CtrlU)
⌘E, ⌘H - uses the current selection as the "Find" field in Find and Replace (CtrlE,CtrlH)
This worked for me. Put your cursor at the beginning of the word you want to replace, then
CtrlK, CtrlD, CtrlD ...
That should select as many instances of the word as you like, then you can just type the replacement.
The Magic is, you have to start with an empty selection, so put your cursor in front of the word/character you want to multi-select and press Ctrl+D .
To me, this is the biggest mistake in Sublime. Alt+F3 is hard to reach/remember, and Ctrl+Shift+G makes no sense considering Ctrl+D is "add next instance to selection".
Add this to your User Key Bindings (Preferences > Key Bindings):
{ "keys": ["ctrl+shift+d"], "command": "find_all_under" },
Now you can highlight something, press Ctrl+Shift+D, and it will add every other instance in the file to the selection.
As user1767754 said, the key here is to not make any selection initially.
Just place the cursor inside the variable name, don't double click to select it. For single character variables, place the cursor at the front or end of the variable to not make any selection initially.
Now keep hitting Cmd+D for next variable selection or Ctrl+Cmd+G for selecting all variables at once. It will magically select only the variables.
It's mentioned by #watsonic that in Sublime Text 3 on macOS, starting with an empty selection, simply ⌃⌘G (AltF3 on Windows) does the trick, instead of ⌘D + ⌃⌘G in Sublime Text 2.
At this moment, 2020-10-17, if you select a text element and hit CTRL+SHIFT+ALT+M it will highlight every instance within the code chunk.
Just in case anyone else stumbled on this question while looking for a way to replace a string across multiple files, it is Command+Shift+F

How to copy and paste multiple lines/selections to the same number of multiple lines/selections without mapping them 1:1 in Sublime Text 2?

Every so often, I copy multiple lines of code and then paste them into multiple selections expecting each selection to have that block of code, but instead the block of code gets split into the multiple selections because the number of lines/selections matched. That's the default behavior if the number of lines/selections match, which is useful but not in cases like this one.
My first instinct was to see if there was a shortcut to cancel that behavior as I'm pasting just like how "paste_and_indent"'s shortcut is "super+shift+v". I didn't find one though.
Every time I have this issue, I deselect one of the selections and paste, then select only the one I had deselected and paste.
If anyone knows of a better way of doing this, please share.
I think that the only one shot way is to create a plugin (Tools/New Plugin...) that copies the clipboard into all selected regions:
import sublime, sublime_plugin
class FullMultilinePasteCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
self.view.insert(edit, region.begin(), sublime.get_clipboard())
Save it in your Packages/User directory.
Then you can add the key binding (Preferences/Key Bindings - User):
{ "keys": ["alt+super+v"], "command": "full_multiline_paste" }

Incremental paste plugin?

I know that often using a for loop to generate repetitive content is the better way than pasting something 20 times and changing each paste to the correct number by hand. But let's say for cases where content is hard-coded and I just want a list from 1-20.
I would like a text editor with a "smart" paste command that takes any number imbedded in a string on the clipboard and increments it each time I paste. If it doesn't exist, I will make a plugin.
I'm trying to think of a good 2-stroke keyboard shortcut to do it, close to the ctrl-V. Maybe ctrl-g or numpad_+ (in an app with no zooming).
Does it exist?
In Zeus this can be easily done as follows:
Column mark the area to be converted to a numbers
Use the Macros, Execute Script menu
Type in numbers to run the numbers Lua macro
Type in the first number of the sequence
The marked area will be replaced by an incrementing sequence of number starting at the first number provided in step 4.
I'm trying to think of a good 2-stroke
keyboard shortcut to do it,
Making this into a key stoke action is as simple as binding the numbers Lua macro to the keyboard.
Here is an example of how it works. If 1 values in the text below are column marked and the macro executed with a starting number of 1000:
Field_1
Field_1
Field_1
Field_1
the following text changes will be made:
Field_1000
Field_1001
Field_1002
Field_1003
Armed bear J has a renumber region command, as well as a case-preserving replace-in-files, which means I often keep it around for those features.
TextPad is another text editor with a Fill Region function, for filling with a character, a string, or incrementing numbers (starting from X, with left- or right-alignment, and space- or zero-filled.)
I used Notepad++ now, but I have to keep TextPad around just for that number-filling function.