Sublime Forum

How to copy line in Sublime without new line

#1

In Sublime when you have nothing selected and click ctrl+c you will copy whole line with the new line. How to remove that new line, so ctrl+c will copy only current line.

I’m using Sublime on Windows.

0 Likes

#4

A naive attempt:

copy_without_newline_command.py

import sublime
import sublime_plugin


class CopyWithoutNewlineCommand(sublime_plugin.TextCommand):
    def run(self, _: sublime.Edit) -> None:
        if self.view.has_non_empty_selection_region():
            self.view.run_command('copy')
            return

        sublime.set_clipboard(
            "\n".join(
                self.view.substr(self.view.line(region))
                for region in self.view.sel()
            )
        )
1 Like

#5

But how to assign it to a keyboard shortcut?

0 Likes

#6
    { "keys": ["ctrl+shift+c"], "command": "copy_without_newline" },
1 Like

#7

@jfcherng, thank you very much for this plugin! Can you please advise me how I can get the copied text to history, so that I can also use “Paste from History”? Thanks!

0 Likes

#8

no. I can’t find a way to influence ST’s copy history (ctrl+k, ctrl+v). not even sublime.set_clipboard API.

0 Likes

#9

What if I change paste_from_history.py?

g_clipboard_history = ClipboardHistory()


class ClipboardHistoryUpdater(sublime_plugin.EventListener):
    """
    Listens on the sublime text events and push the clipboard content into the
    ClipboardHistory object
    """

    def on_post_text_command(self, view, name, args):
        if view.settings().get('is_widget'):
            return

        if name == 'copy' or name == 'cut':
            g_clipboard_history.push_text(sublime.get_clipboard())

I do not know Python, so I am struggling here. Could I just change the line before the last to include the copy_without_newline command? There is an extra issue, though, because I trigger the copy_without_newline command inside a macro, as I have other commands being triggered at the same time (please, see key binding below). Would it be possible to change the paste_from_history.py by adding the command run_macro_file and the argument "file": "Packages/User/Macros/Macro - Copy without New Line.sublime-macro"?

		{ "keys": ["y"], "command": "run_macro_file", "args": { "file": "Packages/User/Macros/Macro - Copy without New Line.sublime-macro"}, "context": [{"key": "setting.command_mode"}, {"key": "vi_action", "operand": "vi_copy"} ] },

Any insight would be appreciated!

0 Likes

#10
import sublime
import sublime_plugin

from Default.paste_from_history import g_clipboard_history


class CopyWithoutNewlineCommand(sublime_plugin.TextCommand):
    def run(self, _: sublime.Edit) -> None:
        if self.view.has_non_empty_selection_region():
            self.view.run_command("copy")
            return

        text = "\n".join(self.view.substr(self.view.line(region)) for region in self.view.sel())

        sublime.set_clipboard(text)
        g_clipboard_history.push_text(text)
1 Like

#11

It works! @jfcherng, thank you so much!

If I put the copy_without_newline command inside a macro, it will not affect the copy history. Surprisingly, even the copy command will not change the copy history when inside a macro. I worked around this by using the Chain of Command command.

Thank you for all you do for the community!

1 Like