Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When I copy and paste text using the X11 mouse selection between two terminal windows, tabs are often, but not always, converted to spaces. This appears to depend on the involved terminal applications and the programs running inside them.

This is what I've found out by experimentation:

  • It depends solely on what's running on the source (copy) side. The target (paste) side will paste tabs no matter what is running, if the source preserved them.
  • Only gnome-terminal on the source side preserves tabs. xterm and konsole do not.
  • Tabs are only preserved if the file has been output using cat. Other applications such as less, vim, emacs, or nano don't preserve tabs.

What is the explanation for this? Are these bugs in konsole and xterm, or can they be configured differently? Can less and such be configured to preserve tabs in output?

share|improve this question

1 Answer 1

This can sometimes be a problem, especially with Makefiles which require tabs. Unfortunately, the problems with different programs are not related. Each one will probably have a very different solution.

For vim you must preform a :set noexpandtab or place set noexpandtab in your ~/.vimrc file. If you want only certain filetypes to have noexpandtab set then you can do an autocommand in your ~/.vimrc I have the following in mine:

"indenting defaults: 4 spaces for an indent
set shiftwidth=4
set softtabstop=4
set expandtab

"exceptions to indenting for specific languages and files--------
"for shell scripts: 2 spaces for an indent
autocmd filetype sh setlocal shiftwidth=2
autocmd filetype sh setlocal softtabstop=2

"for Makefiles: use tabs for indents
autocmd filetype make setlocal tabstop=8
autocmd filetype make setlocal noexpandtab

This will set a default of 4 spaces for a tab in vim and make exceptions for shell scripts and Makefiles.

For the other programs like less, try looking at there man pages. They may have configuration files, environmental variables, or command line options for what you need. Looking at the less man page:

-xn,... or --tabs=n,...
       Sets tab stops.  If only one n is specified, tab stops  are  set
       at  multiples  of n.  If multiple values separated by commas are
       specified, tab stops are set at those positions, and  then  con-
       tinue  with  the  same  spacing  as  the last two.  For example,
       -x9,17 will set tabs at positions  9,  17,  25,  33,  etc.   The
       default for n is 8.

What you are noticing isn't a bug. "Its a feature." The only problem is you need to know how to turn it off.

share|improve this answer
    
less -xn configures it to use n spaces rather than a tab character. -U makes it display tab as ^I. I haven't found a way to make it emit a literal tab character so that copying the text will paste a tab character, but I'd love to know how. –  Matt McClure Jun 4 '13 at 10:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.