micro icon indicating copy to clipboard operation
micro copied to clipboard

Pasting should strip leading whitespace before auto indenting

Open X-Ryl669 opened this issue 9 years ago • 8 comments

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

X-Ryl669 avatar Jul 11 '17 09:07 X-Ryl669

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.

jeffcogswell avatar Oct 17 '17 03:10 jeffcogswell

Update: Also, comment out the first line inside the function that declares leadingWS.

jeffcogswell avatar Dec 06 '17 16:12 jeffcogswell

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.

zyedidia avatar Jan 07 '18 19:01 zyedidia

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):

2018-01-26 3

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

2018-01-26 4

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)?

Xunnamius avatar Jan 26 '18 19:01 Xunnamius

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.

zyedidia avatar Jan 26 '18 19:01 zyedidia

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: atom

Same Code after Pasting from Clipboard to micro: SecureCRT-micro

Now using the Paste As Quotation function in SecureCRT: SecureSRT-pasteasquotation-micro

I don't have this issue using other editors.

odirone avatar Jul 05 '19 08:07 odirone

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.

zyedidia avatar Feb 10 '20 20:02 zyedidia

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.

warvariuc avatar Oct 05 '21 18:10 warvariuc