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?
You can pipe through a short shell script:
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).
Example:
You can put this in a shell script, for example named "indent4", with contents:
Then make it executable. You can also do the chmod by right-clicking the file in Nautilus, going to properties, and changing permissions.
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.
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.)
THIS is a link to a command submitted to commandlinefu.com. 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.
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.