18

I want to be able to easily start Vim in a private mode. Let's define a private mode as one that utilizes this set of commands:

set history=0
set nobackup
set nomodeline
set noshelltemp
set noswapfile
set noundofile
set nowritebackup
set secure
set viminfo=""

Let me know if you can think of any additional commands suitable for the private mode.

The point is I cannot think of any short and easy way to start Vim with this setup.

Here are some ideas I've come up with:

  1. Load .vimrcprivate instead of .vimrc

    You can simply put all the necessary commands into .vimrcprivate and start Vim like this:

    vim -u .vimrcprivate
    
  2. Define and call a Private() function

    Another idea add a code like this to your .vimrc:

    function Private()
        " set of commands here
    endfunction
    

    and then you are able to start Vim in a private mode like this:

     vim -c 'call Private()'
    
  3. Use a variable to trigger the setting up of your commands

    It is quite similar to the second solution. Just add

    if exists('privatemode')
        " set of commands here
    endif
    

    to your .vimrc and then start Vim using

    vim -c 'let privatemode=1'
    

    which is neither smart nor short.

  4. Add an alias to your .bashrc (or to any other .*rc file your shell is using)

    For example:

    alias vimprivate="vim +\"set history=0\" +\"set nobackup\" +\"set nomodeline\"\
        +\"set noshelltemp\" +\"set noswapfile\" +\"set noundofile\"\ 
        +\"set nowritebackup\" +\"set secure\" +\"set viminfo=\"\"\""
    

    which is awful. I don't like it.

  5. Use an environmental variable

    Just run VIM_PRIVATE=1 vim and add

    if $VIM_PRIVATE
        " set of commands here
    endif
    

    to your .vimrc.

  6. Some magic (from a comment by Carpetsmoker in this thread)

    Carpetsmoker says in this comment that

    I would not recommend using a separate vimrc file, for the simple reason > that it's so easy to forget. Something like this autocmd: au BufRead * if &cryptmethod != "" | setlocal nobackup noundofile ... | endif should do the magic for you, and it's impossible to forget.

    However I've got no idea how this magic works yet.


Have you got any idea?

The perfect solution would be vim -private or vim +Private however:

  • I am quite sure you cannot add you own custom command line options like -private.
  • when it comes to +Private I was unable to find it online.
  • 3
    Maybe you could define a custom command inside your vimrc, like this: command! Private set history=0 nobackup secure ..., and then call it from the shell with vim +'Private'. Concerning the last autocommand you mentioned, when a buffer is read, it tests whether the value of the 'cryptmethod' option, or 'cm' for the short version, is non empty. This option controls the encryption method used to encrypt the buffer when it's written to a file with the Ex command :X. If the option is not empty, then the autocommand sets up various options. – saginaw Jan 20 '16 at 2:39
  • 5
    I vote for vim -Nu .vimrcprivate. – romainl Jan 20 '16 at 6:08
  • 3
    How about a combination of 4 and 1,2 or 3? alias vimprivate='vim -u .vimrcprivate' etc. – Steve Jan 20 '16 at 16:04
  • 7
    Another method is to symlink vim to another name such as vimp, then test v:progname == "vimp" and if true, execute your private-mode settings. See :help v:progname. – garyjohn Jan 20 '16 at 17:00
  • @garyjohn. Your solution is really nice! Works like a charm :) – Mateusz Piotrowski Jan 20 '16 at 18:54
10

Well, a bit belated but to respond to:

  1. Some magic (from a comment by Carpetsmoker in this thread)

My general idea was that you always want to start "private mode" if you're using an encrypted file, so this autocommand will do that for you:

au BufRead * if &key!= ""
    \ | setlocal history=0 nobackup nomodeline noshelltemp noswapfile noundofile nowritebackup secure viminfo=""
    \ | endif

Basically, every time a file is read from disk it checks if the key setting is set (this is where your passphrased is stored), and if it is, it sets you settings. See :help autocommand for a general overview on how autocommands work.

The advantage to this is that can't forget to use it.

Of course, it requires that the file is encrypted. If it isn't, nothing is done. But not all is lost, we can use a filename to indicate a file is private, for example:

au BufRead *.private setlocal ...

Would mark all files ending with .private as such.

If you want to randomly open files in "private" mode, then I would recommend using a simple shell alias, e.g.:

alias vimprivate='vim -c "source ~/.vim/private"

And ~/.vim/private would contain:

setlocal history=0 nobackup nomodeline noshelltemp noswapfile noundofile nowritebackup secure viminfo=""

Why not -u? Well, this way you have your own settings and the "private" settings. You can also combine the two with:

au BufRead * if &key!= "" | source ~/.vim/private | endif
1

According to Vim documentation (:help -c) there can only be a maximum of 10 +{command}, -c {command} and -S {file} option arguments combined when starting Vim, and they execute _after_ the first file is read.

The --cmd {command} option allows an additional 10 commands to be executed before processing any vimrc file, but bear in mind that any settings can be overridden by vimrc.

Therefore I prefer to set Vim options early, and avoid any need for environment variable or scripting logic. Also, plugins may be doing dodgy things so it is better to not load any.

The most secure invocation method I can think of is using aliases in ~/.bashrc with a simple and to-the-point vimrcprivate file:

# Secure Vim
alias vimsec="vim -N -n -i NONE -u ~/.vimrcprivate"
alias gvimsec="g$(alias vimsec | sed -e "s/-u/-U/" -e "s/\(.*\)'\(.*\)'/\2/")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.