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

passing input to a filter

Expand Messages
  • Jorge Almeida
    How can I pass input to an external filter? ... (writes name of current file) ... (selects line with above contents) ... (standard input for this program
    Message 1 of 6 , May 1, 2004
    • 0 Attachment
      How can I pass input to an external filter?

      Example:
      :put %
      (writes name of current file)
      :normal v
      (selects line with above contents)
      :! perl -e '$a=<STDIN>; do-something-with-$a...'
      (standard input for this program contains selected line)
      But this won't work if the commands above are in a :function definition,
      because then standard input is not read from the line selected in visual
      mode (variable $a is empty).

      (Existing documentation talks too much about how vim is customizable but
      too little about how to actually customize it. Same remark goes to
      another editor I could mention...)

      Thanks for any help and sorry about my ignorance on vim scripting.

      Jorge Almeida
    • Jacob Lerner
      ... The answer depends on exactly what do you want to do: - Do you want to send the filename to STDIN, or the contents of the current buffer ? - Do you want to
      Message 2 of 6 , May 1, 2004
      • 0 Attachment
        Jorge Almeida wrote:

        >How can I pass input to an external filter?
        >
        >Example:
        > :put %
        > (writes name of current file)
        > :normal v
        > (selects line with above contents)
        > :! perl -e '$a=<STDIN>; do-something-with-$a...'
        > (standard input for this program contains selected line)
        >But this won't work if the commands above are in a :function definition,
        >because then standard input is not read from the line selected in visual
        >mode (variable $a is empty).
        >
        >
        The answer depends on exactly what do you want to do:
        - Do you want to send the filename to STDIN,
        or the contents of the current buffer ?
        - Do you want to substitute the contents of the buffer with
        result of the filter, or not ?

        Anyway:
        (1) if you want to send current filename as STDIN to some
        shell command:
        :!echo %|mycommand

        (2) If you want to send the contents of current buffer to shell command
        without modifying the buffer contents:

        :%w !my_command
        :w !my_command

        (3) If you want to pipe contsnts of current buffer and
        replace the contents with the fileter's STDOUT, then:

        :%! my_command

        Yakov
      • Jorge Almeida
        ... (1) was what I wanted, but (2) is handy too. I knew about (3). I suppose (1) doesn t work with other registers besides % ? Thanks for your reply. Jorge
        Message 3 of 6 , May 1, 2004
        • 0 Attachment
          On Sat, 1 May 2004, Jacob Lerner wrote:
          >
          > The answer depends on exactly what do you want to do:
          > - Do you want to send the filename to STDIN,
          > or the contents of the current buffer ?
          > - Do you want to substitute the contents of the buffer with
          > result of the filter, or not ?
          >
          > Anyway:
          > (1) if you want to send current filename as STDIN to some
          > shell command:
          > :!echo %|mycommand
          >
          > (2) If you want to send the contents of current buffer to shell command
          > without modifying the buffer contents:
          >
          > :%w !my_command
          > :w !my_command
          >
          > (3) If you want to pipe contsnts of current buffer and
          > replace the contents with the fileter's STDOUT, then:
          >
          > :%! my_command
          >
          (1) was what I wanted, but (2) is handy too. I knew about (3).
          I suppose (1) doesn't work with other registers besides % ?
          Thanks for your reply.

          Jorge
        • Gumnos (Tim Chase)
          ... [snip] ... Well, you can always build your favorite command up using the exec command, so that you have something like ... if the former doesn t work.
          Message 4 of 6 , May 1, 2004
          • 0 Attachment
            > > (1) if you want to send current filename as STDIN to some
            > > shell command:
            > > :!echo %|mycommand
            [snip]
            > (1) was what I wanted, but (2) is handy too. I knew about (3).
            > I suppose (1) doesn't work with other registers besides % ?

            Well, you can always build your favorite command up using the "exec"
            command, so that you have something like

            :exec ":!echo ".@a." |mycommand"

            if the former doesn't work. Additionally, you can open a new buffer, paste
            the contents you want to pass to stdin of "mycommand", and do a

            :%!mycommand

            which will convert the contents of the buffer from the input to the output.

            Just a few more ideas,

            -tim
          • Karthikeyan Ramnath
            ... or you could just select the range you want to filter, and do a :range! to pass the selected lines through the filter.. karthikeyan.r DISCLAIMER: This
            Message 5 of 6 , May 4, 2004
            • 0 Attachment
              > Well, you can always build your favorite command up using the "exec"
              > command, so that you have something like
              >
              > :exec ":!echo ".@a." |mycommand"
              >
              > if the former doesn't work. Additionally, you can open a new
              > buffer, paste
              > the contents you want to pass to stdin of "mycommand", and do a
              >
              > :%!mycommand
              >
              > which will convert the contents of the buffer from the input
              > to the output.
              or you could just select the range you want to filter, and do a :range! to
              pass the selected lines through the filter..
              karthikeyan.r


              DISCLAIMER: This email is bound by the terms and conditions described at
              http://www.subexgroup.com/mail-disclaimer.htm
            • Jorge Almeida
              ... Thanks for the replies, Karthikeyan and Tim. Jorge
              Message 6 of 6 , May 5, 2004
              • 0 Attachment
                On Wed, 5 May 2004, Karthikeyan Ramnath wrote:

                >
                > > Well, you can always build your favorite command up using the "exec"
                > > command, so that you have something like
                > >
                > > :exec ":!echo ".@a." |mycommand"
                > >
                > > if the former doesn't work. Additionally, you can open a new
                > > buffer, paste
                > > the contents you want to pass to stdin of "mycommand", and do a
                > >
                > > :%!mycommand
                > >
                > > which will convert the contents of the buffer from the input
                > > to the output.
                > or you could just select the range you want to filter, and do a :range! to
                > pass the selected lines through the filter..
                > karthikeyan.r
                >
                Thanks for the replies, Karthikeyan and Tim.
                Jorge
              Your message has been successfully submitted and would be delivered to recipients shortly.