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

Command substitution within vim

Expand Messages
  • cjsmall
    For years I have been using the following vim mapping: map a :. w `dictname` Here, backslash-a writes the current line (which is always a single word)
    Message 1 of 3 , Jul 5, 2014
    • 0 Attachment
      For years I have been using the following vim mapping:

      map \\a :. w>> `dictname`<CR>

      Here, backslash-a writes the current line (which is always a single word)
      out to the appropriate file which is substituted by the command "dictname".
      Apparently, vim is doing the command substitution internally in order to
      know what file to write, although I cannot find this documented anywhere.

      Recently, I decided to start using the backtick, like the backslash, for
      macros. At some point I got around to defining:

      map! `d o<div><CR></div><ESC>O

      and this began to screw up the backslash-a macro because the backtick-d
      kept getting substituted with "o<div>". I tried escaping the backtick
      in the macro definition but nothing worked. I tried replacing `dictname`
      with $(dictname) but this also fails because vim does not do the command
      substitution and tries to literally write to the nonexistent file
      "$(dictname)".

      I have a workaround, but I would really like to understand what is happening
      here.

      1: Is it documented that vim does backtick substitution? I don't understand
      how that is possible when the backtick is being used for motion commands
      identical to the forward single quote.

      2: Any suggestions on alternate means of command substitution or how to
      otherwise get the output of the dictname command into vim just before
      the macro is executed?

      Maybe this is a place where system() should be used, but I'm unclear whether and how that can be introduced into a macro.

      Thanks.

      --
      --
      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.
    • Ben Fritz
      ... That would be because your :map! command applies also to command-line mode, because you did not specify a specific mode for it to apply in (for example,
      Message 2 of 3 , Jul 5, 2014
      • 0 Attachment
        On Saturday, July 5, 2014 5:14:08 PM UTC-5, cjsmall wrote:
        > For years I have been using the following vim mapping:
        >
        > map \\a :. w>> `dictname`<CR>
        >
        > Here, backslash-a writes the current line (which is always a single word)
        > out to the appropriate file which is substituted by the command "dictname".
        > Apparently, vim is doing the command substitution internally in order to
        > know what file to write, although I cannot find this documented anywhere.
        >
        > Recently, I decided to start using the backtick, like the backslash, for
        > macros. At some point I got around to defining:
        >
        > map! `d o<div><CR></div><ESC>O
        >
        > and this began to screw up the backslash-a macro because the backtick-d
        > kept getting substituted with "o<div>".

        That would be because your :map! command applies also to command-line mode, because you did not specify a specific mode for it to apply in (for example, :imap or :inoremap). This causes a problem because your first mapping, for normal-mode, is allowing recursive mappings. I.e. you used ":map" instead of ":noremap".

        I suggest cleaning up you mappings to specify the specific mode you want, and also to disallow recursive map triggers unless you WANT them to trigger mappings recursively. For example, these two mappings of yours are probably intended to be:

        nnoremap \\a :. w>> `dictname`<CR>
        inoremap `d o<div><CR></div><ESC>O

        >
        > I have a workaround, but I would really like to understand what is happening
        > here.
        >
        > 1: Is it documented that vim does backtick substitution? I don't understand
        > how that is possible when the backtick is being used for motion commands
        > identical to the forward single quote.
        >

        That would be :help backtick-expansion. It works in command-line and also within the expression register. Using it for motion commands is in normal mode and completely different, Vim won't expand it there, but you're also not using it in that way.

        --
        --
        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.
      • cjsmall
        ... Ben: Thanks for the great reply. Most of these mappings migrated forward from the vi(1) days. I was aware of these new mapping options but I haven t
        Message 3 of 3 , Jul 6, 2014
        • 0 Attachment
          On Saturday, July 5, 2014 4:01:51 PM UTC-7, Ben Fritz wrote:

          > That would be because your :map! command applies also to command-line mode, because you did not specify a specific mode for it to apply in (for example, :imap or :inoremap). This causes a problem because your first mapping, for normal-mode, is allowing recursive mappings. I.e. you used ":map" instead of ":noremap".
          >
          > I suggest cleaning up you mappings to specify the specific mode you want, and also to disallow recursive map triggers unless you WANT them to trigger mappings recursively. For example, these two mappings of yours are probably intended to be:
          >
          > nnoremap \\a :. w>> `dictname`<CR>
          > inoremap `d o<div><CR></div><ESC>O

          Ben:

          Thanks for the great reply. Most of these mappings migrated forward from the vi(1) days. I was aware of these new mapping options but I haven't really learned or used them much. Thanks for jogging my memory. I reread the manual section and rewrite my macro definitions.

          > > 1: Is it documented that vim does backtick substitution? I don't understand
          > > how that is possible when the backtick is being used for motion commands
          > > identical to the forward single quote.
          > >
          >
          > That would be :help backtick-expansion. It works in command-line and also within the expression register. Using it for motion commands is in normal mode and completely different, Vim won't expand it there, but you're also not using it in that way.

          Thanks again! I read up on that topic too!

          Regards,
          --
          Jeff

          --
          --
          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.