How to paste a tab into OS X Terminal from clipboard?

Solution 1:

This is tricky, because you have to consider not only the pasting operation, but also the original copying step.

Indeed in OS X it seems to be impossible to paste a tab character in the Terminal app. On the other hand, pasting a tab into some other app (such as TextEdit) works fine.

On Linux, it's kinda the opposite. Pasting tabs works fine; but copying them in the first place is a problem. It depends on where you're trying to copy them from. If you simply dump the contents of a file onto your terminal (with cat for example), any tab characters get expanded: replaced with some number of spaces. If you then tried to select the text and copy from the terminal output, it would be too late. Note that it's not the cat program itself that does the expansion, it's the lower-level device driver or terminal emulator.

It's instructive to consider OS X again. On OS X, the stty command has an option to control whether tabs should be expanded on output or not. If you run stty oxtabs and then try to dump some text to Terminal, you would see that the tabs get expanded, as they unconditionally do on Linux, and if you try to copy/paste that output the tabs are replaced.

Solution 2:

If you just need to insert a tab, you can press control-v and tab.

To temporarily allow pasting tabs in bash, run:

bind '"\t":self-insert'

To re-enable tab completion, run:

bind '"\C-i":complete'

If you're pasting text to emacs, you can use a function like this instead of command-v:

(defun pbpaste ()
  (interactive)
  (shell-command-on-region
    (point)
    (if mark-active (mark) (point))
    "pbpaste" nil t))

Solution 3:

In both linux and OS/x there does not appear to be a means to paste tabs into a terminal. If anyone (eventually) comes up with a solution please feel free to add the answer.

Solution 4:

Sort of a workaround (at least in Linux)

  • v=$(cat)
  • then paste your text
  • then ctrl-D to close the 'cat' command
  • and there you have your text with tabs in $v, which you can output with echo "$v"