Home Explore Blog CI



neovim

6th chunk of `runtime/doc/usr_07.txt`
62e6a535b8db74b856013e47e51052a12488def575420bb90000000100000ca1
 collecting lines of text into one file, you can use this command: >

	:write >> logfile

This will write the text of the current file to the end of "logfile".  Thus it
is appended.  This avoids that you have to copy the lines, edit the log file
and put them there.  Thus you save two steps.  But you can only append to the
end of a file.
   To append only a few lines, select them in Visual mode before typing
":write".  In chapter 10 you will learn other ways to select a range of lines.

==============================================================================
*07.6*	Viewing a file

Sometimes you only want to see what a file contains, without the intention to
ever write it back.  There is the risk that you type ":w" without thinking and
overwrite the original file anyway.  To avoid this, edit the file read-only.
   To start Vim in readonly mode, use this command: >

	vim -R file

On Unix this command should do the same thing: >

	view file

You are now editing "file" in read-only mode.  When you try using ":w" you
will get an error message and the file won't be written.
   When you try to make a change to the file Vim will give you a warning:

	W10: Warning: Changing a readonly file ~

The change will be done though.  This allows for formatting the file, for
example, to be able to read it easily.
   If you make changes to a file and forgot that it was read-only, you can
still write it.  Add the ! to the write command to force writing.

If you really want to forbid making changes in a file, do this: >

	vim -M file

Now every attempt to change the text will fail.  The help files are like this,
for example.  If you try to make a change you get this error message:

	E21: Cannot make changes, 'modifiable' is off ~

You could use the -M argument to setup Vim to work in a viewer mode.  This is
only voluntary though, since these commands will remove the protection: >

	:set modifiable
	:set write

==============================================================================
*07.7*	Changing the file name

A clever way to start editing a new file is by using an existing file that
contains most of what you need.  For example, you start writing a new program
to move a file.  You know that you already have a program that copies a file,
thus you start with: >

	:edit copy.c

You can delete the stuff you don't need.  Now you need to save the file under
a new name.  The ":saveas" command can be used for this: >

	:saveas move.c

Vim will write the file under the given name, and edit that file.  Thus the
next time you do ":write", it will write "move.c".  "copy.c" remains
unmodified.
   When you want to change the name of the file you are editing, but don't
want to write the file, you can use this command: >

	:file move.c

Vim will mark the file as "not edited".  This means that Vim knows this is not
the file you started editing.  When you try to write the file, you might get
this message:

	E13: File exists (use ! to override) ~

This protects you from accidentally overwriting another file.

==============================================================================

Next chapter: |usr_08.txt|  Splitting windows

Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl:

Title: Appending to Files, Viewing Files Read-Only, and Changing File Names in Vim
Summary
This section covers three additional file-related tasks in Vim. It explains how to append the contents of the current file to another file using `:write >> logfile`. It describes opening a file in read-only mode using `vim -R file` or `view file` to prevent accidental modifications and how to override this. It also covers using `vim -M file` to completely disallow modifications. Finally, it details how to use `:saveas move.c` to save a copy of the current file under a new name and switch to editing the new file, and how to change the file name associated with the current buffer using `:file move.c` without saving the file, preventing accidental overwrites.