Insert characters/strings before and after selected block

Insert characters/strings before and after selected block

3
NewbieNewbie
3

    May 09, 2009#1

    I use plain text almost exclusively but manipulate it a lot. One thing I have not been able to do easily in UE (just upgraded to V15) is to select a block of text and insert strings of characters before and after the text block -- I'm actually building commands to execute. I collect lines of text, then need to add prefixes and suffixes, then recopy the block and paste the lines into another program as commands. The 'raw' lines of text collected are not necessarily the same length so using column mode would work sometimes but not always.

    The most endearing trait of the editor I've been using is the very friendly pop-up box (via keyboard shortcut) that lets me insert text, numbers, or bullets into all or any lines, and text before or after any (selected) line, and will also skip whitespace, 'xx' number of characters, and/or empty lines. I wouldn't have come back to UE except the editor lost some data due to a bug that is not fixed yet. I'll try to attach a .jpg of the pop-up box.

    Can UE do something similar?
    insert.jpg (70.18KiB)
    insert pop-up box

    6,682583
    Grand MasterGrand Master
    6,682583

      May 09, 2009#2

      There are several possibilities to insert a string before and after a selection.

      For example HTML writers often use the tag list (View - Views/Lists - Tag List) to insert a HTML tag like <strong></strong> or <p></p> around a selection.

      But the tags from the tag list can be inserted only by double clicking on a tag in the tag list view. So IDM introduced the HTML toolbar with the customizable HTML toolbar commands which support also inserting strings (primary HTML or XHTML tags) around a selection.

      But the HTML toolbar commands cannot be executed by key which is not good for HTML writers using mainly the keyboard. But the up to 50 templates (Advanced - Display/Modify Templates) can be executed by key and also from within macros and scripts. And templates support also inserting strings around a selection using a template definition like string[$replace$]string.

      Another possibility is to use a simple non regular expression replace by searching for ^s which means selected text and replacing it with string^sstring which works for blocks not larger than 30.000 characters (bytes). As macro code:

      IfSel
      Find "^s"
      Replace All SelectText "string^sstring"
      EndIf

      Last you can cut the selected block to a user clipboard, insert the string before the selected block, paste back the block and insert the string after the selected block. Recorded into a macro this version looks like:

      IfSel
      InsertMode
      Clipboard 9
      Cut
      "string"
      Paste
      "string"
      ClearClipboard
      Clipboard 0
      EndIf

      This method works nearly always, but has the disadvantage that it produces several undo steps in comparison to all other methods producing only 1 undo step.

      3
      NewbieNewbie
      3

        May 29, 2009#3

        Thanks for the ideas, Mofi!

        Unfortunately it's just too cumbersome, I'll shelve UE and go back to my other editor.

        In looking through the forum entries to see if someone had already raised this issue, there were others who wanted to insert line numbers, etc. in the text. Perhaps the developers might accept this as a future enhancement. The 'insert options' box (see the .jpg file) in my other editor are really really simple to use.

        Thanks again for your help.

        6,682583
        Grand MasterGrand Master
        6,682583

          May 29, 2009#4

          Oh, I haven't loaded the attached image and now as I loaded it, I could see that you meant something different than I thought. You want to insert something on every line of the current selection. This is done in UltraEdit normally by either using regular expression replaces on current selection (which you can save for fast re-use) or by using the column editing mode.

          For example you toggle to column edit mode with Column - Column Mode (Alt+C), select a column over multiple lines with or without selecting characters and just type what you want to insert on the selected lines in the current column. For numbering there is Column - Insert Number. Column - Insert/Fill Columns is also used very often by me.

          3
          NewbieNewbie
          3

            May 31, 2009#5

            Hi Mofi, thanks for your comments...
            I have also used column mode which I find most useful for many things and thought it would be the answer. The problem is that in some cases the end of the line for the commands I'm trying to build is not in the same column. And depending on the command, it may or may not allow blanks so I have to go through the list and remove superlfluous blanks manually. If there are many of them, it's rather tedious and prone to errors. My other little editor will put whatever characters you enter at the beginning and/or end of each line, including blanks (or not) in whatever position the beginning or end of the line may fall. It's truly most useful! That feature keeps me from migrating to UE, I can't find an easy way to do it.

            6,682583
            Grand MasterGrand Master
            6,682583

              Jun 01, 2009#6

              Inserting something on begin or end of every line in a selection can be done easily with regular expressions. You can save them for re-use. You can also write 1 or more scripts which offers you exactly the same functionality of the dialog in your other editor, but of course without an easy to use dialog.

              For example to append something at end of every line run a regular expression replace with search string $ (means end of line) and use the string you want to append at end of every line as replace string.

              To insert something at start of every line search for regular expression string % (UltraEdit regex engine) or ^ (Unix/Perl regex engine) and the replace string is the string you want to insert. Well, I use normally %^([~^p]^) (for UE) or ^([^\r\n]) (for Unix/Perl) as search string and the replace string ends with ^1 (for UE) or \1 (for Unix/Perl) to insert a string only at beginning of non blank lines of a selection.

              To insert a string at beginning and end of every selected line (blank or with content) with the UltraEdit regular expression engine use:

              Find What: %^(*^)$
              Replace With: string^1string

              To do the same as above but only on non blank lines use:

              Find What: %^(?+^)$
              Replace With: string^1string

              To insert a string at beginning after the preceding whitespaces and end of every selected line with the UE regular expression engine use:

              Find What: %^([ ^t]++^)^(*^)$
              Replace With: ^1string^2string

              And if above should be done only on lines having at least 1 non whitespace character the UE regular expression is:

              Find What: %^([ ^t]++^)^([~ ^t^p]?++^)$
              Replace With: ^1string^2string

              All regular expression above can be also done with Unix or Perl regular expression engine. Just the search and replace strings must use the different syntax of Unix/Perl.