4

Is there a way to paste a multi-cursor (Ctrl+d, Ctrl+d, ... Ctrl+C) select, stripped of its newlines?

If [...] represents the highlight, and the cursor:

The ⦙[red].
The ⦙[blue].
The ⦙[green].

And if I pasted I'd get:

red
blue
green⦙

but instead I want

redbluegreen⦙

Is this possible?

Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81

2 Answers2

3

Save the following script @:
/Packages/Paste Without NewLines/paste_without_newlines.py

 

import sublime, sublime_plugin

class paste_without_newlines( sublime_plugin.TextCommand ):
    def run( self, edit ):

        clipboard = sublime.get_clipboard()
        clipboard = clipboard.replace( "\n", "" )
        sublime.set_clipboard( clipboard )
        self.view.run_command( "paste" )

 


 

To execute via Command Palette > Paste Without NewLines, add the following code @:
/Packages/Paste Without NewLines/Default.sublime-commands

 

[
    {
        "caption": "Paste Without NewLines",
        "command": "paste_without_newlines",
    },
]

 


 

To execute via Ctrl + Shift + Alt + V, add the following code @:
/Packages/Paste Without NewLines/Default.sublime-keymap

 

[
    {
        "keys": ["ctrl+shift+alt+v"],
        "command": "paste_without_newlines",
    },
]
Enteleform
  • 3,753
  • 1
  • 15
  • 30
  • 1
    The 2nd replace in the plugin `.replace( "\r", "" )` is not needed. Sublime Text always uses just `\n` to represent new lines internally, regardless of the OS and the current file's line ending setting. The line ending setting only comes into effect when saving files. See: [Is a newline always represented internally by ‘\n’?](https://forum.sublimetext.com/t/is-a-newline-always-represented-internally-by-n/20506) – mattst Jun 22 '16 at 14:37
  • Also a `sublime_plugin.TextCommand` class name (as well as an `ApplicationCommand` but not a `EventListener`) should use *CamelCase* followed by the word `Command`. In this case `PasteWithoutNewlinesCommand` should be used instead of `paste_without_newlines` in the line `class paste_without_newlines...` . ST translates *CamelCaseCommand* class names into a ST *snake_name* command, i.e. `paste_without_newlines`, dropping the `Command` ending. To be fair the plugin works as is but the [plugins documentation](http://docs.sublimetext.info/en/latest/extensibility/plugins.html) is clear about this. – mattst Jun 22 '16 at 15:19
  • Just in case I wasn't clear the `PasteWithoutNewlinesCommand` plugin class name gets translated to the ST `paste_without_newlines` command so that is still what should still be used in the key binding and the command palette entry. – mattst Jun 22 '16 at 15:28
  • @mattst : Good call on `\r`, I didn't feel like looking it up & added it just in case. `a sublime_plugin class name should use CamelCase followed by the word Command` I disagree. You linked to the **unofficial** documentation, and the **official** documentation is known to be incomplete. I used to use `PluginNameCommand`, until I found some example that used `plugin_name`. IMO; it's a better option as it reflects the actual text that will be used in the command & it doesn't require the redundancy of the `Command` suffix. Also, there are no issues with using it since ST treats them the same. – Enteleform Jun 22 '16 at 15:30
  • As you wish but the same advice is given in several places in the unofficial docs [here](https://docs.sublimetext.info/en/latest/extensibility/plugins.html) and [here](https://docs.sublimetext.info/en/latest/reference/plugins.html) and the unofficial docs are linked from the [main ST support page](https://www.sublimetext.com/support). All the plugins in the `Default.sublime-package` archive use the `CamelCaseCommand` convention for classes that inherit `TextCommand`, `ApplicationCommand`, and `WindowCommand` but not `EventListener` derived classes, so maybe there's a reason why. – mattst Jun 22 '16 at 16:14
3

For anyone else stumbling across this, there's a sublime text package that solves this exact problem, it's called Paste PDF Text Block.

You can then Ctrl+Alt+v the text you want to copy from a pdf file into a new file in Sublime Text.

Worked a treat, and is a good solution if you don't know enough about making your own packages in sublime text, like me :(

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
Galaxy
  • 3,370
  • 3
  • 29
  • 41