1

I'm using ZSH on Mac OS if that is important. When doing some manual cherry picking on branches in git, I sometimes would like to type git checkout origin/develop --<path> where I copy and paste from gitlab. However for some reason sometimes the copy and paste feature seems to fail but then there is a newline for some reason on the clipboard and it checks out the branch. I would like to prevent that.

5
  • I just add a comment char # (#git checkout origin/develop --<path>) then paste/edit command line then remove #. If there is a newline in pasted text or I accidentaly depress CR i just press arrow-up and continue editing.
    – ReAl
    Mar 9, 2020 at 10:07
  • If you use iTerm, it will warn you about pasting a newline and offer to remove it: i.stack.imgur.com/gqlaq.png
    – muru
    Mar 9, 2020 at 10:09
  • @muru Is it possible without using iTerm? I use the Terminal function of PHPStorm a lot
    – Thomas
    Mar 9, 2020 at 10:57
  • 4
    Modern versions of zsh enable bracketed paste by default: when you paste a newline, it pastes a newline, it doesn't immediately run the command. This requires support from the terminal. If your terminal doesn't have this feature, I recommend that you switch to a better one. Mar 9, 2020 at 12:49
  • Damn I just realized as a vim user I can't disable newlines when I want to paste into a file. Aug 18, 2022 at 17:41

1 Answer 1

3

This is possible with iTerm as follows. I chose Ctrl+Shift+V as my hotkey when programming the following as that is used in Chrome and other applications to be "paste as plain text."

Option 1: Strip all new lines

Preferences -> Keys (tab) -> + (button) -> Action: Paste... -> Remove newlines (checkbox checked).

Option 1: Remove newlines

Pasting the following:

asdf
asdf

Will result in this:

asdfasdf

Option 2: Replace new lines with spaces via regex

Preferences -> Keys (tab) -> + (button) -> Action: Paste... -> Replace (checkbox checked) and enter the following:

(\r\n|\r|\n) -> (a space character).

Option 2: Replace new lines with spaces via regex

Pasting the following:

asdf
asdf

Results in this (this is my preference over Option 1 above):

asdf asdf

It is still safe (the command won't execute, but it is more readable in case there are multiple lines).

1
  • Will this work if you trigger paste via the middle mouse button instead of the keyboard? May 26, 2022 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .