Sublime Forum

[Solved][Question] Move cursor to previous positions

#1

Briefly

I can not move cursor to previous positions.

Detail

I want create macro for wrapping code for GitHub. After wrapping cursor must be situated after ''', so I can write my programming language.

'''ruby
My code

Also code!
'''

How it’s worked:

But I can not move cursor to position when I paste code.

Did not help

jump_back and jump_forward commands. This commands moving cursor, if I use mouse for move to text.

Do not offer

Please, do not offer solutions, when result of macros will be not as this (| — cursor position after run macro):

'''|
My code

Also code!
'''

Thanks.

0 Likes

#2

You can create this keybinding:

    {
        "keys": ["alt+shift+w"],
        "command": "insert_snippet",
        "args": {
            "contents": "``` $1\n$SELECTION\n```"
        },
        "context":
        [
            { "key": "selector", "operator": "equal", "operand": "text.html.markdown" }
        ]
    },

Now you can just select the text you want to have as source code and press the keys to wrap it.
If you always want to wrap the text you paste one could make a “paste_wrapped” command.

0 Likes

#3

@r-stein, thanks for answer, but your solution not very good, because if fragment of code is large, selection code may be many times.

I search plugins for Wrap clipboard or Wrap insert function and I found Snippet from clipboard plugin, but its no work for me. In this plugin use CLIPBOARD environment variable, but now I can not find this variable in unofficial documentation. For example, this configuration not work for me:

{ "keys": ["ctrl+super+h"],
    "command": "insert_snippet",
    "args": {
                        "contents": "```$1\n$CLIPBOARD\n```"
              },
},

Thanks.

0 Likes

#4

I do not understand what it is.

  1. What you want to do?
  2. And how it is being currently done?
0 Likes

#5

@addons_zz, see my screencast. I need place the cursor before My code. On screencast I use a mouse. But how I can place the cursor before ‘My code’ without using mouse?

Thanks.

0 Likes

#6

Much better to understand now. To do it, You will need create a small package to do that. Basically find when is the best moment to catch the right moment/the moment you want to it to act, and perform the cursor positioning as you like.

0 Likes

#7

If you always want to paste it you can write a plugin Tools > Developer > new plugin and paste:

import sublime
import sublime_plugin


class PasteAndWrapCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        pos = view.sel()[0].b
        view.insert(edit, pos, "``` \n{0}\n```".format(sublime.get_clipboard()))
        view.sel().clear()
        view.sel().add(sublime.Region(pos + 4))

Change the keybinding to:

    {
        "keys": ["alt+v"],
        "command": "paste_and_wrap",
        "context":
        [
            { "key": "selector", "operator": "equal", "operand": "text.html.markdown" }
        ]
    },
2 Likes

#8

@r-stein, thank you very much!

0 Likes