Select Inline text and then add to beginning & end of selection



  • I need to mark-up text in many documents. I need to select one or more adjacent words and then add RTF formatting commands for the selection.

    Example:
    In this sentence, this is bold.

    I would select “this is bold” and add /b before “this” and /b0 after “bold”.

    Ideally I would like to have a macro or hot key to add the /b and /b0 in one key stroke.

    I would make similar hot keys for /i and /i0 and /ul and /ul0

    I am a NotePad++ newbie.

    Thanks for the help
    Blair



  • Hi Blair,

    I did that this way:

    1. Select some text
    2. Menu > Macro > Start Recording
    3. Ctrl+X, /b, Ctrl+V, /b0
    4. Menu > Macro > Save Current Recorded Macro
    5. Enter “Add bold tags” for the Name
    6. Choose a hotkey.
    7. Done. Now select some text and use the hotkey.
    8. Give Michael Godin a reputation point so he doesn’t have to wait 20 minutes between posts! :)

    Have a great day,
    Mike
    3dstudiomike@gmail.com

    P.S.: I’ve never had a problem with macros in ANY program because I’ve been using Macro Toolworks for almost 20 years now and have hundreds of well organized macros. Since Macro Toolworks provides macros at the Windows level, it can be used in all programs you use. I am sure there is an open source equivalent, but its name escapes me right now.



  • Thanks Mike!

    Works great!

    The Macro ToolWorks suggestion looks like a winner.

    Blair



  • Hello, Blair Brenner,

    IMPORTANT : This post is OUT of DATE. Just refer to my updated next post, below !

    Glad to know that Michael could help you, in that matter. Indeed, it’s the sensible solution : three short macros for adding emphasis to your RTF text.

    But just for fun, I tried to imagine an other way with … a search/replacement, based on regular expressions ! ( Building regexes is just my besetting sin ! )

    • With the macro’s method, you need to select, first, any range of characters to emphasize and, then, run the appropriate macro, one at a time.

    • With the S/R method, you would, just, have to mark with a special character the needed emphasis, while writing your text, and, then, perform this S/R, once only , at the end of the writing phase, which would write all the RTF formatting commands, in one go !

    I, arbitrary, chose these three single characters, below, for each kind of emphasis :

    • The # character for bold emphasis

    • The @ character for italic emphasis

    • The % character for underlined emphasis

    IMPORTANT : If you want to use any of the 14 characters, below, as a formatting sign, you’ll need to escape it, in the searched regular expression, with an antislash ( \ ) character !

    .   *   +   ?   (   )   [   ]   {   }   ^   $   |   \
    

    Then, I imagined TWO ways of placing these emphasis’s signs :

    • Any word, expression or sentence, surrounded by TWO identical emphasis signs, would emphasize that word, expression or sentence

      This #is an example# of text @with@ my emphasis %forms% to add !

    • Any emphasis sign, followed by a space character, located before a word, would emphasize ONLY this word !

      This is an # second example of text @ with my emphasis % forms !


    So, ONCE all your text, written with these NEW rules :

    • Go back to the very beginning of your document ( Ctrl + Origin )

    • Open the Replace dialog ( Ctrl + H )

    • In the Find what zone, type (?-s)(?|((#)|(@)|(%))([^ ].+?)\1|((#)|(@)|(%)) (\w+))

    • In the Replace with zone, type (?2/b\5/b0)(?3/i\5/i0)(?4/u\5/u0)

    • Select, at bottom, the Regular expression search mode ( IMPORTANT )

    • Click on the Replace All button

    Et voilà !

    => You should have all the RTF formatting commands, added to all the ranges of characters, previously chosen :-))

    Of course, you can save this search/replacement as a macro, as well !


    For instance, from the text, below, which is part of the license.txt file, and which contains my emphasis marks :

    The licenses for # most software are designed to take away @your freedom to share and change it@. By contrast, the %GNU General Public License% is intended to guarantee your freedom to #share and change free software#--to make sure the software is @ free for @ all its users. This %General Public License% applies to #most# of the Free Software Foundation's software and to @any@ @ other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to % your %programs, too%.
    

    would be changed, in one go, into the text below :

    The licenses for /bmost/b0 software are designed to take away /iyour freedom to share and change it/i0. By contrast, the /uGNU General Public License/u0 is intended to guarantee your freedom to /bshare and change free software/b0--to make sure the software is /ifree/i0 for /iall/i0 its users. This /uGeneral Public License/u0 applies to /bmost/b0 of the Free Software Foundation's software and to /iany/i0 /iother/i0 program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to /uyour/u0 /uprograms, too/u0.
    

    Best Regards

    guy038

    P.S.,

    In addition, with my regex method you can, perfectly, emphasize a part of a single word, only !

    For instance the syntax h#on#@ey@-% moon would produce, after the S/R, the text h/bon/b0/iey/i0-/umoon/u0 :-))



  • @guy038

    Thanks Guy. I can see the real value and time savings in your suggestion.
    I have a couple of clarifications:

    • Formatting commands

      • I need to use an anti-slash \ NOT a /
      • There needs to be a space after the fomatting command as a delimiter for RTF
        *The correct commands are:
        • bold: on "\b " off "\b0 "
        • italic: on "\i " off "\i0 "
        • underline: on "\ul " off "ul0 "
    • Use of the “13 characters”:

      • By “use” do you mean as an emphasis character?
        OR
        • “use” in the text I am writing?
      • I may use some of the 13 in my text. So I would type ? in “What is this?”
      • What if I use # @ % in my text? What do I enter?
      • As you can see, I use the anti-slash in the final output.
        • Temporarily, I got around this by changing any “/” to “” with a regular SR, as so far I have not had a “/” in my typed text

    What I have done so far was:
    * Step 1: Modify the Replace with (?2/b \5/b0 )(?3/i \5/i0 )(?4/ul \5/ul0 )
    * Step 2: Do a Normal SR Find what / Replace with
    That gives me what I need at the moment.

    What changes / suggestions do you have to answer my questions and make this more straight forward?

    Thanks so much for your insights.

    Blair



  • @Blair-Brenner said:

    @guy038

    Thanks Guy. I can see the real value and time savings in your suggestion.
    I have a couple of clarifications:

    • Formatting commands

      • I need to use an anti-slash \ NOT a /
      • There needs to be a space after the fomatting command as a delimiter for RTF
        *The correct commands are:
        • bold: on "\b " off "\b0 "
        • italic: on "\i " off "\i0 "
        • underline: on "\ul " off "ul0 "
    • Use of the “13 characters”:

      • By “use” do you mean as an emphasis character?
        OR
        • “use” in the text I am writing?
      • I may use some of the 13 in my text. So I would type ? in “What is this?”
      • What if I use # @ % in my text? What do I enter?
      • As you can see, I use the anti-slash in the final output.
        • Temporarily, I got around this by changing any “/” to “” with a regular SR, as so far I have not had a “/” in my typed text

    What I have done so far was:
    * Step 1: Modify the Replace with (?2/b \5/b0 )(?3/i \5/i0 )(?4/ul \5/ul0 )
    * Step 2: Do a Normal SR Find what / Replace with
    That gives me what I need at the moment.

    What changes / suggestions do you have to answer my questions and make this more straight forward?

    Thanks so much for your insights.

    Blair

    The anti-slash ("") was dropped out by the system when posted in a couple of important spots:

      1. I may use some of the 13 in my text. So I would type ? in “What is this?”
      • Should be: I may use some of the 13 in my text. So I would type \? in “What is this?”

          • More explicit: So I would type “anti-slash” ? in “What is this?”
        1. Temporarily, I got around this by changing any “/” to “” with a regular SR, as so far I have not had a “/” in my typed text
        • Should be: Temporarily, I got around this by changing any “/” to “\” with a regular SR, as so far I have not had a “/” in my typed text
          • More explicit: by changing any “/” to “anti-slash” with a regular SR, as so far I have not had a “/” in my typed text
        1. Step 2: Do a Normal SR Find what / Replace with \
        • Should be: Step 2: Do a Normal SR Find what / Replace with \
          • More explicit: Find what / Replace with “anti-slash”


  • Hi, Blair Brenner,

    Just feel you at ease :-)) In this second version of my regexes :

    • I simplified the search regex

    • I corrected the 3 formatting signs by changing the slash into an antislash character and adding a space character at the end

    • I, also, corrected the two underlined formatting signs from “\u” and “\u0” to “\ul” and “\ul0

    • Any formatting sign ( #, @ or % ), preceded by an antislash, is, now, re-written as a simple literal character :-)


    So, in the Find what : and the Replace with : zones, just change the previous regexes by the following ones :

    SEARCH (?-s)(?<!\\)((#)|(@)|(%))(?|([^ ].+?)\1| (\w+))|\\([#@%])

    REPLACE (?2\\b \5\\b0 )(?3\\i \5\\i0 )(?4\\ul \5\\ul0 )(?6\6)

    Don’t worry : if this second version is OK for you, I’ll fully explain theses regexes, in a new post or, may be, I added them to this present post, in a post-scriptum !

    Therefore :

    • When text is surrounded by two identical formatting signs :

      Test 01 : #single# word => \b single\b0 word
      Test 02 : @Any string with any special " ’ ^ | @ character => \i Any string with any special " ’ ^ | \i0 character
      Test 03 : %A complete sentence, with many% words => \ul A complete sentence, with many\ul0 words

    • When a single word is preceded by a formatting sign + a Space character :

      Test 04 : # single word => \b single\b0 word
      Test 05 : @ Any string with any special " ’ ^ | character => \i Any\i0 string with any special " ’ ^ | character
      Test 06 : % A complete sentence, with many words => \ul A\ul0 complete sentence, with many words

    • When a formatting sign is preceded by an antislash character \ , NOTHING is changed => The formatting sign is considered as a literal sign, only ! And the antislash character is, of course, removed ( All cases from Test 07 to Test 18 ) :

      Test 07 : #word => #word
      Test 08 : @Any string with any special " ’ ^ | character => @Any string with any special " ’ ^ | character
      Test 09 : %A complete sentence, with many words => %A complete sentence, with many words

      Test 10 : # word => # word
      Test 11 : @ Any string with any special " ’ ^ | character => @ Any string with any special " ’ ^ | character
      Test 12 : % A complete sentence, with many words => % A complete sentence, with many words

      Test 13 : word# => word#
      Test 14 : Any string with any special " ’ ^ | character@ => Any string with any special " ’ ^ | character@
      Test 15 : A complete sentence, with many words% => A complete sentence, with many words%

      Test 16 : word # => word #
      Test 17 : Any string with any special " ’ ^ | character @ => Any string with any special " ’ ^ | character @
      Test 18 : A complete sentence, with many words % => A complete sentence, with many words %

      Test 16 : wo#rd => wo#rd
      Test 17 : Any string with any special @ " ’ ^ | character => Any string with any special @ " ’ ^ | character
      Test 18 : A complete sen%tence, with many words => A complete sen%tence, with many words


    So, my previous original text, below, that I split in few lines :

    The licenses for # most software are designed to take away @your freedom to share and change it@. By contrast,
    the %GNU General Public License% is intended to guarantee your freedom to #share and change free software#--to make
    sure the software is @ free for @ all its users. This %General Public License% applies to #most# of the
    Free Software Foundation's software and to @any@ @ other program whose authors commit to using it. (Some other
    Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it
    to % your %programs, too%.
    

    would be changed, with these new regexes, as below :

    The licenses for \b most\b0  software are designed to take away \i your freedom to share and change it\i0 . By contrast,
    the \ul GNU General Public License\ul0  is intended to guarantee your freedom to \b share and change free software\b0 --to make
    sure the software is \i free\i0  for \i all\i0  its users. This \ul General Public License\ul0  applies to \b most\b0  of the
    Free Software Foundation's software and to \i any\i0  \i other\i0  program whose authors commit to using it. (Some other
    Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it
    to \ul your\ul0  \ul programs, too\ul0 .
    

    Concerning the 14 meta-characters, below ( not 13 ! ), which have a special meaning in regexes, I wanted to say : “If you intend to use any other formatting sign, than those I chose, which is one of these 14 meta-characters, you’ll need to escape it with an antislash character !”

    .   *   +   ?   (   )   [   ]   {   }   ^   $   |   \
    

    BTW, It’s just the case, in that second version, with the antislash character, itself, which is escaped by an second antislash !! If you forget this escaping sequence, in the searched regex , you’ll, probably, get the message "Invalid regular expression :-((

    But, while building your text, except, of course, for the set [#@%], you can, simply, write any symbol as it ! ( I updated that list, in my previous post, too )

    Hope that this second version looks fine to you :-))

    Cheers,

    guy038

    P.S. :

    • Blair, as a word is, generally, followed with a space character, once the regex engine changes the ending formatting sign by the correct sequences, the parts “\b0”, “\i0” and “\ul0” are, therefore, followed by TWO space characters ! Is the expected syntax that you would like to ?

    • Of course, if an ending formatting sign, is followed with a non-space character, the parts “\b0”, “\i0” and “\ul0” are, normally, followed by one space character, only !



  • Thanks guy038!

    Looks good. Will be using this on Monday.

    Best
    Blair


Log in to reply