2

I'm using vim with some plugins, such as Nerdtree, YCM, to build an coding environment.

Now I want to achieve this:

When I execute the command vim, a file containing some ASCII image, will be loaded automatically.

Well, this is only for good looking.

What I've done is to add a line in the file ~/.bashrc: alias vim=vim -o ~/.vim/ascii_img.

It works but I want to make the buffer of ascii_img read only to prevent the file ascii_img getting changed accidently.

Then I changed the line above in ~/.bashrc: alias vim=vim -ro ~/.vim/ascii_img.

However, now all of files become read only.

Is it possible to make only the specific buffer read only?

2 Answers 2

3

An alternate method is to modify your alias (I prefer functions, so I’ll show both):

# alias
alias vim='vim +"setlocal readonly" ascii'

# function
vim () {
  command vim +'setlocal readonly' ascii "$@"
}

The issue here is I’m not sure if vim is ok with options/flags that come after the first file argument.

1

You will need to add to your vimrc an autocommand which will be executed every time you read the file and will be used to set the 'readonly' option on the file:

augroup readonly
    autocmd!
    autocmd! BufReadPost ascii_img setlocal readonly
augroup END

See :h autocmd and :h 'readonly'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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