Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I occasionally have the need to indent a block of text with spaces for use within a web browser, for example, when formatting a code block on this site or in a post on Launchpad.

So far I've just done it by hand by copying four spaces to the clipboard and then mashing keys really fast:

, Home, Ctrl+V (repeat)

What is the quickest way to accomplish this?

  • Copying and pasting to another program? (Which?)
  • A Firefox or Chrome browser extension?
  • A command to directly modify the clipboard contents?
  • An auto-typing program?
share|improve this question
Since the exact command depends on the editor/ide and possibly the OS, please specify your environment. – David Harris Dec 28 '10 at 21:39
This question was mistakenly migrated to meta. This is a question not only about here on Ask Ubuntu - but in any application. Answers should be application independent whenever possible. – Marco Ceppi Dec 29 '10 at 2:38
up vote 3 down vote accepted

You can pipe through a short shell script:

$ xsel -b | sed 's/^/    /' | xsel -b

The first xsel -b reads the clipboard, sed adds four spaces (^ matches start-of-line), then the second xsel -b puts it back on the clipboard. Drop the -b to use the primary selection instead (the middle-click-paste buffer).


# put two lines in the clipboard, "abc" and "123", for the example
# the \n is a newline, and echo adds another newline to the end
$ echo $'abc\n123' | xsel -b

$ xsel -b | sed 's/^/    /'  # output written to the terminal
$ xsel -b | sed 's/^/    /' | xsel -b  # again, to the clipboard

You can put this in a shell script, for example named "indent4", with contents:

xsel -b | sed 's/^/    /' | xsel -b

Then make it executable. You can also do the chmod by right-clicking the file in Nautilus, going to properties, and changing permissions.

$ chmod +x indent4

# test it:
$ echo $'abc\n123' | xsel -b  # load clipboard
$ ./indent4                   # assuming it's in the current directory
$ xsel -b                     # show clipboard

And then put the file on your desktop, or store it anywhere and make a launcher for it. Now you can run indent4 (e.g. double-click) and the clipboard will be modified.

xsel is from a package named, not surprisingly, "xsel"; you may have to install it. See "man xsel" for details. The $''-style strings in the example are bash-specific.

share|improve this answer
If you're already in an editor, it surely supports some way of doing this easily (maybe just selecting lines and pressing tab), but this works outside of any specific editor. – Fred Nurk Dec 29 '10 at 5:05
This is great. I've added a keyboard shortcut to run xsel -b | sed "s/^/$(zenity --entry --title "Modify clipboard" --text "Enter text to be prepended to each line in the clipboard:")/" | xsel -b. – ændrük Dec 30 '10 at 0:54
@ændrük: There are some special characters you need to be careful of (or escape) with that method, but otherwise, I'm glad it works well. – Fred Nurk Dec 31 '10 at 22:12

I use the pentadactyl plug-in for Firefox. (This is a successor to the vimperator plugin.)

Besides a lot of other great features, it lets you edit any text box in a webpage with an external editor. (You press Ctrl-I when editing the text box and an editor opens; when the editor saves the file, it is automatically copied back into the text box.) I use gvim as my external editor. In (g)vim, the > key in visual mode (i.e., when characters are selected or highlighted) will indent all the selected lines by an amount you set in your .vimrc.

There are ways of setting up pentadactyl to tell gvim what filetype is being edited (e.g., html or markdown for this page, or bbcode for the Ubuntu forums, etc., so you can use filetype-specific plugins and even get syntax highlighting.)

share|improve this answer

THIS is a link to a command submitted to It properly indents your whole file using emacs, so you don't have to think about what's a space and what's a tab.

share|improve this answer

You can copy the test into Gedit, highlight the whole thing (CTRL-A), hit Tab, then copy again. It's manual, but it's quick.

share|improve this answer
I regularly use Gedit's automatic tab indentation, so for this to work I would have to first set Edit ▸ Preferences ▸ Editor ▸ Insert spaces instead of tabs, then press Tab , and then unset Edit ▸ Preferences ▸ Editor ▸ Insert spaces instead of tabs again, which I feel is a little too cumbersome. – ændrük Dec 29 '10 at 21:55

Your Answer


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.