Buffering Buffers


I don't think there is anybody who uses a graphical interface without periodically cutting and pasting information between windows. Under some operating systems (for example, Microsoft Windows), there is only one commonly used clipboard. This clipboard can be used to share information between applications. Under Ubuntu, X-Windows and the Gnome Desktop provide two clipboards for common use. The first is the selection clipboard. Whenever you highlight any text, it gets placed in this buffer. Using the middle mouse button, you can paste the contents.

The second clipboard (called the primary clipboard) is used when you use Ctrl+C to copy, Ctrl+X to cut, and Ctrl+V to paste. Word processors (for example, OpenOffice) and graphic editors (for example, Gimp) usually use this buffer. Also, some text applications, like gnometerminal, change cut-and-paste to use Shift+Ctrl instead of Ctrl (for example, Shift+Ctrl+C to copy).

Note 

X-Windows actually provides a couple of different clipboard buffers. Clipboard is used for selection, primary is used by Ctrl+C and Ctrl+V, and secondary is usually unused.

I usually come across two problems with the default clipboards. First, the selection clipboard loses information too quickly. Simply clicking in a window to bring it forward could accidentally select a space or other character, wiping out the current selection. Although you could click the title bar to bring the window forward and prevent clipboard changes, other windows usually cover my title bars. Second, when doing a lot of development or editing, I sometimes need more than just two clipboards. I usually end up pasting text into a temporary file and then grabbing the temporary data when I need it.

Fortunately, there is a better way to manage clipboards. The program xclip (sudo apt-get install xclip) enables you to manipulate the clipboard contents. You can dump the clipboard contents to a file or load a file into the clipboard. Using xclip, you can easily give yourself one or more additional clipboards. You can even save clipboard contents between system reboots! Think of it like a calculator with a memory button-do you want one storage area, or many? Do you want to lose the memory when the calculator shuts off, or retain the memory?

The basic xclip usage specifies whether you want to write out (-o) or read in (-i) clipboard data, and which clipboard (p for primary, s for secondary, and c for the selection clipboard). For example:

  • To store the primary clipboard to a file, use:

     xclip -o -selection p > buff 
  • To load a file into the secondary clipboard, use:

     xclip -i -selection s < buff 
  • To copy the selection clipboard into the primary clipboard, use:

     xclip -o -selection c | xclip -i -selection p 

These commands can be mapped to keys. For example, Listing 8-1 creates three additional clipboards. Ctrl+F1 copies the primary clipboard into the first storage area, Ctrl+F2 uses the second storage area, and Ctrl+F3 creates the third storage area. To recall these buffers, use Ctrl+Shift+F1, Ctrl+Shift+F2, or Ctrl+Shift+F3. Using this hack, you can copy text into the primary clipboard (Ctrl+C) and then move it into the second storage area (Ctrl+F2). Later, you can recall this buffer using Ctrl+Shift-F2 and paste it using Ctrl+V.

Tip 

This hack also enables you to inspect and easily replace the contents of the clipboard. The storage buffers from the hack (see Listing 8-1) are in $HOME/.xclip/. You can view, edit, or replace the contents and then use the Ctrl+Shift keys (for example, Ctrl+Shift+F1) to load them into the clipboard buffer.

A similar hack can be used to exchange the contents of the selection and primary buffers (Listing 8-1, mapped to Ctrl+F4). This way, if you press Ctrl+C to copy a buffer and then use Ctrl+F4 to swap clipboards, allowing the middle mouse button to paste the contents rather than using Ctrl+V.

Tip 

Swapping the clipboards may sound overly complicated or unnecessary, but some applications use specific clipboards. For example, xterm only uses the primary buffer. Anything copied into the clipboard buffer (e.g., using Ctrl+C in a word processor) cannot be pasted into an xterm window. Using Ctrl+F4, you can now paste the content.

Listing 8-1: Using xclip to Create Three Additional Clipboards

image from book
 mkdir ~/.xclip  # create space for the clipboard buffers # Map commands to actions ## Map the save commands gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_1 \   'bash -c "xclip -o -selection p > ~/.xclip/clip.1"' gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_2 \   'bash -c "xclip -o -selection p > ~/.xclip/clip.2"' gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_3 \   'bash -c "xclip -o -selection p > ~/.xclip/clip.3"' ## Map the recall commands gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_4 \   'bash -c "xclip -i -selection p < ~/.xclip/clip.1"' gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_5 \   'bash -c "xclip -i -selection p < ~/.xclip/clip.2"' gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_6 \   'bash -c "xclip -i -selection p < ~/.xclip/clip.3"' # Map keys to commands gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_1 \   '<Control>F1' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_2 \   '<Control>F2' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_3 \   '<Control>F3' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_4 \   '<Control><Shift>F1' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_5 \   '<Control><Shift>F2' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_6 \   '<Control><Shift>F3' # Make Control-F4 swap the primary and selection clipboards # Do this by using the secondary clipboard as a temporary buffer gconftool-2 -t str --set /apps/metacity/keybinding_commands/command_10 \   'bash -c "xclip -o -selection p | xclip -i -selection s ;             xclip -o -selection c | xclip -i -selection p ;             xclip -o -selection s | xclip -i -selection c"' gconftool-2 -t str --set /apps/metacity/global_keybindings/run_command_10 \   '<Control>F4' 
image from book



Hacking Ubuntu
Hacking Ubuntu: Serious Hacks Mods and Customizations (ExtremeTech)
ISBN: 047010872X
EAN: 2147483647
Year: 2004
Pages: 124
Authors: Neal Krawetz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net