Automatically select pasted text in Sublime Text 3 - macros

Is there any way, plugin, macro or something to make Sublime Text 3 automatically select the text that was just pasted?
I need to copy and paste some JSON data, but the pasted text is never in line with the surrounding text. Paste and indent -feature does not work properly for this.
What does work is the reindent feature, but it requires me to select a block of text and pressing a hotkey. So after pasting I would benefit for having the just pasted block of text being automatically selected, so I can just press the reindent hotkey to properly indent what I pasted.
Furthermore, it would be even better if I could bind the whole process to a hotkey, so:
Select text
Copy
Press some self defined hotkey to run a macro(?)
This macro the pastes the text, selects the pasted text and runs the reindent hotkey (*)
*So basically I would like to make a keybinding, say, ctrl+shift+b to do the following:
ctrl+v
Somehow select pasted text
ctrl+shift+f

You can create a plugin to do this, and execute it with a keybinding:
from the Tools menu -> Developer -> New Plugin...
select all and replace with the following
import sublime
import sublime_plugin
class PasteAndReindentCommand(sublime_plugin.TextCommand):
def run(self, edit):
before_selections = [sel for sel in self.view.sel()]
self.view.run_command('paste')
after_selections = [sel for sel in self.view.sel()]
new_selections = list()
delta = 0
for before, after in zip(before_selections, after_selections):
new = sublime.Region(before.begin() + delta, after.end())
delta = after.end() - before.end()
new_selections.append(new)
self.view.sel().clear()
self.view.sel().add_all(new_selections)
self.view.run_command('reindent')
save it, in the folder ST suggests (Packages/User/) as something like paste_and_reindent.py
add the following to your User keybindings { "keys": ["ctrl+shift+b"], "command": "paste_and_reindent" },
Note that Ctrl+Shift+B will replace the default binding for "Build With".
How it works:
when the keybinding is pressed, it runs the new command created in the plugin
this stores the current text selection positions
then it performs the paste operation
then it gets the new text caret positions
then it compares the old positions to the new ones, and selects the text that was pasted
then it runs the reindent command
You could get it to clear the selections again afterwards (by repositioning the text carets to the end of the selections - i.e. the default behavior after pasting) by doing another comparison of the selections before and after the reindentation.

On MacOS you can add:
"find_selected_text": true
to Sublime Text->Preferences->Settings (User Settings View)

Related

Is there a keyboard shortcut to select word occurrences in just one line in visual studio code

