11

Is there a shortcut, plugin or code to wrap text with quotation marks ("" or '') in Notepad++?

Eg "text".

I don't know Python and other advanced programming languages, so please explain in a simple way ...

Hendrikto
  • 513
  • 4
  • 14
Bayu
  • 437
  • 2
  • 10
  • 19

5 Answers5

29

I know this is kind of old but I stumbled upon this while searching for an answer to an unrelated problem so maybe other people will too. Here's an improvement to nichos' answer: Instead of two regexes this can easily be done with just one

Search for ^(.+)$

Replace with "\1"

Hendrikto
  • 513
  • 4
  • 14
10

I'm not sure if you want the whole line or just each word. This will do each line: Open replace (ctrl H)
Find: ^.
Replace with: "
make sure you select "regular expression" in the search mode.
Select replace all.
That will add a " at the start of each non-blank link.

For the ending one:
Find: .$
Replace with: "

nichos
  • 157
  • 1
  • 8
8

Wrapping a selected text

  1. Npp -> Plugins -> Python Script -> New Script
  2. Select where to save ... and give the name qquote (say)
  3. in opened Npp doc insert this code:

    class qquote01:
        qq='"'
        editor.replaceSel(qq+editor.getSelText()+qq)
    

    Attention on indentation (it is PYTHON ...), Save.

  4. Npp -> Plugins -> Python Script -> Configuration ... search the qquote.py, select it.
  5. Push left button Add. This is required because we need the name of that script appeared in plugins menu.
  6. Since the name in menu, we go to Npp -> Settings -> Shortcut Mapper and look at the top of the frame for Plugin commands button, click it, then search the qquote name, assign shortcut.
  7. Restart Npp and verify, that in Npp -> Plugins -> Python Script -> item qquote exist and the script has a designated shortcut.
  8. You can replace the value of the variable qq, if you wish, on any character or sequence of characters.
Avtokod
  • 181
  • 1
  • 5
  • 1
    Works like a CHAMP. Thanks. – Love and peace - Joe Codeswell Jan 20 '14 at 22:28
  • Anyone used this recently? These steps didn't work for me...nothing happens when I run the plugin. Perhaps notepad++ changed the syntax or something of the replaceSel or getSelText functions? – Brad P. Feb 13 '15 at 14:52
  • @Brad P Npp -> `Plugins` -> `Python Script` -> `Show console` anything written in the window? – Avtokod Mar 13 '15 at 02:24
  • @Avtokod, there isn't anything displayed when I attempt to run the function, just the usual python startup stuff (Python 2.7.6...etc. etc. Ready). From the sound of your comment, it seems like I should expect some sort of error output if that qquote function failed in some way. Perhaps it's not even getting run... – Brad P. Mar 13 '15 at 13:17
  • @Brad : in that window, in the bottom, you must see `Python input box`, prepended by `>>>`. (1) in a document please select a word. (2) copy-paste `qq='"'` into the Python box, press Enter. (3) copy-paste `editor.replaceSel(qq+editor.getSelText()+qq)` into the Python box, press Enter. In the document window selected text **must be** wrapped in dbl quote. Good luck! – Avtokod Mar 14 '15 at 03:50
2

You can use regular expressions for this.

Find:

(([a-zA-Z]+):)

Replace with:

("\2"):
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
1

To create the new macro functions:

  1. Open the file shortcuts.xml you find in Notepad++ directory

  2. Into "macro" section add the following code:

    <Macro name="Selection Into Double Quotes" Ctrl="yes" Alt="yes" Shift="no" Key="50">
        <Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
        <Action type="1" message="2170" wParam="0" lParam="0" sParam='&quot;' />
        <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
        <Action type="1" message="2170" wParam="0" lParam="0" sParam='&quot;' />
    </Macro>
    <Macro name="Selection Into Single Quotes" Ctrl="yes" Alt="yes" Shift="no" Key="49">
        <Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
        <Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
        <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
        <Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
    </Macro>
    
  3. Save, close, the file, restart N++: You will find the new functions into "Macro" Menu.

To use it: Simply select the text and choose the desired menu item or use the desired keyboard shortcut showed at the right side of the menu item itself.

Edit of [2023-05-30]:

Just few days ago I got aware that there is a plugin that can be installed using Plugin Admin that can do exactly what is asked by the OP:

Surrounds the selection in single|double quotes/brackets/parenthesis/etc.

The plugin name is SurroundSelection. I installed it on Npp 853 and it seems to work as expected. How to use it: Install it and activate it from its own menu, than select the target text, and on the keyboard press the button of the symbol you want to use to wrap the selection into, for example, the parenthesis.

As far as I discovered until now it works well with

  • back-tick `Selection`
  • apostrophe 'Selection'
  • double quotes "Selection"
  • curly brackets {Selection}
  • square brackets [Selection]
  • parenthesis (Selection)
  • angular brackets

and maybe with others I did not discover yet.

I find it much better than my previous solution using Npp macros.

willy wonka
  • 1,440
  • 1
  • 18
  • 31
  • Warning: This is a good approach and works well. But be aware that this will replace your existing clipboard content with the currently selected text! – subdeveloper Feb 04 '23 at 11:24