How do I open a file in a read-only split window in Vim?
Answer
:sview {file}
Explanation
The :sview command opens a file in a horizontal split window with the buffer set to read-only. It combines :split and :view into a single command, giving you a side-by-side reference view of another file without any risk of accidentally editing it. This is especially useful when you need to consult a config file, reference implementation, or documentation file while working in the main window.
How it works
:view {file}— opens a file in the current window as read-only ('readonly'is set):sview {file}— same, but first creates a new horizontal split- In a read-only buffer, attempting to write warns:
E45: 'readonly' option is set - You can still copy text, navigate, and search freely
- To open in a vertical split instead:
:vert sview {file}
Example
:sview ~/.ssh/config
This splits the window and shows ~/.ssh/config read-only above (or below) your current file:
+---------------------------+
| ~/.ssh/config [RO] |
| Host dev-server |
| HostName 10.0.0.1 |
+---------------------------+
| current file.go |
| ... |
+---------------------------+
Tips
- Use tab completion:
:sview <Tab>works just like:e <Tab> - To also open in a vertical split:
:vertical sview {file}or:vert sview {file} - Close the split with
:qor<C-w>q - To force-write a read-only buffer if needed:
:w!(or:set noreadonlyfirst) - The related command
:peditopens a file in the preview window (:pcloseto close)