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.
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.
A naive attempt:
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()
)
)
@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!
no. I can’t find a way to influence ST’s copy history (ctrl+k, ctrl+v). not even sublime.set_clipboard
API.
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!
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)
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!