将数据复制到剪贴板时如何去除前导标签和/或前导空格?

13 clipboard notepad++ windows-10

我需要一种方法来从 Notepad++ 复制到剪贴板的数据中去除前导缩进(制表符和空格)。

Notepad++ 文档本身中的数据应该保持这些前导缩进,而复制到剪贴板的数据不应该。

此行为应由 CTRL+C 单独启动,并且仅当我在 Notepad++ 中时才启动。


当我用鼠标定期标记文本时,这就是 Notepad++ 中发生的情况:

在此处输入图片说明

这就是我定期标记文本并复制后剪贴板中应该有的内容:

在此处输入图片说明


笔记:

  1. 这些图像只是为了更好地说明我的需要 - 我的实际代码更长。
  2. 我也在这里问过这个。
  3. 我问这个是因为如果这些代码块包含catheredocuments(与处理 heredocument 分隔符有关,一个长话),我无法将缩进的代码块粘贴到 Linux 终端。

Ste*_*ven 6

考虑以下AutoHotKey (AHK) 脚本。有关AutoHotkey 脚本的更多解释,请参阅AutoHotkey 教程和文档。

安装 AutoHotKey 后,在 Notepad++ 中按Ctrl+ Shift+cx将复制(或剪切)到剪贴板并修剪线条。

注意:我使用了Ctrl+Shift以便您仍然可以使用原始副本并仅使用Ctrl. 如果您不喜欢这种行为,只需++^c::和 中删除+^v::

有关;解释,请参阅注释(以 开头)。与任何编码一样,最好留下注释,以便在您稍后返回时更好地理解脚本。

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

TrimClipboard()
{
    ; Split each line of the clipboard into an array. 
    ; Note: Ignoring Cr (`r) prevents duplicate rows
    linesArray := StrSplit(clipboard, "`n", "`r")

    newClip := "" ; Initialize output string

    for index, element in linesArray
    {   
        ; For each line: trim it, append it and CrLf to output string
        newClip .= trim(element) . "`r`n" 
    }
    ; Note: There is always an extra newline at this point, regardless 
    ; of if the clipboard ended in a newline.

    ; Assign value back to clipboard, without the trailing CrLf
    clipboard := SubStr(newClip, 1, -2)
}

#IfWinActive ahk_class Notepad++
; On Shift+Ctrl+C, perform copy, wait for new content, and trim clipboard
+^c::
    ; Note: ^{sc02e} is the scancode for c which works regardless of keyboard layout
    Send, ^{sc02e}
    Clipwait
    TrimClipboard()
return

;On Shift+Ctrl+X, perform copy, wait for new content, and trim clipboard
+^x::
    ; Note: ^{sc02d} is the scancode for x which works regardless of keyboard layout
    Send, ^{sc02d}
    Clipwait
    TrimClipboard()
return

; sc02e && sc02d are keyboard scan codes for the keys c and x respectively.
; The scancodes work regardless of the keyboard layout set in Windows

#IfWinActive
Run Code Online (Sandbox Code Playgroud)