Pasting should strip leading whitespace before auto indenting
Description of the problem or steps to reproduce
When copying text to the (internal) clipboard, and having autoindent switched on, pasting double the indentation (because it starts on the next indented position, but also paste the clipboard's lines indentation). Worse, if you have multiple line, each line will have increasing indentation! Thus, stripping indentation before pasting when autoindent is on is required IMHO.
Specifications
micro-1.2.1-dev.70-linux64 Terminal: terminology
It appears the behavior you describe is by design. Maybe the developers will add this as an option, but here's a quick fix/hack that seems to work. If you're willing to modify and build the code yourself as per the instructions in the readme, you can easily change it. Open up the cmd/micro/view.go file, and find this function:
func (v *View) paste(clip string) {
leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
if v.Cursor.HasSelection() {
v.Cursor.DeleteSelection()
v.Cursor.ResetSelection()
}
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
v.Buf.Insert(v.Cursor.Loc, clip)
// v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
v.freshClip = false
messenger.Message("Pasted clipboard")
}
and just comment out the Replace call, which is prepending the strings with leading whitespace:
func (v *View) paste(clip string) {
leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
if v.Cursor.HasSelection() {
v.Cursor.DeleteSelection()
v.Cursor.ResetSelection()
}
// ========= COMMENT OUT the next line =========
// clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
v.Buf.Insert(v.Cursor.Loc, clip)
// v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
v.freshClip = false
messenger.Message("Pasted clipboard")
}
Then rebuild as per the instructions and it will paste like other editors, and it seems to work without causing other problems, although I didn't do a full test of it. I'm not one of the devs on the project, but if I have time maybe I can look into adding an option and doing a pull request.
Update: Also, comment out the first line inside the function that declares leadingWS.
Is this still an issue and if so could you provide a way of reproducing? I'm not sure I fully understand what the problem is.
First off: congratulations on building such an epic tool @zyedidia!
I am able to reproduce this issue on Windows + WSL/Bash/Ubuntu using Hyper. It is exactly as @X-Ryl669 describes.
When copying text to the (internal) clipboard, and having autoindent switched on, pasting double the indentation (because it starts on the next indented position, but also paste the clipboard's lines indentation).
Original (with proper indentation):

Pasted into micro (beginning on line 112, autoindent adds extraneous levels of indentation as described above):

I'm pasting into micro using CTRL+SHIFT+V, which I believe results in a "paste" result indistinguishable from the user having typed each character out manually (and really quickly). I believe the proposed solution (stripping the whitespace if autoindent is on) would be the perfect fix. For now, unfortunately, I'll have to disable autoindent ☹️
Also, it's kinda annoying that the line numbers are included in the selection during copy and paste, though I guess there's not much that can be done about it (maybe Hyper's fault)?
Hmm ok. Micro should be able to detect when you are pasting with CtrlShiftV but maybe it is getting confused. It is especially helpful if you use a terminal that supports bracketed paste (this way the terminal can say when a paste event is happening).
About the line numbers, maybe Hyper doesn't have mouse support but micro should be able to interface with the mouse and handle selection properly for you. You can also toggle the line numbers quickly with CtrlR.
I Have the same issue using SecureCRT, connecting to a server and pasting code from ATOM on a Windows PC to a FreeBSD Server with micro.
HTML Code in ATOM:
Same Code after Pasting from Clipboard to micro:
Now using the Paste As Quotation function in SecureCRT:
I don't have this issue using other editors.
Please use the paste option when pasting, so that micro will not think you are typing when you are pasting (or use Ctrl-v instead of Ctrl-Shift-v/Cmd-v).
Run > set paste on before pasting and > set paste off after you are done.
I had a similar issue. Turned out I had disabled "paste bracketing" in iTerm2: https://iterm2.com/documentation-preferences-profiles-terminal.html#:~:text=paste%20bracketing Leaving this comment here, maybe it will help others.