Help with scripting commands, on a vim-crypted file.

24 views
Skip to first unread message

Harry

unread,
Dec 6, 2008, 2:09:06 AM12/6/08
to vim_use
Hi,

I have a vim-encrypted file, say, "db". (They encryption key being
"foo", let's say.)

Now, I would like to be able to accomplish the following via a bash
script:
o Take a fresh, multi-line note from the user (via a vim session),
o Save it to /tmp/new-note file, and
o Prepend /tmp/new-note to "db".

Ideally, I would like to know how I could specify the key value "foo",
both interactively as well as via a bash script.

What I've seen so far is this.

1. If I issue
vim -c 'set key=foo" db
it results in vim interactively prompting me for the key.

2. I cannot prepend /tmp/new-note to "db". If I do this:
vim -c 'set key=jjj' -c 'r !cat /tmp/new-note' db
the contents of /tmp/new-note end up getting inserted after the 1st
line of the original "db".

Basically, I don't know how to open a new line (the 'O' command)
before the first line of a file via a script!

Regards,
/HS

Matt Wozniski

unread,
Dec 6, 2008, 4:35:29 AM12/6/08
to vim...@googlegroups.com
On Sat, Dec 6, 2008 at 2:09 AM, Harry wrote:
>
> Hi,
>
> I have a vim-encrypted file, say, "db". (They encryption key being
> "foo", let's say.)
>
> Now, I would like to be able to accomplish the following via a bash
> script:
> o Take a fresh, multi-line note from the user (via a vim session),
> o Save it to /tmp/new-note file, and
> o Prepend /tmp/new-note to "db".
>
> Ideally, I would like to know how I could specify the key value "foo",
> both interactively as well as via a bash script.
>
> What I've seen so far is this.
>
> 1. If I issue
> vim -c 'set key=foo" db
> it results in vim interactively prompting me for the key.

Use --cmd instead of -c. It will take effect before the buffer is loaded.

> 2. I cannot prepend /tmp/new-note to "db". If I do this:
> vim -c 'set key=jjj' -c 'r !cat /tmp/new-note' db
> the contents of /tmp/new-note end up getting inserted after the 1st
> line of the original "db".
>
> Basically, I don't know how to open a new line (the 'O' command)
> before the first line of a file via a script!

Use :0r instead of :r.
See :help :r | /:0r

~Matt

Harry

unread,
Dec 7, 2008, 2:46:04 AM12/7/08
to vim_use
Thanks. Any way to make it take the vimcrypt key from within the
script, instead of interactively from the keyboard?

Matt Wozniski

unread,
Dec 7, 2008, 4:51:52 AM12/7/08
to vim...@googlegroups.com
On Sun, Dec 7, 2008 at 2:46 AM, Harry wrote:
> On Dec 6, 2:35 pm, "Matt Wozniski" wrote:
>> On Sat, Dec 6, 2008 at 2:09 AM, Harry wrote:
>> > What I've seen so far is this.
>>
>> > 1. If I issue
>> > vim -c 'set key=foo" db
>> > it results in vim interactively prompting me for the key.
>>
>> Use --cmd instead of -c. It will take effect before the buffer is loaded.
>
> Thanks. Any way to make it take the vimcrypt key from within the
> script, instead of interactively from the keyboard?

When I use

vim --cmd 'set key=foo' db

it doesn't prompt me for the key. Are you saying that you see
something different?

~Matt

Harry

unread,
Dec 7, 2008, 7:49:38 AM12/7/08
to vim_use
Sorry, for being rather unclear/wrong in my previous post. I used a
combination of the overall info you provided in your first reply
without expressly telling you so. So, to answer your question, I also
get the same behavior (as you do) when I say:
vim --cmd 'set key=foo' db
meaning... no key prompting.

But here's what I'm doing presently:
vim -c "0r ! cat /tmp/new-note" -c 'set key=foo' db
The '0r' part does work. But I still get prompted for the key, even
though I specified it at the command line.

Also, btw, if I try,
vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
I begin seeing strange rendering behavior by vim: The prompt for
entering encryption key not only comes right on the console (instead
of within the vim application) but the prompt reappears for every key
letter that I type in. I'm attaching the session below. Each
'snapshot' refers to what I see on my xterm after I type a single
keystroke:

==============
Snapshot 1
[hs@box:~/tmp/tmp]
$ vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
"db" Enter encryption key:
==============
Snapshot 2
[hs@box:~/tmp/tmp]
$ vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
"db" Enter encryption key: *Enter encryption
key: *
==============
Snapshot 3
[hs@box:~/tmp/tmp]
$ vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
"db" Enter encryption key: *Enter encryption
key: * *Enter encryption key: **
==============
Snapshot 4
[hs@box:~/tmp/tmp]
$ vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
"db" Enter encryption key: *Enter encryption
key: * *Enter encryption key: ** *Enter encryption key: ***
==============
Snapshot 5
[hs@box:~/tmp/tmp]
$ vim --cmd "r ! cat /tmp/new-note" -c 'set key=foo' db
"db" [crypted] 1 line, 27 characters
Press ENTER or type command to continue
==============
Snapshot 6
1 this is the new note
2 This is the original text.
~
~
... <snip> ...
==============





So, any way to supply the key non-interactively?

/HS

Ben Schmidt

unread,
Dec 7, 2008, 8:43:24 AM12/7/08
to vim...@googlegroups.com
>> When I use
>>
>> vim --cmd 'set key=foo' db
>>
>> it doesn't prompt me for the key. Are you saying that you see
>> something different?
>
> Sorry, for being rather unclear/wrong in my previous post. I used a
> combination of the overall info you provided in your first reply
> without expressly telling you so. So, to answer your question, I also
> get the same behavior (as you do) when I say:
> vim --cmd 'set key=foo' db
> meaning... no key prompting.
>
> But here's what I'm doing presently:
> vim -c "0r ! cat /tmp/new-note" -c 'set key=foo' db
> The '0r' part does work. But I still get prompted for the key, even
> though I specified it at the command line.

Combine the two. Something like

vim --cmd 'set key=foo' -c "0r ! cat /tmp/new-note" db

probably.

The rendering you are seeing is indeed odd, but I don't have any ideas
about that quickly, and it's a side issue, so I will leave it for now!

Ben.

Agathoklis D. Hatzimanikas

unread,
Dec 7, 2008, 8:46:11 AM12/7/08
to vim...@googlegroups.com

The order of the given options matters.

Consider this:

this will echo "1" while opening a vim instance,

vim -c "let g:var = 1" -c "echo g:var"

but this will complain for an undefined variable,

vim -c "echo g:var" -c "let g:var = 1"

but if you do to that same instance,

:echo g:var

it will echo "1"


So you already have the answer, just concatenate it (it should work):

vim --cmd 'set key=foo' -c "0r !cat /tmp/new-note" db

Regards,
Ag.

Agathoklis D. Hatzimanikas

unread,
Dec 7, 2008, 9:02:52 AM12/7/08
to vim...@googlegroups.com
On Sun, Dec 07, at 03:46 Agathoklis D. Hatzimanikas wrote:
> On Sun, Dec 07, at 04:49 Harry wrote:
> >
> > But here's what I'm doing presently:
> > vim -c "0r ! cat /tmp/new-note" -c 'set key=foo' db
> > The '0r' part does work. But I still get prompted for the key, even
> > though I specified it at the command line.
>
> The order of the given options matters.
>
Although in that case doesn't (sorry), because you want to use "--cmd"
instead of "-c" as you did.

So the following command should work (no matter of the order) because
"--cmd"'s have priority (read more in :help --cmd):

vim -c "echo g:var" --cmd "let g:var = 1"

> Consider this:
>
> this will echo "1" while opening a vim instance,
>
> vim -c "let g:var = 1" -c "echo g:var"
>
> but this will complain for an undefined variable,
>
> vim -c "echo g:var" -c "let g:var = 1"
>
> but if you do to that same instance,
>
> :echo g:var
>
> it will echo "1"
>

Regards,
Ag.

Reply all
Reply to author
Forward
0 new messages