1

I want to open file with modifiable buffer set noma ON by default for this specific file.

for example something like this:

 vi file1.txt noma
 vi file2.txt ma   , # default

OR at least inside vim:

:tabnew file1.txt noma

Thanks

M.Ed
  • 13
  • 2
  • I don't have an answer for a specific file but I found this answer that allow to set specific parameters depending on the file type. https://stackoverflow.com/questions/158968/changing-vim-indentation-behavior-by-file-type – Gowachin Dec 14 '20 at 12:10
  • afaik, if i update my vimrc, it will be global for all files I open using vim, which i don't want to happen. – M.Ed Dec 14 '20 at 12:10
  • Consider using the [vi.se] Stack Exchange for your future questions on Vim. – filbranden Dec 14 '20 at 16:10

2 Answers2

0

You can add that to your .vimrc, which will set the flag if the file has that name:

augroup ReadOnly
       autocmd!
       autocmd BufReadPost file1.txt set noma
augroup END

That will set it both if the file exists (BufReadPost).

Edit: The view command allows you to open a file in readonly mode.

For reference: :help autocmd-groups and :help autocmd-define

padawin
  • 4,230
  • 15
  • 19
  • Thanks for the answer, however does it mean I have to pre-define first the name of the readonly files inside the augroup? isn't there a genric way to do it? – M.Ed Dec 14 '20 at 12:58
  • you can use a wildcard (e.g. *.ini). But your question seems to say that you have a specific file (therefore with a specific name) that you want to always open non-modifiable. Isn't it the case? – padawin Dec 14 '20 at 13:07
  • I meant not to applied to all files and at same time not pre-defined, in other meaning, when i am opening a file for preview only w/o editing, i have to manually "set noma" inside it which is risky a little bit, so what I am looking for is an option to open file readonly from the terminal if needed, given a generic file name and generic extension. – M.Ed Dec 14 '20 at 13:44
  • Also you can use the command `view`, which opens a file with Vim, but in readonly mode. – padawin Dec 14 '20 at 13:57
  • `view` command is very useful, that is what I was looking for, Thanks a lot @padawin – M.Ed Dec 14 '20 at 14:21
  • Edited the answer so that you can accept it. – padawin Dec 14 '20 at 14:25
0

You can use the -R command-line option to open a file in 'readonly' mode (which includes 'nomodifiable' and also other options that are useful when viewing files.)

$ vim -R file1.txt

The view command is usually available as a shortcut to vim -R, so this is often also possible:

$ view file1.txt

If you already have a Vim running and want to use it to open a file in read-only mode in a new tab, you can use this sequence:

:tab sview file1.txt

The :view command opens a file in read-only mode, the :sview command does so in a split and the :tab modifier has Vim do so in a new tab instead.

filbranden
  • 8,522
  • 2
  • 16
  • 32