Sublime Forum

Selection Mode

#1

Hi,

I would like to know if there is a way to get SublimeText into a “selection mode” where I can move my cursors (without pressing the shift key) and text is selected from the start of the selection mode to the current position of the cursor.

This might sound strange, but I have to work a lot on a laptop and use a special keymap where I map the cursor keys onto alt+j i k l. Selecting would obviously work by holding down the shift key, but I would like to use the shift key to modify the behaviour like alt+shift+i jumps up a paragraph.

Thanks,
Markus

0 Likes

#2

Try Vintage/VintageEX and use Vim selection mode:

[quote]
V - selects entire lines
v - selects range of text
ctrl-v - selects columns
gv - reselect block[/quote]

cs.swarthmore.edu/help/vim/selection.html

0 Likes

#3

I’m don’t think this is what I am looking for. So at the moment I have in my keymap something like his:

{ “keys”: “alt+l”], “command”: “move”, “args”: {“by”: “characters”, “forward”: true, “extend”: false} },

I would search for a function which I could also put onto some key, that would change the “context” of alt+l in such a way, that extend gets set true (this is e.g how selecting in emacs works)

0 Likes

#4

Probably i understand wrongly, but not this is what are you looking for?

(http://img.iamntz.com/jing/2012-10-19_1017.swf)

0 Likes

#5

Oh, thanks, you are right. This is exactly what I am looking for :smile: Thank you for your help

0 Likes

#6

However, I would not like to use full vintage mode. I was trying to extract the relevant functions from vintage, however this is rather new to me. Could someone give me which functions I would have to move to a new package so that I only have this functionality?

0 Likes

#7

I think what I would need is the possibility to toggle some mode/state/context with a keybinding, than I could use that context for my keybindings. Does someone have an idea how to do this?

0 Likes

#8

You could edit Vintage/Default.sublime-keymap file and start digging :smile: Dunno if is possible without vintage mode though.

0 Likes

#9

Yes, I already tried to find the relevant parts in Vintage mode but I did not succeed so far.

0 Likes

#10

I suspect there’s a more elegant way to do this, but the following will work:

example sublime-keymap entry:

{ "keys": "alt+l"], "command": "move", "args": {"by": "characters", "forward": true, "extend": true}, "context": 
	{ "key": "setting.selection_mode", "operator": "equal", "operand": true }
] },

example sublime-commands entry (you could also bind this to a key, if you prefer):

{
	"caption": "Selection Mode: Toggle Selection Mode",
	"command": "toggle_setting",
	"args": {"setting": "selection_mode"}
},

Does this make sense?

Alex

0 Likes

#11

@quodlibet: Great, thanks. This is exactly what i meant

0 Likes

#12

A further question: When I am done with my selection I have to exit the selection mode again. Is there a way so that the selection_mode would finish if any other key than e.g. a movement key is pressed?

0 Likes

#13

bump

0 Likes

#14

I’m almost certain this would require a plugin – which means python – and, not being a programmer, I can’t help you further. Hopefully someone else will pitch in.

0 Likes

#15

Take a look at the Edit/Mark menu, too, which already implements a selection mode of sorts, a la emacs

0 Likes

#16

You probably solved it in some way, but anyway, here is hint for others.

You need custom plugin for that (Tools -> New Plugin and then save it and restart ST):

import sublime, sublime_plugin
 
# Takes an array of commands (same as those you'd provide to a key binding) with
# an optional context (defaults to view commands) & runs each command in order.
# Valid contexts are 'text', 'window', and 'app' for running a TextCommand,
# WindowCommands, or ApplicationCommand respectively.
class RunMultipleCommand(sublime_plugin.TextCommand):
  def exec_command(self, command):
    if not 'command' in command:
      raise Exception('No command name provided.')
 
    args = None
    if 'args' in command:
      args = command'args']
 
    # default context is the view since it's easiest to get the other contexts
    # from the view
    context = self.view
    if 'context' in command:
      context_name = command'context']
      if context_name == 'window':
        context = context.window()
      elif context_name == 'app':
        context = sublime
      elif context_name == 'text':
        pass
      else:
        raise Exception('Invalid command context "'+context_name+'".')
 
    # skip args if not needed
    if args is None:
      context.run_command(command'command'])
    else:
      context.run_command(command'command'], args)
 
  def run(self, edit, commands = None):
    if commands is None:
      return # not an error
    for command in commands:
      self.exec_command(command)

Although not perfect, here’s how I do it:

	{ "keys": "alt+space"], "command": "toggle_setting", "args": {"setting": "selection_mode"}},

	{ "keys": "alt+l"], "command": "move", "args": {"by": "characters", "forward": true}}, 
	{ "keys": "alt+l"], "command": "move", "args": {"by": "characters", "forward": true, "extend" : true}, 
						 "context": {"key": "setting.selection_mode", "operator": "equal", "operand": true}]}, 
	{
	    "keys": "alt+c"],
	    "command": "run_multiple",
	    "args": {
	      "commands": 
	        { "command": "copy" },
	        { "command": "set_setting", "args": {"setting": "selection_mode", "value": false }},
	      	]
	    }
	},

If you have better way, don’t hesitate to share it.

Regards,
dingo

0 Likes