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

  • On second thought, doesn't this belong on Stack Exchange site Software Recommendations? – Peter Mortensen Oct 23 '14 at 20:13
  • I made a text editor that does full Python-style regular expressions (using the Python regex module). It also allows for using number groups for replacements, and even allows for using custom functions for replacements. I haven't released it yet, but I'm working on getting it ready for release. Remind me to keep you posted if you're interested, if I don't say anything. It will replace everything at once, or one thing at a time, or everything after or before the text insert. – Brōtsyorfuzthrāx Oct 24 '14 at 2:59
  • @PeterMortensen That section is still in beta. On the other hand, looking at the most recent 50 questions at this time (android, games, MSWord ...) and the answers given, I'd say NOT. – alwaysask Oct 24 '14 at 6:49
  • @user2962794 Looking forward, good luck! – alwaysask Oct 24 '14 at 6:59
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)

  • This is pretty much what use now but I need to check the result before copying the text to the clipboard and some times do some manual changes (and do it fast). Nice answer but it needs to be a text editor. – alwaysask Oct 24 '14 at 6:30
  • Hmmm, yes, I understand. Let see... – Rmano Oct 24 '14 at 7:27
  • Nice. It's a bit better/faster than I currently do. Thanks. I voted up but the Q remains - a text editor with multi-pattern text replace capability built in. Even as a plugin for gedit or alike. – alwaysask Oct 24 '14 at 18:00
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

  • As I said in my comment on the other answer, I'd prefer a text editor. – alwaysask Oct 24 '14 at 6:57
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.

  • 1
    You can also chain a bunch of commands using |, for example: :%s/foo/bar/g | %s/brown/fox/g | %s/lorum/ipsun/g. Or put the commands one line at a time in a file and use :source commandfile.vim to run the commands. Or create a macro... – evilsoup Oct 23 '14 at 9:25
  • Vim was never too appealing for me (I'm spoiled, yes) but I'll give it a go a.s.a.p. and see if/how it gets the job done. – alwaysask Oct 24 '14 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.