Loading ...
Sorry, an error occurred while loading the content.

Enclosing current line in HTML tags

Expand Messages
  • Robert MannI
    Hello! What s the quickest way to enclose the current line the cursor is on in, say, tags? Thank you, Rob
    Message 1 of 5 , Dec 31, 2006
    • 0 Attachment
      Hello!


      What's the quickest way to enclose the current line the cursor is on
      in, say, <li></li> tags?




      Thank you,
      Rob
    • A.J.Mechelynck
      ... I suggest ... which can be mapped to a key. This method lends itself to adding an arbitrary tag (in a script, with the tag ... but I don t see an easy way
      Message 2 of 5 , Dec 31, 2006
      • 0 Attachment
        Robert MannI wrote:
        > Hello!
        >
        >
        > What's the quickest way to enclose the current line the cursor is on
        > in, say, <li></li> tags?
        >
        >
        >
        >
        > Thank you,
        > Rob
        >

        I suggest

        :s/^.*$/<li>\0<\/li>/

        which can be mapped to a key.

        This method lends itself to adding an arbitrary tag (in a script, with the tag
        in a variable):

        :exe 's/^.*$/<' . tag . '>\0<\/' . tag . '>/'

        but I don't see an easy way to apply it to a characterwise visual selection
        (as the OP wanted).


        Best regards,
        Tony.
      • Tim Chase
        ... Depends on what you mean by quickest ...are you looking for a one-shot, or are you looking for something you can map, or are you looking to perform the
        Message 3 of 5 , Dec 31, 2006
        • 0 Attachment
          > What's the quickest way to enclose the current line the cursor is on
          > in, say, <li></li> tags?


          Depends on what you mean by "quickest"...are you looking for a
          one-shot, or are you looking for something you can map, or are
          you looking to perform the action on a number of lines?

          For a simple one-shot, you can just do

          I<li>^[A</li>^[

          (where ^[ is escape). If you want the opening tag at the
          column-beginning-of-line rather than before the first, start with
          "gI" instead of just "I".

          If you plan to do a one-shot, but across a whole bunch of lines,
          you can use

          :%s!.*!<li>&</li>

          Or, if you just want to do it for lines matching a particular
          pattern, you can use

          :g/pattern/s!.*!<li>&</li>

          If you want the opening tag after leading whitespace, you can
          change those to

          s!^\s*\zs.*!<li>&</li>

          Lastly, if it's something you want to do regularly, you can map
          any of the above behaviors with something like

          :nnoremap <f4> :s!^\s*\zs.*!<lt>li>&<lt>/li><cr>

          Hope this gives you some ideas with which to work.

          -tim
        • David Fishburn
          ... Wow, 2 emails in the same day. I created a tip for this: Tip #346: Wrap text in HTML/XML tags after prompting for the tag name
          Message 4 of 5 , Dec 31, 2006
          • 0 Attachment
            > What's the quickest way to enclose the current line the
            > cursor is on in, say, <li></li> tags?

            Wow, 2 emails in the same day.

            I created a tip for this:
            Tip #346: Wrap text in HTML/XML tags after prompting for the tag name
            http://www.vim.org/tips/tip.php?tip_id=346


            With some refinements at the bottom (plus a pile of SPAM!!).

            What I did was if the visually selected text spanned more than 1 line I
            added:

            <something>
            Akj lkjaf kjdsflj fjalf js
            Akj lkjaf kjdsflj fjalf js
            </something>


            But if the visually selected text is < 1 line then I do:
            Akj lkjaf <something>kjdsflj</something> fjalf js


            HTH,
            Dave
          • Alan G Isaac
            ... Here s one approach: http://www.american.edu/econ/notes/html_xhb.vim hth, Alan Isaac
            Message 5 of 5 , Jan 1, 2007
            • 0 Attachment
              On Sun, 31 Dec 2006, Robert MannI apparently wrote:
              > What's the quickest way to enclose the current line the
              > cursor is on in, say, <li></li> tags?

              Here's one approach:
              http://www.american.edu/econ/notes/html_xhb.vim

              hth,
              Alan Isaac
            Your message has been successfully submitted and would be delivered to recipients shortly.