Move Cursor on Middle Button Paste in Sublime Text 3

Move cursor on middle button paste in Sublime Text 3

Just found a fix, it's actually super easy.

Just create a file named Default (Linux).sublime-mousemap with the following content:

[
{
"button": "button3",
"press_command": "drag_select",
"command": "paste_selection_clipboard"
}
]

And save it in your Packages/User/ folder (in my case: /home/boris/.config/sublime-text-3/Packages/User/). That's it.

Explanation: the default mousemap does not have the line "press_command": "drag_select", which is the one telling Sublime to change the cursor position on click.

Sublime Text 3 - Middle click paste in Windows OS

In sublime goto:

Preferences->Browse Packages 

It should bring up your windows explorer. In that explorer click the User folder In that folder make a file called:

Default (Windows).sublime-mousemap 

Finally in that file put:

[ { "button": "button3", "modifiers": [], "command": "paste" } ] 

Save and restart sublime.

Middle button paste deleted text in Sublime 3

I've came with plugin like this:

import sublime, sublime_plugin
import re

class MyListener(sublime_plugin.EventListener):
def __init__(self):
sublime_plugin.EventListener.__init__(self)
self.deleted = ''
self.mark_for_clear = ''
self.clipboard_save = ''
self.empty_matcher = re.compile('^\s+$')

# Clear last deleted word if user made highlight of another one
# Delete if you want to e.g delete one word and highlight another to replace
def on_selection_modified(self, view):
selected_text = view.substr(view.sel()[0])
if self.mark_for_clear != '' and self.mark_for_clear != self.deleted:
self.deleted = ''
self.mark_for_clear = ''
if len(selected_text) > 0 and not self.empty_matcher.match(selected_text):
self.mark_for_clear = selected_text

def on_text_command(self, view, name, args):
# Save deleted word if command was deletion command
if name in ['right_delete', 'left_delete']:
self.deleted = view.substr(view.sel()[0])
#print("Deleted: %s " % self.deleted)
# Propagate saved deleted word to clipboard and change command to
# standard paste (we can only control standard paste clipboard)
if name == "paste_selection_clipboard" and len(self.deleted) > 0:
#print("Pasting:%s" % self.deleted)
self.clipboard_save = sublime.get_clipboard()
sublime.set_clipboard(self.deleted)
# Comment line below to enable multiple middle-click pasting of deleted words:
self.deleted = ''
return("paste", 'HackedByAlkuzad')

# If we are after paste_selection_clipboard command, return old clipboard
def on_post_text_command(self, view, name, args):
if name == 'paste' and len(self.clipboard_save) > 0 and args == 'HackedByAlkuzad':
sublime.set_clipboard(self.clipboard_save)
self.clipboard_save = ''

This plugin will detect delete comamnd (right = delete, left = backspace) and copy deleted content to memory. Then if user uses middle-click paste it replaces clipboard with deleted content and pastes it. After paste it restores saved clipboard.

I assumed that the copy from deletion should work on empty space (ST does not have insert mode without Vintage). To change that behaviour you can delete on_selection_modified function to stop checking for that explictly, however highlighting the new word will not copy it to middle-button clipboard.

Edit:
Version for system-wide clipboard using Linux xclip (taken from pyperclip)

import sublime, sublime_plugin
import re
from subprocess import Popen, PIPE, check_call, CalledProcessError

class DeletedToClipboard(sublime_plugin.EventListener):
empty_matcher = re.compile('^\s*$')

def __init__(self):
sublime_plugin.EventListener.__init__(self)
try:
check_call(['which','xclip'])
except CalledProcessError:
sublime.error_message("You have to have xclip installed to use DeletedToClipboard")

@classmethod
def _is_empty(cls, text):
return len(text) <= 0 or cls.empty_matcher.match(text)
# Thanks pyperclip :)
@classmethod
def _copy_to_system_clipboard(cls, text):
# try secondary if not working
p = Popen(['xclip', '-selection', 'primary'], stdin=PIPE)
try:
p.communicate(input=bytes(text, 'utf-8'))
except Exception as e:
print("Error on paste to clipboard, is xclip installed ? \n{}".format(e))

def on_text_command(self, view, name, args):
# Save deleted word if command was deletion command and selected text was not empty
if name in ['right_delete', 'left_delete']:
deleted = []
for region in view.sel():
text = view.substr(region)
if not DeletedToClipboard._is_empty(text):
deleted.append(text)
if deleted != []:
self._copy_to_system_clipboard("\n".join(deleted))

How to make Sublime Text 3 to move the cursor to the next line after ctrl + / comment?

The simplest solution is to use a Macro for this that combines together the commands for toggling a line comment and then moving the cursor, and then rebinding the key to run the macro.

Such a macro would look something like the following. Here this is saved as Packages\User\comment_line.sublime-macro.

[
{
"command": "toggle_comment",
"args": {"block": false }
},
{
"command": "move",
"args": {"by": "lines", "forward": true }
}
]

With this in place, you can add a binding such as the following to your custom key bindings:

{
"keys": ["ctrl+/"],
"command": "run_macro_file",
"args": {"file": "res://Packages/User/comment_line.sublime-macro"},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": "true", "match_all": true },
]
},

If you change the name of the macro when you save it, that needs to be reflected here.

This binding includes a context that makes it only apply when there is no selection, in which case this binding will be ignored and Sublime will use the default instead.

That can be removed if you wish. However, WebStorm (the only JetBrains tool I have handy at the moment) operates in this fashion, so presuming that IntelliJ does as well this more accurately mimics what happens there.

Additionally, if you're mapping a different key to the comment command, make sure the original line is above the new addition:

{ "keys": ["ctrl+q"], "command": "toggle_comment", "args": { "block": false } },
{
"keys": ["ctrl+q"],
"command": "run_macro_file",
"args": {"file": "res://Packages/User/comment_line.sublime-macro"},
"context": [
{ "key": "selection_empty", "operator": "equal", "operand": "true", "match_all": true },
]
},

How to make a multiline cursor without using a mouse in Sublime text 3?

Yes, there is. Using Shift or Shift, select the lines you want multiple cursors on. Next, hit CtrlShiftL (CommandShiftL on macOS) to split the selection into lines. Finally, hitting will put the cursors at the beginning of the lines, while will put them at the end.

Edit

There is also another, quicker way (thanks to minitech) - CtrlAlt/ will create multiple cursors without having to do selections first, and you can place the cursors anywhere in the line you wish. However, on Windows these key combos may be mapped to changing the screen orientation. To change this, hit CtrlAltF12 to open the Intel control panel, click Options, and either remap the screen orientation hotkeys, or click Off on the left side to disable all of them.

Removing a cursor in Sublime Text after Ctrl+click

You can use the alt modifier on windows and linux and shift+cmd modifier on OS X while selecting to use "subtractive" mode. You could also use a simple plugin to remove the first or last cursor in the view (not first or last added, first or last overall) and tie it to a key command.



Related Topics



Leave a reply



Submit