Monday, December 21, 2015

Re: Question about '[,']!filter versus %!filter (in autocmd)

On Tue, Dec 22, 2015 at 1:44 AM, <raf@raf.org> wrote:
> Hi,
>
> I found the following auto commands somewhere for using gnupg:
>
> " Transparent editing of gpg encrypted files.
> " By Wouter Hanegraaff
> augroup encrypted
> au!
>
> " First make sure nothing is written to ~/.viminfo while editing
> " an encrypted file.
> autocmd BufReadPre,FileReadPre *.gpg set viminfo=
> " We don't want a various options which write unencrypted data to disk
> autocmd BufReadPre,FileReadPre *.gpg set noswapfile noundofile nobackup
>
> " Switch to binary mode to read the encrypted file
> autocmd BufReadPre,FileReadPre *.gpg set bin
> autocmd BufReadPre,FileReadPre *.gpg let ch_save = &ch|set ch=2
> " (If you use tcsh, you may need to alter this line.)
> autocmd BufReadPost,FileReadPost *.gpg '[,']!gpg --decrypt 2> /dev/null
>
> " Switch to normal mode for editing
> autocmd BufReadPost,FileReadPost *.gpg set nobin
> autocmd BufReadPost,FileReadPost *.gpg let &ch = ch_save|unlet ch_save
> autocmd BufReadPost,FileReadPost *.gpg execute ":doautocmd BufReadPost " . expand("%:r")
>
> " Convert all text to encrypted text before writing
> " (If you use tcsh, you may need to alter this line.)
> autocmd BufWritePre,FileWritePre *.gpg '[,']!gpg --default-recipient-self -ae 2>/dev/null
> " Undo the encryption so we are back in the normal text, directly
> " after the file has been written.
> autocmd BufWritePost,FileWritePost *.gpg u
> augroup END
>
> This is very similar to the script at http://vim.wikia.com/wiki/Edit_gpg_encrypted_files
> where, in the discussion, someone comments that they don't understand why '[,']!gpg
> was used instead of %!gpg.
>
> Can anyone explain this? '[,'] refers to the previously changed or yanked
> text whereas % refers to the entire file (which is what is wanted).
>
> Will '[,'] always refer to the entire file in the autocommands above?
> If so, how so? Would it be better with % or identical?
>
> I can see that it would refer to the entire file in the BufReadPost,FileReadPost
> state because the entire file has just been read so the last "change" is the entire
> file but what about the BufWritePre,FileWritePre state?
>
> I should say that saving works even if I have changed or yanked part of the
> file immediately before saving. I just don't know why it works.
>
> cheers,
> raf

The % range is a shortcut for 1,$ which means "first line to last
line". OTOH, using an unabbreviated range lets you use the full power
of "how to define a line". In :'[,']!filter, the range goes from the [
mark to the ] mark, i.e. from the line containing the first character
of the latest change or yank, to the line containing the last one. At
the BufReadPost and FileReadPost autocommands, the last operation on
the file consisted of reading it in, so that "the text latest changed"
is the whole file. Even if the viminfo is used to keep a file's mark
from one session to the next, the '[ and '] marks are not saved: see
the help mentioned below.

See:
:help :range
:help '[
:help ']
:help viminfo-file-marks
and also the full section starting at
:help mark-motions


Best regards,
Tony.

--
--
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@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment