Ubuntu – Text editor with multi-pattern search and replace at once

software-recommendationtext-editor

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…

Best Answer

...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)

Related Question