8

I'm looking for a GUI text editor that is capable of doing a multiple-pattern search and replace all at once. If the source would be a text file, it can be done in command line mode with sed, but the text comes from the clipboard (no other way possible).

I need to: paste the text from the clipboard (5-50 words at most) in text editor, hit the 'replace' button with a set of 10 predefined replace patterns, and copy the result to the clipboard. This sequence will be done about 3000 times...

Pandya
  • 37,289
alwaysask
  • 220

3 Answers3

13

...why not use a script? Check xclip (sudo apt-get install xclip)

 xclip -o -selection clipboard  

will send the clipboard to standard output, and with -i you can replace the clipboard. So

 xclip -o -selection clipboard | sed "s/change this/to this/" | xclip -i -selection clipboard 

will apply the change to the selection, and now you can paste it.

If you want a graphical thing, you can embed the script with yad:

#! /bin/bash 
#
yad --title Choose --button One:1 --button Two:2 --button Three:3
choice=$?
case $choice in
        1) 
        xclip -o -selection clipboard | 
                sed "s/one/uno/" | 
                xclip -i -selection clipboard
        xclip -o selection clipboard
        ;;
        2)      
        xclip -o -selection clipboard | 
                sed "s/two/dos/" | 
                xclip -i -selection clipboard
        xclip -o selection clipboard
        ;;
        3)
        echo "executing 3 --- well, you got the idea"
        ;;
esac

That will show you a dialog like this:

YAD example

Notice that the script will both modify the clipboard (paste) buffer and print it. To embed this in an editor, for example vim, you can do the following:

  1. Add to your .vimrc:

    nmap <F4> :r ! /path/to/the/script <CR>
    
  2. run for example gvim.

  3. Now you copy the text, go the the editor, press F4. Choose the change you want to apply.

  4. The text will appear in the editor. If it's ok as is, you can paste it. Otherwise

  5. Edit the text and copy it again. (In gvim, you can select the text with the mouse and simply choose paste --- or learn the vim commands, whatever).

It could be optimized for sure (you probably can easily define another key to select and paste the modified text so that you have even less keypress to use)

Rmano
  • 32,167
5

You can do this all on the command line still using something like xsel or xclip to retrieve the current clipboard and then stuff the result back into it. Here's a little example that shows sed being used to do multiple replacements.

echo -n abc | xsel -bi                               # write to clipboard
xsel -bo | sed 's/abc/def/;s/def/123/' | xsel -bi    # process it
echo $(xsel -bo)                                     # output it for testing

Returns 123

Oli
  • 299,380
3

I present to you the Vim editor! (or Gvim for the graphical implementation)

# apt-get install gvim

With this text-editor you can do pretty much everything. It's based on keyboard shortcuts like i for entering text and dd for deleting textlines, dw for deleting words, d2w for deleting two words.

In your case: to paste your text from the clipboard use "*p

And to replace text use :s/foo/bar/g replacing foo with bar here.

muru
  • 207,228
Pascal
  • 173