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

Specify a register for a custom command

Expand Messages
  • Nathan Neff
    Hello, I d like to create a custom command where the user could specify the register to use. Like the [ x]p and [ x]P commands in Vim. I see that there s a
    Message 1 of 9 , Nov 29, 2009
    • 0 Attachment
      Hello,

      I'd like to create a custom command where the user could specify
      the register to use.

      Like the ["x]p and ["x]P commands in Vim.

      I see that there's a "-register" option that I can use when defining
      a custom command, but I'm having trouble understanding how to use it.

      For example, I just want to create a command that prints out the contents of a register that is specified by the user.

      I tried something like this, but can't print out the <reg> part:

      :command! -register Foo :echo You picked this register: <reg>

      What am I doing wrong?

      Thanks,
      --Nate

      --
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
    • Christian Brabandt
      Hi Nathan! ... You need to expand , e.g. com! -register Foo :echo expand( ) regards, Christian -- Math problems? Call
      Message 2 of 9 , Nov 29, 2009
      • 0 Attachment
        Hi Nathan!

        On Mo, 30 Nov 2009, Nathan Neff wrote:

        > I see that there's a "-register" option that I can use when defining
        > a custom command, but I'm having trouble understanding how to use it.
        >
        > For example, I just want to create a command that prints out the contents of
        > a register that is specified by the user.
        >
        > I tried something like this, but can't print out the <reg> part:
        >
        > :command! -register Foo :echo You picked this register: <reg>
        >
        > What am I doing wrong?

        You need to expand <reg>, e.g. com! -register Foo :echo expand("<reg>")

        regards,
        Christian
        --
        Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].

        --
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
      • Andy Wokula
        ... You were just guessing? ... executes ... -- Andy -- You received this message from the vim_use maillist. For more information, visit
        Message 3 of 9 , Nov 30, 2009
        • 0 Attachment
          Christian Brabandt schrieb:
          > Hi Nathan!
          >
          > On Mo, 30 Nov 2009, Nathan Neff wrote:
          >
          >> I see that there's a "-register" option that I can use when defining
          >> a custom command, but I'm having trouble understanding how to use it.
          >>
          >> For example, I just want to create a command that prints out the contents of
          >> a register that is specified by the user.
          >>
          >> I tried something like this, but can't print out the <reg> part:
          >>
          >> :command! -register Foo :echo You picked this register: <reg>
          >>
          >> What am I doing wrong?
          >
          > You need to expand <reg>, e.g. com! -register Foo :echo expand("<reg>")

          You were just guessing?
          :Foo a
          executes
          :echo expand("a")

          Nathan, try these examples:
          :com! -register Foo echo 'Register:' <q-reg>
          :com! -register Foo display <reg>

          :h <q-args>

          --
          Andy

          --
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
        • Nathan Neff
          ... Thanks for the replies, but none of these examples do what I m looking for. I m looking to create a custom command / mapping where I can use the contents
          Message 4 of 9 , Nov 30, 2009
          • 0 Attachment
            On Mon, Nov 30, 2009 at 2:56 AM, Andy Wokula <anwoku@...> wrote:
            Christian Brabandt schrieb:
            > Hi Nathan!
            >
            > On Mo, 30 Nov 2009, Nathan Neff wrote:
            >
            >> I see that there's a "-register" option that I can use when defining
            >> a custom command, but I'm having trouble understanding how to use it.
            >>
            >> For example, I just want to create a command that prints out the contents of
            >> a register that is specified by the user.
            >>
            >> I tried something like this, but can't print out the <reg> part:
            >>
            >> :command! -register Foo :echo You picked this register: <reg>
            >>
            >> What am I doing wrong?
            >
            > You need to expand <reg>, e.g. com! -register Foo :echo expand("<reg>")

            You were just guessing?
               :Foo a
            executes
               :echo expand("a")

            Nathan, try these examples:
               :com! -register  Foo  echo 'Register:' <q-reg>
               :com! -register  Foo  display <reg>

               :h <q-args>

            Thanks for the replies, but none of these examples do what I'm looking for.

            I'm looking to create a custom command / mapping where I can use the contents of a register that is specified by the user.

            Similar to how ["x]p works.  The user can press "ap and the contents of the "a" register will be pasted.  The user can just press p and the contents of the unnamed register are pasted by default.

            I want to define a custom command where the user can put the optional ["x] in front of the mapping, and my command will work with the register that the user specified.

            If the user doesn't specify a register, then I'm going to use the unnamed register by default.

            Is there a way to create a mapping that accepts the optional ["x] in front of it?

            Thanks,
            --Nate

            --
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
          • Andy Wokula
            ... nn a : call MyPaste() func! MyPaste() exec normal! v:count1. .v:register. p endfunc And what is your command supposed to do? -- Andy
            Message 5 of 9 , Nov 30, 2009
            • 0 Attachment
              Nathan Neff schrieb:
              > I'm looking to create a custom command / mapping where I can use the
              > contents of a register that is specified by the user.
              >
              > Similar to how ["x]p works. The user can press "ap and the contents of the
              > "a" register will be pasted. The user can just press p and the contents of
              > the unnamed register are pasted by default.
              >
              > I want to define a custom command where the user can put the optional ["x]
              > in front of the mapping, and my command will work with the register that the
              > user specified.
              >
              > If the user doesn't specify a register, then I'm going to use the unnamed
              > register by default.
              >
              > Is there a way to create a mapping that accepts the optional ["x] in front
              > of it?
              >
              > Thanks,
              > --Nate

              :h v:register


              nn <Leader>a :<C-U>call MyPaste()<CR>

              func! MyPaste()
              exec "normal!" v:count1. '"'.v:register. "p"
              endfunc


              And what is your command supposed to do?

              --
              Andy

              --
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
            • Christian Brabandt
              ... No, I thought this would work. And a simple test case seemed to work. Oh well, sorry. regards, Christian -- You received this message from the vim_use
              Message 6 of 9 , Nov 30, 2009
              • 0 Attachment
                On Mon, November 30, 2009 9:56 am, Andy Wokula wrote:
                > Christian Brabandt schrieb:
                >> You need to expand <reg>, e.g. com! -register Foo :echo expand("<reg>")
                >
                > You were just guessing?

                No, I thought this would work. And a simple test case seemed to work.

                Oh well, sorry.

                regards,
                Christian

                --
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
              • Nathan Neff
                ... Good question. The command will take the contents of the specified register and paste it in a new line below the cursor. Similar to the :pu command.
                Message 7 of 9 , Nov 30, 2009
                • 0 Attachment
                  On Mon, Nov 30, 2009 at 3:36 AM, Andy Wokula <anwoku@...> wrote:
                  Nathan Neff schrieb:
                  > I'm looking to create a custom command / mapping where I can use the
                  > contents of a register that is specified by the user.
                  >
                  > Similar to how ["x]p works.  The user can press "ap and the contents of the
                  > "a" register will be pasted.  The user can just press p and the contents of
                  > the unnamed register are pasted by default.
                  >
                  > I want to define a custom command where the user can put the optional ["x]
                  > in front of the mapping, and my command will work with the register that the
                  > user specified.
                  >
                  > If the user doesn't specify a register, then I'm going to use the unnamed
                  > register by default.
                  >
                  > Is there a way to create a mapping that accepts the optional ["x] in front
                  > of it?
                  >
                  > Thanks,
                  > --Nate

                  :h v:register


                  nn <Leader>a :<C-U>call MyPaste()<CR>

                  func! MyPaste()
                     exec "normal!" v:count1. '"'.v:register. "p"
                  endfunc


                  And what is your command supposed to do?


                  Good question.  The command will take the contents of the specified register and paste it in a new line below the cursor.  Similar to the :pu command.

                  Thanks,
                  --Nate
                   
                  --
                  Andy

                  --
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php

                  --
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                • Nathan Neff
                  ... Andy, your solution worked. I have a mapping that will paste a newline plus the contents of the specified register (or the unnamed register by default)
                  Message 8 of 9 , Nov 30, 2009
                  • 0 Attachment
                    On Mon, Nov 30, 2009 at 9:09 PM, Nathan Neff <nathan.neff@...> wrote:


                    On Mon, Nov 30, 2009 at 3:36 AM, Andy Wokula <anwoku@...> wrote:
                    Nathan Neff schrieb:
                    > I'm looking to create a custom command / mapping where I can use the
                    > contents of a register that is specified by the user.
                    >
                    > Similar to how ["x]p works.  The user can press "ap and the contents of the
                    > "a" register will be pasted.  The user can just press p and the contents of
                    > the unnamed register are pasted by default.
                    >
                    > I want to define a custom command where the user can put the optional ["x]
                    > in front of the mapping, and my command will work with the register that the
                    > user specified.
                    >
                    > If the user doesn't specify a register, then I'm going to use the unnamed
                    > register by default.
                    >
                    > Is there a way to create a mapping that accepts the optional ["x] in front
                    > of it?
                    >
                    > Thanks,
                    > --Nate

                    :h v:register


                    nn <Leader>a :<C-U>call MyPaste()<CR>

                    func! MyPaste()
                       exec "normal!" v:count1. '"'.v:register. "p"
                    endfunc


                    And what is your command supposed to do?


                    Good question.  The command will take the contents of the specified register and paste it in a new line below the cursor.  Similar to the :pu command.

                    Thanks,
                    --Nate
                     

                    Andy, your solution worked.

                    I have a mapping that will paste a newline plus the contents of the specified register (or the unnamed register by default)

                    func! MyPaste()
                        exec "pu " . v:register
                    endfunc

                    nn <leader>p :<C-U>call MyPaste()

                    This seems petty - it's the difference between 5 keystrokes and 2, but I use it
                    about 30 times a day.  Can I ask what the <C-U> does?  I tried the code w/o the <C-U> and I can't see a difference in the functionality.

                    Thanks,

                    --Nate

                    --

                    Andy

                    --
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php


                    --
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                  • anna klein
                    ... You can find here. ... Note: The is required to remove the line range that you get when typing : after a count Anna -- You received this message
                    Message 9 of 9 , Nov 30, 2009
                    • 0 Attachment
                      This seems petty - it's the difference between 5 keystrokes and 2, but I use it
                      about 30 times a day.  Can I ask what the <C-U> does?  I tried the code w/o the <C-U> and I can't see a difference in the functionality.


                      You can find here.
                      :h count-variable

                      "Note: The <C-U> is required to remove the line range that you get when typing ':' after a count"

                      Anna


                      --
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                    Your message has been successfully submitted and would be delivered to recipients shortly.