Sublime Forum

[solved] Complex column paste operations

#1

Hi all. I’m having a question/problem - hopefully just a small one but I couldn’t find a solution. I want to insert a text block behind another text block like it is possible in UltraEdit: Just copy the text block, activate column mode and insert behind the target text block. No matter whether the lengths of lines in a text block are equal or the text blocks have the same amount of lines. I know this post and it actually works well: Is there any possible way to column paste?. But that doesn’t solve my issue.

To make it more clear here some examples:

:Textblock1 
interface Serial0/0/0
 description thu2
 ip address 172.29.237.13 255.255.255.252
 ip ospf network point-to-point
 ip ospf cost 20
 ip ospf hello-interval 1


:Textblock2
interface Serial0/0/0
 description thu1
 ip address 172.29.237.14 255.255.255.252
 ip ospf network point-to-point
 ip ospf cost 20
 ip ospf hello-interval 1

And I need this output:

:Textblock1                                :Textblock2
interface Serial0/0/0                       interface Serial0/0/0
 description thu2                            description thu1
 ip address 172.29.237.13 255.255.255.252    ip address 172.29.237.14 255.255.255.252
 ip ospf network point-to-point              ip ospf network point-to-point
 ip ospf cost 20                             ip ospf cost 20
 ip ospf hello-interval 1                    ip ospf hello-interval 1

Or something like this:

:Textblock1                               :Textblock2
interface Serial0/0/0                     interface Serial0/0/0
 description thu2                          description thu1
 ip address 172.29.237.13 255.255.255.252  ip address 172.29.237.14 255.255.255.252
 ip ospf network point-to-point            ip ospf network point-to-point
 ip ospf cost 20                           # maybe more example line
 ip ospf hello-interval 1                  ip ospf cost 20
                                           ip ospf hello-interval 1

These are just simplified examples. Reality is most of the time more complex.

Is that possible with sublime text?

0 Likes

#2

Please wrap your example blocks in fenced code blocks (```) or indent them by 4 spaces, so that we can see whether or not the forum collapsed multiple whitespaces.

Either way, this would certainly be possible with a plugin.

0 Likes

#3
0 Likes

#5

As already mentioned you can easily do this with a plugin. Select Tools > Developer > New Plugin… and paste:

import sublime
import sublime_plugin


class PasteJoinBlockCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        content = sublime.get_clipboard()
        clines = content.split("\n")
        if len(view.sel()) == 0:
            region = sublime.Region(0, view.size())
        else:
            sel = view.sel()[0]
            if sel.empty():
                region = sublime.Region(sel.b, view.size())
            else:
                region = sel
        lines = view.lines(region)
        lines = lines[:len(clines)]
        max_len = max(len(l) for l in lines) + 1
        if len(clines) > len(lines):
            rest, clines = clines[len(lines):], clines[:len(lines)]
            rest = ((" " * (max_len)) + l for l in rest)
            clines[-1] = clines[-1] + "\n" + "\n".join(rest)
        assert len(clines) == len(lines)
        # insert the lines from the last line to the first line
        for line, cline in reversed(list(zip(lines, clines))):
            indent = (" " * (max_len - len(line)))
            view.insert(edit, line.b, indent + cline)

Then create a keybindings

    {
        "keys": ["alt+v"],
        "command": "paste_join_block",
    },
0 Likes

#6

I don’t understand your post. However I solved my issue by myself. Just Ctrl+Shift+P -> install > Paste as column. Reload sublime_text and, voila, right click provides a new menu entry labled “Past in column”. It works very well.

0 Likes

#7

Ok. I’m wrong. I was a little bit stunned by your code block. But you’re right. It’s quite easy. But I have one more question reguarding the key binding. I pasted the binding into $HOME/GitSrc/GitHub/sublime/sublime_alignment/Default\ (Linux).sublime-keymap and it works. But I think it isn’t a good place. Would it be more senible to put it somewhere else? Where?

0 Likes

#8

The best place for the keybinding would be the keybindings inside the User diretory, you can access it via Preferences > Key Bindings. The plugin file should also be inside the User directory.

0 Likes

#9

Great! I did it. It works very well. Thank you very much.

0 Likes

#10

Er … I would like to get rid of this plugin, because it is restricted compared to the package “Paste as Column”. But I cannot find it. Where is it?

0 Likes

#11

If you mean the code that was posted above, you saved it as a .py file in your user package. In that case you can select Preferences > Browse Packages... from the menu, go into the User package and look for files of that type that might be this one.

You can delete the file or just modify the extension so that it’s not a .py file, and Sublime will ignore it. May also be a good idea to restart Sublime afterwards just to make sure it’s fully gone.

0 Likes

#12

It helped. I found it. Thank you.

0 Likes