Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
:edit foo.txtYou can use any file name instead of "foo.txt". Vim will close the current file and open the new one. If the current file has unsaved changes, however, Vim displays an error message and does not open the new file:
:help E37At this point, you have a number of alternatives. You can write the file using this command:
:writeOr you can force Vim to discard your changes and edit the new file, using the force (!) character:
:edit! foo.txtIf you want to edit another file, but not write the changes in the current file yet, you can make it hidden:
:hide edit foo.txtThe text with changes is still there, but you can't see it. This is further explained in section 22.4: The buffer list.
vim one.c two.c three.cThis command starts Vim and tells it that you will be editing three files. Vim displays just the first file. After you have done your thing in this file, to edit the next file you use this command:
:nextIf you have unsaved changes in the current file, you will get an error message and the ":next" will not work. This is the same problem as with ":edit" mentioned in the previous section. To abandon the changes:
:next!But mostly you want to save the changes and move on to the next file. There is a special command for this:
:wnextThis does the same as using two separate commands:
:write :nextWHERE AM I?
:argsThis is short for "arguments". The output might look like this:
:previousThis is just like the ":next" command, except that it moves in the other direction. Again, there is a shortcut command for when you want to write the file first:
:wpreviousTo move to the very last file in the list:
:lastAnd to move back to the first one again:
:firstThere is no ":wlast" or ":wfirst" command though!
:2next
:set autowriteWhen you are editing a file which you may not want to write, switch it off again:
:set noautowrite
:args five.c six.c seven.hOr use a wildcard, like it's used in the shell:
:args *.txtVim will take you to the first file in the list. Again, if the current file has changes, you can either write the file first, or use ":args!" (with ! added) to abandon the changes.
CTRL-^
(on English-US keyboards the ^
is above the 6 key). Example::args one.c two.c three.cYou are now in one.c.
:nextNow you are in two.c. Now use
CTRL-^
to go back to one.c. Another CTRL-^
and
you are back in two.c. Another CTRL-^
and you are in one.c again. If you now
do::nextYou are in three.c. Notice that the
CTRL-^
command does not change the idea
of where you are in the list of files. Only commands like ":next" and
":previous" do that.CTRL-^
will not work, since there isn't a previous file.`"This takes you to the position where the cursor was when you left the file. Another mark that is remembered is the position where you made the last change:
`.Suppose you are editing the file "one.txt". Somewhere halfway through the file you use "x" to delete a character. Then you go to the last line with "G" and write the file with ":w". You edit several other files, and then use ":edit one.txt" to come back to "one.txt". If you now use" Vim jumps to the last line of the file. Using. takes you to the position where you deleted the character. Even when you move around in the file
" and
. will take you
to the remembered position. At least until you make another change or leave
the file.50%mFNow edit the file "bar.txt" and place the B mark (B for bar) at its last line:
GmBNow you can use the "'F" command to jump back to halfway of foo.txt. Or edit yet another file, type "'B" and you jump to the end of bar.txt.
:marks MYou can also give several arguments:
:marks MCPDon't forget that you can use
CTRL-O
and CTRL-I
to jump to older and newer
positions without placing marks there.:set backupThe name of the backup file is the original file with a ~ added to the end. If your file is named data.txt, for example, the backup file name is data.txt~. If you do not like the fact that the backup files end with ~, you can change the extension:
:set backupext=.bakThis will use data.txt.bak instead of data.txt~. Another option that matters here is 'backupdir'. It specifies where the backup file is written. The default, to write the backup in the same directory as the original file, will mostly be the right thing.
:set patchmode=.origWhen you now edit the file data.txt for the first time, make changes and write the file, Vim will keep a copy of the unchanged file under the name "data.txt.orig". If you make further changes to the file, Vim will notice that "data.txt.orig" already exists and leave it alone. Further backup files will then be called "data.txt~" (or whatever you specified with 'backupext'). If you leave 'patchmode' empty (that is the default), the original file will not be kept.
:edit thisfile /This vjjjj$yNow edit the file you want to put the text in. Move the cursor to the character where you want the text to appear after. Use "p" to put the text there.
:edit otherfile /There pOf course you can use many other commands to yank the text. For example, to select whole lines start Visual mode with "V". Or use
CTRL-V
to select a
rectangular block. Or use "yy" to yank a single line, "yaw" to yank-a-word,
etc.
The "p" command puts the text after the cursor. Use "P" to put the text
before the cursor. Notice that Vim remembers if you yanked a whole line or a
block, and puts it back that way."fyasThe "yas" command yanks a sentence like before. It's the "f that tells Vim the text should be placed in the f register. This must come just before the yank command. Now yank three whole lines to the l register (l for line):
"l3yyThe count could be before the "l just as well. To yank a block of text to the b (for block) register:
CTRL-Vjjww"byNotice that the register specification "b is just before the "y" command. This is required. If you would have put it before the "w" command, it would not have worked. Now you have three pieces of text in the f, l and b registers. Edit another file, move around and place the text where you want it:
"fpAgain, the register specification "f comes before the "p" command. You can put the registers in any order. And the text stays in the register until you yank something else into it. Thus you can put it as many times as you like.
"wdawAgain, the register specification comes before the delete command "d".
:write >> logfileThis 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.
vim -R fileOn Unix this command should do the same thing:
view fileYou 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:
vim -M fileNow 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:
:set modifiable :set write
:edit copy.cYou 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.cVim 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.cVim 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: