Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surround with quotation marks

Tags:

eclipse

Maybe this is not what you mean but...

If I'm on a line in Eclipse and I enter double quotation marks, then inside that paste a multiline selection (like your xyz example) it will paste out like this:

"xxxx\n" +   
"yyyy\n" +  
"zzz"

Then you could just find/replace in a selection for "\n" to "", if you didn't intend the newlines.

I think the option to enable this is in Window/Preferences, under Java/Editor/Typing/, check the box next to "Escape text when pasting into a string literal". (Eclipse 3.4 Ganymede)


Find/Replace with the regex option turned on. Find:

^(.*)$

Replace with:

"$1" +

Well, the last line will have a surplus +, you have to delete it manually.


I would go with a Find/Replace eclipse in regexp mode:

  • Find:

    ^((?:\s(?)\S?)((?:\s(?![\r\n])))

  • Replace with

    \1"\2"\3 +

Will preserve exactly whatever space or tabs you have before and after each string, and will surround them with the needed double-quotes. (last '+' needs to be removed)


This may not be exactly the answer you're looking for. You can easily achieve what you're asking by using the sed stream editor. This is available on all flavors of Unix, and also on Windows, by downloading a toolkit like cygwin. On the Unix shell command line run the command

sed 's/^/"/;s/$/"+/'

and paste the text you want to convert. On its output you'll obtain the converted text. The argument passed to sed says substitute (s) the beginning of a line (^) with a quote, and substitute (s) the end of each line ($) with a quote and a plus.

If the text you want to convert is large you may want to redirect sed's input and output through files. In such a case run something like

   sed 's/^/"/;s/$/"+/' <inputfile >outputfile

On Windows you can also use the winclip command of the Outwit tool suite to directly change what's in the clipboard. Simply run

winclip -p | sed 's/^/"/;s/$/"+/' | winclip -c

The above command will paste the clipboard's contents into sed and the result back into the clipboard.

Finally, if you're often using this command, it makes sense placing it into a shell script file, so that you can easily run it. You can then even assign an Eclipse keyboard shortcut to it.