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

dynamically replace = to " = " when followed by [A-z0-9]

Expand Messages
  • BaRud
    Hi, I am trying to replace = by = only when the previous character is [A-z0-9] so, blah= should be dynamically replaced by blah = , but not !=. In a
    Message 1 of 4 , Jun 16, 2014
    • 0 Attachment
      Hi,
      I am trying to replace "=" by " = " only when the previous character is [A-z0-9]
      so, "blah=" should be dynamically replaced by "blah = ", but not !=.

      In a vim, I can do this by

      %s/\v([A-z0-9])\=/\1 = /g

      but, can it be dynamic, or atleast check the current line for every = entered and replace that?

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/d/optout.
    • Ethan Hereth
      ... You might be able to use an abbreviation to do this. I have not done this but it seems like you can write a function and have the abbreviation call the
      Message 2 of 4 , Jun 16, 2014
      • 0 Attachment
        On Mon, Jun 16, 2014 at 3:54 PM, BaRud <bnrj.rudra@...> wrote:
        > Hi,
        > I am trying to replace "=" by " = " only when the previous character is [A-z0-9]
        > so, "blah=" should be dynamically replaced by "blah = ", but not !=.
        >
        > In a vim, I can do this by
        >
        > %s/\v([A-z0-9])\=/\1 = /g
        >
        > but, can it be dynamic, or atleast check the current line for every = entered and replace that?
        >

        You might be able to use an abbreviation to do this. I have not done
        this but it seems like you can write a function and have the
        abbreviation call the function. See the example under :help
        abbreviation where a function called Eatchar is used to delete a space
        typed after an abbreviation. You might be able to write a function
        that checks to see what character appears before the "=" character and
        insert a space if necessary.

        Cheers,

        Ethan Alan
        > --
        > --
        > You received this message from the "vim_use" maillist.
        > Do not top-post! Type your reply below the text you are replying to.
        > For more information, visit http://www.vim.org/maillist.php
        >
        > ---
        > You received this message because you are subscribed to the Google Groups "vim_use" group.
        > To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        > For more options, visit https://groups.google.com/d/optout.

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/d/optout.
      • Conner McDaniel
        How about something like this: inoremap = = :call replaceEqualSign() a fun! s:replaceEqualSign() let pos = virtcol( . ) let n =
        Message 3 of 4 , Jun 16, 2014
        • 0 Attachment
          How about something like this:

          inoremap <silent> = =<Esc>:call <SID>replaceEqualSign()<CR>a
          fun! s:replaceEqualSign()
          let pos = virtcol('.')
          let n = substitute(getline('.'),'\v([A-z0-9])\=','\1 = ','g')
          call setline('.',n)
          call cursor(line('.'), pos + 2)
          endfun

          - Conner


          On Mon, Jun 16, 2014 at 04:22:39PM -0400, Ethan Hereth wrote:
          >On Mon, Jun 16, 2014 at 3:54 PM, BaRud <bnrj.rudra@...> wrote:
          >> Hi,
          >> I am trying to replace "=" by " = " only when the previous character is [A-z0-9]
          >> so, "blah=" should be dynamically replaced by "blah = ", but not !=.
          >>
          >> In a vim, I can do this by
          >>
          >> %s/\v([A-z0-9])\=/\1 = /g
          >>
          >> but, can it be dynamic, or atleast check the current line for every = entered and replace that?
          >>
          >
          >You might be able to use an abbreviation to do this. I have not done
          >this but it seems like you can write a function and have the
          >abbreviation call the function. See the example under :help
          >abbreviation where a function called Eatchar is used to delete a space
          >typed after an abbreviation. You might be able to write a function
          >that checks to see what character appears before the "=" character and
          >insert a space if necessary.
          >
          >Cheers,
          >
          >Ethan Alan
          >> --
          >> --
          >> You received this message from the "vim_use" maillist.
          >> Do not top-post! Type your reply below the text you are replying to.
          >> For more information, visit http://www.vim.org/maillist.php
          >>
          >> ---
          >> You received this message because you are subscribed to the Google Groups "vim_use" group.
          >> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          >> For more options, visit https://groups.google.com/d/optout.
          >
          >--
          >--
          >You received this message from the "vim_use" maillist.
          >Do not top-post! Type your reply below the text you are replying to.
          >For more information, visit http://www.vim.org/maillist.php
          >
          >---
          >You received this message because you are subscribed to the Google Groups "vim_use" group.
          >To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          >For more options, visit https://groups.google.com/d/optout.

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/d/optout.
        • Paul Isambert
          ... You already have received solutions, here’s a one-liner: inoremap = getline( . )[col( . )-2] =~ [[:alnum:]] ? = : = Not thoroughly tested,
          Message 4 of 4 , Jun 16, 2014
          • 0 Attachment
            Le lundi 16 juin 2014 à 09:54, BaRud a écrit:
            > Hi,
            > I am trying to replace "=" by " = " only when the previous character is [A-z0-9]
            > so, "blah=" should be dynamically replaced by "blah = ", but not !=.
            >
            > In a vim, I can do this by
            >
            > %s/\v([A-z0-9])\=/\1 = /g
            >
            > but, can it be dynamic, or atleast check the current line for every = entered and replace that?

            You already have received solutions, here’s a one-liner:

            inoremap <expr> = getline(".")[col(".")-2] =~ '[[:alnum:]]' ? " = " : "="

            Not thoroughly tested, mind you.

            Best,
            Paul

            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/d/optout.
          Your message has been successfully submitted and would be delivered to recipients shortly.