Paste text without jumping to cursor
-
I just want to paste a 1000char line (probably HTML or JSON) and move on to the next line without my view getting jolted. (Where I continue the process). Does Notepad++ support that feature?
A related question would be how to put the cursor off screen without scrolling to the cursor.
-
@Thomas-Zito said in Paste text without jumping to cursor:
I just want to paste a 1000char line (probably HTML or JSON) and move on to the next line without my view getting jolted. (Where I continue the process). Does Notepad++ support that feature?
What do you mean by “view getting jolted”?
If you paste 1000 characters that don’t end in a newline sequence, and your screen width only handles 120 characters, then Notepad++ will either scroll to the end of the line (if you don’t have word-wrap enabled), or will just visually wrap it to fit on screen (if you have word-wrap enabled from the View menu or the toolbar toggle).
If you don’t want word-wrap on, but don’t want Notepad++ to scroll to the end of your paste, then you might consider doing the copy before the paste to include the newline sequence, so that way when you paste, Notepad++ puts the caret on the next line already.A related question would be how to put the cursor off screen without scrolling to the cursor.
The typing caret for Notepad++ is always onscreen, unless you move it then intentionally scroll away from the caret. With a scripting plugin, you could automate moving the caret then scrolling in such a way that it probably goes off-screen (it would be a similar concept to my script shown in the “Is there a way to keep a blank “working space” as I’m logging information?” conversation
-
What do you mean by “view getting jolted”?
I think what is meant here is that the OP wants to be able to paste and have the new data not shift any existing data on screen. As a normal paste leaves the caret at the end of the pasted data, if the data is of great length, this is not possible.
Some scripting might help, but I don’t believe it is possible to guarantee with certainty that a shift/jolt will not happen.
Experiment with the follow PythonScript, as a substitute operation for “stock” paste:
p = editor.getCurrentPos() editor.paste() editor.setSel(p, p)
-
@Alan-Kilborn Yes, that is what I mean.
Unfortunately that scripting would not guarantee. I guess it’s not something anyone besides me would consider a feature in the first place.
Thanks though. Maybe I’ll look at the source code one day… like a year in the future lol
-
@Thomas-Zito said in Paste text without jumping to cursor:
Maybe I’ll look at the source code one day
If you do that, you’ll probably have to look at non-Notepad++ source code. Notepad++ uses an entirely different project for its editing functions, and where the caret moves after pasting is likely something that belongs to the editing component called “Scintilla”.
-
@Alan-Kilborn said in Paste text without jumping to cursor:
@Thomas-Zito said in Paste text without jumping to cursor:
Maybe I’ll look at the source code one day
If you do that, you’ll probably have to look at non-Notepad++ source code. Notepad++ uses an entirely different project for its editing functions, and where the caret moves after pasting is likely something that belongs to the editing component called “Scintilla”.
The necessary function already exists: SCI_INSERTTEXT. If Python scripting can retrieve the contents of the clipboard and can call that Scintilla function, it should be possible to script it.
-
@Coises ,
Confirmed.
The following script grabs the contents of the clipboard and inserts them at the current typing caret, instead of pasting them at the current typing caret, so this will leave the caret where it was.
# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/26203/ Running this script will _insert_ the clipboard instead of _pasting_ the clipboard, so the typing caret won't move """ from Npp import editor import ctypes def get_clipboard_text(): """https://stackoverflow.com/a/23285159/5508606""" CF_TEXT = 1 kernel32 = ctypes.windll.kernel32 kernel32.GlobalLock.argtypes = [ctypes.c_void_p] kernel32.GlobalLock.restype = ctypes.c_void_p kernel32.GlobalUnlock.argtypes = [ctypes.c_void_p] user32 = ctypes.windll.user32 user32.GetClipboardData.restype = ctypes.c_void_p user32.OpenClipboard(0) try: if user32.IsClipboardFormatAvailable(CF_TEXT): data = user32.GetClipboardData(CF_TEXT) data_locked = kernel32.GlobalLock(data) text = ctypes.c_char_p(data_locked) value = text.value kernel32.GlobalUnlock(data_locked) return value finally: user32.CloseClipboard() editor.insertText(editor.getCurrentPos(), get_clipboard_text())
If you don’t have PythonScript plugin yet, or don’t know how to install and run a script using that plugin, this FAQ explains how to do it, including how to set a keyboard shortcut
-
-
@Coises said:
The necessary function already exists: SCI_INSERTTEXT
Good eye.
I would have thought that function also does things to “jolt the screen”, but apparently not.
Very similar, but simpler and less “raw”:
from Npp import * try: editor3h # third editor, hidden except NameError: editor3h = notepad.createScintilla() def get_stream_text_from_clipboard(): retval = '' editor3h.clearAll() editor3h.paste() if editor3h.getLength() > 0: retval = editor3h.getText() return retval editor.insertText(editor.getCurrentPos(), get_stream_text_from_clipboard())
Note that the extra effort to get the clipboard text is because PythonScript doesn’t support a way to get current clipboard contents. It can paste the contents with
editor.paste()
, but if you want to avoid side effects from paste (as we do here) or you want to modify the clipboard text before inserting it, there’s no super simple (i.e. function call way to do it).Note also that my
get_stream_text_from_clipboard()
function only works with non-column block data (as its name implies).BTW, there was a PythonScript request that such a function be created, see HERE, but it seems to be an issue that isn’t going anywhere.