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?

2 Answers 2


Yes, your observation is correct. What happens when you copy-paste is that the target obtains the data from the source. When displaying your tabs, the xterm (and konsole), apparently, translate them into spaces themselves (if the application running inside the terminal has not done that already). Thus, when a request comes for the selected text, all they have to offer is the spaces.

I even vaguely remember a response from an xterm-maintainer years back, claiming it would be very difficult to fix...

I'm not sure, how gnome-terminal does the right thing, but I'm glad they do :) Another alternative is to use gvim (graphical version of vim) -- instead of running vim inside a terminal application -- you can safely copy-paste from gvim, without losing tabs.


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.

  • 2
    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. Jun 4, 2013 at 10:43
  • It's worth noting that in Roff (troff & nroff), tabstops can be set independently of each other, much like the "ruler" toolbars in modern word processors (OpenOffice, Microsoft Word, et al). Manpages rarely use custom tab-stops in practice, however.
    – user403378
    Jul 12, 2018 at 5:56
  • mandoc(1) also allows users to override the default indentation width from command-line, using -O indent=N (defaults to 5 columns).
    – user403378
    Jul 12, 2018 at 6:00

You must log in to answer this question.

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