Sublime Forum

Copy paste indent problem

#1

Selected part is the original which I want to copy:

When I paste it using paste_and_indent , and outside for loops it becomes:
t1 = [‘qwerty’, ‘asdfgh’, ‘zxcvbn’]

for t in t1:
	for tx in t:
		print()
		print()
		
		print()
		print()

When I paste it without paste_and_indent, and outside for loops it becomes:
t1 = [‘qwerty’, ‘asdfgh’, ‘zxcvbn’]

for t in t1:
	for tx in t:
		print()
		print()
		
print()
		print()

Here I want that whenever I paste the text. The indentation in the second line(or the next coming lines) must be removed just like the first “print()” statement.

It must look like:
t1 = [‘qwerty’, ‘asdfgh’, ‘zxcvbn’]

for t in t1:
	for tx in t:
		print()
		print()
		
print()
print()

It’s really annoying since I just made my transition from Atom to ST3 and in Atom it was almost automatic. I’m sure there must be something in ST3 to make this happen, about which I don’t know. Thanks.

0 Likes

#2

Interesting. So a new paste mode which is essentially trim-and-paste? I would say that the normal paste is doing the right thing by keeping the sequence of characters exactly as they were, including whitespace, but I often have the exact same scenario and have gotten used to paste+select+unindent as a normal way of re-adjusting.

Seems there are a few plugins, but I don’t see one that does this.

0 Likes

#3

the problem with paste+select+unindent is that for a case of multiple lines it becomes impractical, and causes wastage of time. It was comparatively easier with just two print() statements in this case but what if the number of statements gets larger?

0 Likes

#4

maybe just put a comment marker at column 0 on the line above where you want to paste and indent, and make sure you select the whole line you want to copy to the clipboard - i.e. include the whitespace before the first print, so that paste_and_indent knows that the indentation for the two print statements should be the same

0 Likes

#5

Oki. Yes. That helped a lot :slight_smile: Thank you very much. I’ve been at it since past 4 hours trying to make it work. This can be considered a hack I guess, but once it gets into muscle memory it won’t matter that much.

0 Likes

#6

You could also use this command (you should make a plugin and put it in User folder):

import sublime
import sublime_plugin

class PasteAndUnindent(sublime_plugin.TextCommand):

    def run(self, edit):
        clip = sublime.get_clipboard()
        clip = clip.split("\n")
        clip = [line.lstrip() for line in clip]
        clip = "\n".join(clip)
        sublime.set_clipboard(clip)
        self.view.run_command('paste')

This always strips any indentation, if that’s what you want. Also assign a keybinding like:

{ "keys": ["ctrl+shift+alt+v"],     "command": "paste_and_unindent"},
1 Like

#7

Thanks this seems to do the trick.

0 Likes