How to strip leading tabs and/or leading spaces when copying data to clipboard?
Consider the following AutoHotKey (AHK) script. See the AutoHotkey Tutorial and documentation for more explanations on AutoHotkey scripts.
After installing AutoHotKey, press Ctrl+Shift+c or x within Notepad++ to copy (or cut) to the clipboard with the lines trimmed.
Note: I used Ctrl+Shift so that you could still use the original copy and cut normally with only Ctrl. If you don't like this behavior, just remove +
in both +^c::
and +^v::
.
See the comments (starting with ;
) for an explanation. As with any coding, better leave in the comments to better understand the script when you come back to it later.
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
Just use
Alt + Mouse dragging
or Alt + Shift + Arrow keys
to select the column, then Ctrl+C
to copy the column.
See this example : https://notepad-plus-plus.org/fr/features/column-mode-editing.html
Edit:
We can also select the whole column, or even more then one column, also we can select one or many rows.
Here is a simple way to do it:
Left mouse click at the beginning of the wanted selection.
Go to the location where the wanted selection must stop
Move the cursor to that second and final location, with :
- A hit on the
ALT
+SHIFT
keys and aleft click
, simultaneously, to select the desired columns and rows.
- A hit on the
Hope this help