I know the topic of multi cursors-editing in visual studio code is duplicate but what I want is the way (by keyboard) to select word occurrences in just one line in visual studio code, because the other option: ctrl + F2 it selects all the occurrences in all the file, and not the mouse way by holding alt and click.
let say I have this:
const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
when the cursor is on REQUEST word for the second line I want to do two cursors in the second line after the two occurrences of REQUEST.
You have 2 alternatives:
Enable the Find in Selection option in the Find Widget after entering your find query.
An extension that I wrote does this fairly well, see Find and Transform.
With this simple keybinding:
{
"key": "alt+y", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"restrictFind": "line" // find all on current line only
// with multiple cursors you can have as many current lines as you wish
}
}
It does a find in the current file. Since there is no actual find query, like REQUEST, designated in the args. It will find the current word at the cursor on the line. Different languages define what a "word" is differently. For javascript, for example, FETCH_USERS_REQUEST is the current word even if the cursor is on Request only.
You can manage this by actually selecting, double-clicking on Request or Ctrl+D, and then trigger the above keybinding. Then the extesnion will search for whatever is selected, if there is a non-empty selection.
The extension is designed to select those find matches, not put the cursor after them, but you can simply right-arrow to dismiss the selection and the cursor will be where you want.
[I have to update the extension, the current version v0.9.7 in the Marketplace won't do this - but here is a demo of it already working. It should be updated tomorrow at the latest, look for v0.9.8.]

VSCode: How to split editor using same shortkeys as Sublime Text

In Sublime Text I can use the following shortkeys:
ALT+SHIFT+2 : create two columns
ALT+SHIFT+3 : create three columns
...
I want to be able to do the exact same thing in VSCode (without downloading the Sublime Text keybinding since I want the rest to stay the same)
If you search for columns within the "Keyboard Shortcuts" editor, you wil see these options:
workbench.action.editorLayoutTwoColumns
workbench.action.editorLayoutThreeColumns
They are unbound to any keyboard shortcut by default. Click the plus sign to the left of each of these commands in turn and you will get a dialog box where you can enter your chosen keybinding for each and you are done.

Pasting data in multiple cursor mode

in vs code I have the following file
1
2
3
a
b
c
Now I do the following steps
cut the lines a b c.
select the lines 1, 2, 3 and then go into multiple cursor mode (shift, alt I).
go to the end of the each number press and type a , and then do a paste.
The result is
1, a
b
c
2, a
b
c
3, a
b
c
but the result I wanted is
1, a
2, b
3, c
Check out Mark's good answer as well. There have been some updates to VS Code he mentions that should be considered as well.
Using Shift+Alt+I, you need to cut the text in multiple cursor mode as well:
1Enter2Enter3EnterEnteraEnterbEnterc
Shift+↑↑
Shift+Alt+I
Shift+←
Ctrl+X
↑↑↑↑→
, 
Ctrl+V
As of the 1.23.1 April update, a more convenient, middle mouse button selection can be used.
With the cursor and keyboard,
Instead of cutting the lines like normal, select from the end of the cursor to the beginning while in multi-cursor mode while holding Ctrl+Shift. After cutting the text with Ctrl+X, select with multiple cursors again by holding Ctrl+Shift. Then, type ,  and paste with Ctrl+V like you described.
You can also use Ctrl+Alt+Shift and the direction arrows to select with multiple cursors,
Sometime ago this functionality was apparently added. You can simply cut to your clipboard and then paste to multiple cursors - and, if there are the same number of lines on the clipboard as multiple cursors - each cursor will get one line from the clipboard.
You no longer need to be in multi-cursor mode for the cut or however you got the text onto the clipboard.
Just cut it.
Demo:
Unfortunately, the gif cut off the full command after selecting the 1,2,3,. What you then want to do is put a cursor at the end of each line of the selection. Command: Add Cursors to Line Ends Shift+Alt+I.
VSCode 1.39 added this setting:
Editor: Multi Cursor Paste
Controls pasting when the line count of the pasted text matches the
cursor count.
"editor.multiCursorPaste": "spread"
that will do what you want.
There is a second option full where each cursor will get the entire clipboard text. See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_39.md#new-option-for-multi-cursor-pasting. Demo of the full option:
From the multi-cursor paste release notes:
New option for multi cursor pasting
In the past, when pasting multi-line text from the clipboard, VS Code
would check if the clipboard text line count matches the cursor count,
and if it does, it would "distribute"/"spread" each line to a cursor.
This behavior is now tunable via the editor.multiCursorPaste setting,
which can have the values:
spread - Each cursor pastes a line of text (default).
full - Each cursor pastes the full clipboard text.

Sublime text 2 - find file by class name in Zend Framework

When you press Ctrl+p Sublime will open popup when you can easily find the file. Sublime auto detect the file location in both situation when you press / or space between file path parts.
In Zend Framework all classes has name within follow template: Namespace_Module_Other_Part_Of_Class_Location, how can I make Sublime understand the _ as a path separator when I press Ctrl+p and copy past the class name there?
So the above class should be recognized on location: Project/Namespace/Module/Other/Part/Of/Class/Location.php
I'm still looking for the solution of it. Even if the file search is hard-coded in Sublime 3, and you have a workaround to make it works, maybe to write some plugin? you are welcome.
Thank you.
You can do this with a simple plugin and key binding. Select Tools -> New Plugin... and replace the contents with the following:
import sublime
import sublime_plugin
class UnderscoreToSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command('copy')
clipboard = sublime.get_clipboard()
clipboard = clipboard.replace('_', ' ')
sublime.set_clipboard(clipboard)
Save the file as Packages/User/underscore_to_space.py where Packages is the folder opened when clicking on Preferences -> Browse Packages....
Next, create a custom key binding for the command. Select Preferences -> Key Bindings-User and add the following:
{ "keys": ["ctrl+shift+c"], "command": "underscore_to_space" }
If the file is empty when you open it, surround the above line with square brackets [ ]. Save the file (it will automatically save to the correct location), and you're all set.
Now, all you need to do is select the text you want to convert, and hit CtrlShiftC. This will copy the text to the clipboard, replace the underscores with spaces, and put the modified text back in the clipboard. You can now hit CtrlP to open Goto Anything... and paste in the modified text with CtrlV.
If you prefer to have the underscores replaces with forward slashes /, just change the clipboard.replace() arguments from ('_', ' ') to ('_', '/').
To get to the class definition you are looking for there exist several plugins doing "code intelligence". The plugins are language specific.
The most popular is SublimeCodeIntel which provides Jump to symbol definition functionality. SublimeCodeIntel claims to do this for PHP too. However, who to setup this for your project should be another question.
Some more options for possible source code static analysis in Sublime Text 2 in this blog post:

How to do multiple line editing?

I want to edit multiple lines in eclipse, but I can't find any short cut or Plugin. In Geany I just press ctrl+alt+up/down I can add / edit multiple lines.
Maybe this example can explain what I mean:
var text = "myname";
var addr = "myaddr";
var age = "myage";
I want to edit text above into:
var my_text = "myname";
var my_addr = "myaddr";
var my_age = "myage";
The text above is just a simple example, but sometimes I have many lines of words that I have to edit its prefix.
Press alt + shift + A to Toggle block selection (Toggle block / column selection in the current text editor), this will let you write vertically in eclipse, then you can easily do this.
Go to Window->Preferences.
Find for binding in text box surrounded by red box.
On OS X, the key combination for multi-line edits in Eclipse (or STS) is option/alt+command+A
You can try the following plugin,
https://github.com/caspark/eclipse-multicursor/releases
With this multiple occurrence of same text can be selected and edited. This is similar to multi select functionality available in editors like Sublime and Visual studio code.
The Eclipse 4.24 (June 15 2022) will integrate it (See https://bugs.eclipse.org/bugs/show_bug.cgi?id=576377):
Multi selection down relative to anchor selection (e.g. Ctrl-Alt-J)
Multi selection up relative to anchor selection (e.g. Alt-J)
End multi-selection (e.g. ESC)
Add all matches to multi-selection (e.g. Ctrl-Shift-Alt-J)
Multi caret up (e.g. Ctrl-Alt-Shift-Up)
Multi caret down (e.g. Ctrl-Alt-Shift-Down)
Thanks to eclipse contributor !
The Eclipse 4.22 (Q4 2021) equivalent of Geany would Alt+Click on the lines you want to edit in one go.
Eclipse now supports
Multiple text selection
Support for multiple selection has been added to Text Editors.
Multi selections allow most edit operations (text replacement or insertion, extend selection to next word or to next line, copy/paste...) to apply simultaneously on all ranges.
Multiple strategies are available to enable multi-selections:
Turn a block selection into a multi-selection using the To multi-selection command,
Add a caret with Alt+Click,
Use the new Select All button on the Find/Replace dialog.
So check if this would work in your case.
I know this is an old post, but I still want to share my way of multi select and editing. However this way is restricted to only the same variables across the file. Simply highlight the variable to edit, right click, choose Refactor->Rename. Then edit the variable and it will also edit the same variables across the file. Hope it helps..:)
Press key - { Alt + Shift + A } You will see A [+] symbol in IDE then use this symbol as drag