Search code examples
bashvimgnupg

How can I keep a file in memory during editing?


Short version : echo "testing" | vim - | grep "good"

This doesn't work as vim won't output to a pipe. It says : "Vim: Warning: Output is not to a terminal". Any way to do this? Cross-editor support would be nice too.

I've tried named pipes, but vim won't open them.

Long version : echo $passw | gpg -q -d --passphrase-fd 0 $filename | vim - | "somehow gpg encrypt it using $passw and store it back in $filename".

I'm trying to edit a gpg encrypted file but would like to not have the decrypted file on disk at any time.

The full script is here : https://github.com/ptarjan/viencrypt


Solution

  • You could just have vim encrypt it for you.

    Before you save the file in vim, filter the content through your gpg encrypter:

        :{range}![!]{filter} [!][arg]               *:range!*
              Filter {range} lines through the external program
              {filter}.  Vim replaces the optional bangs with the
              latest given command and appends the optional [arg].
              Vim saves the output of the filter command in a
              temporary file and then reads the file into the
              buffer.  Vim uses the 'shellredir' option to redirect
              the filter output to the temporary file.
              However, if the 'shelltemp' option is off then pipes
              are used when possible (on Unix).
              When the 'R' flag is included in 'cpoptions' marks in
              the filtered lines are deleted, unless the
              |:keepmarks| command is used.  Example: >
                :keepmarks '<,'>!sort
        <            
              When the number of lines after filtering is less than
              before, marks in the missing lines are deleted anyway.
            w 
    

    So, if you want to filter it through gpg (I'm guessing at the flags here):

    :%!gpg -q -d -p $password
    :w $filename
    

    You'll need to export the $password and $filename environment variables so vim has access to them if you want to use them within vim.