DEV Community

Amin
Amin

Posted on

Open, write & close files — Neovim From Scratch

This episode is part of a serie called Neovim From Scratch where I show you the basics of how to use Neovim (and Vim) when editing text.

We can open files using the nvim command in a terminal (may vary depending on your operating system) like so.

$ nvim
Enter fullscreen mode Exit fullscreen mode

Once you entered Neovim, you can quit out of it by pressing the keybind :quit.

This tells Neovim to issue the command quit which is denoted by the colon that prepends this command. The colon is a special keybind that, when in normal mode, will tell Neovim that the next characters are related to a command you wish to execute.

There as several command, and we will see a few of them in this article.

Throughout this series, we will be using several other commands that will be useful for our text editing.

Instead of simply opening Neovim and be presented in the Neovim home screen, you can actually tell neovim to start and read one of the file that you have in your current working directory.

Let's try to open a file that already exists. I'll use the command echo and a redirection to quickly create a new file.

$ echo "Hello, world!" > file.txt
Enter fullscreen mode Exit fullscreen mode

Now that we have a file, we can open it using Neovim.

$ nvim file.txt
Enter fullscreen mode Exit fullscreen mode

By providing a file as an argument to nvim, we can open a file directly. Exciting!

Now, let's say you don't want to create the file by hand, maybe because you don't remember the syntax using echo, or you simply don't want to bother using any other command except nvim.

You can actually start Neovim as previously stated by simply running the command without argument.

$ nvim
Enter fullscreen mode Exit fullscreen mode

Now that you are presented with the loverly startup screen, you can actually run a command to create the file! Simply type the following.

:write file.txt
Enter fullscreen mode Exit fullscreen mode

That's it! If it went well, you should see the startup screen disappear, that's good news! It means that the file has first been created, then read by Neovim.

Since this file is empty by default, you don't see much.

We will learn how to manipulate text in an opened file later.

Actually, we won't be using the term file to specify an opened file in our text editor but rather a buffer.

A buffer is a file that has been opened and loaded in memory by Neovim.

This means that once you start writing some text, it won't be immediately be saved on your file system, but rather in the memory of the nvim program.

If you wish to write this file, you can use the command :write that we already saw earlier.

Now that you know how to open, create and close files, let's try to add another command that will help us open another file.

It would be great to be able to work with many files. Let's try to add some more files just for testing purposes. Again, I'll use the terminal for this task.

$ echo "Test1" > test1.txt
$ echo "Test2" > test2.txt
$ echo "Test3" > test3.txt
Enter fullscreen mode Exit fullscreen mode

Now, let's open Neovim without any arguments. We will see in a bit how we can open multiple files inside of Neovim.

$ nvim
Enter fullscreen mode Exit fullscreen mode

Now that we are presented with the startup screen, we can use the command edit command in order to open the content of a file inside of a new Neovim buffer.

:edit test1.txt
Enter fullscreen mode Exit fullscreen mode

If everything went according to the plan, you should see the text Test1 displayed on the screen. This means that we successfully opened the file test1.txt!

Now, without quiting Neovim, let's try to open a second file.

:edit test2.txt
Enter fullscreen mode Exit fullscreen mode

Again, if you managed to see the text Test2, this means that you now understand how to quickly open files as buffers inside of Neovim.

Now, let's change the assignment a little bit: let's try to open a file that does not exist. Like test4.txt.

:edit test4.txt
Enter fullscreen mode Exit fullscreen mode

If you issued the command, you should see that no errors have been thrown by the editor. This works as opening an existing file!

Again, this works because any file that you open is a buffer, it does not exist on the file system. Try quiting and listing the files now.

:quit
Enter fullscreen mode Exit fullscreen mode
$ ls
file.txt test1.txt test2.txt test3.txt
Enter fullscreen mode Exit fullscreen mode

As you can see, there is no mention of the test4.txt here.

Now, let's go back inside Neovim.

$ nvim
Enter fullscreen mode Exit fullscreen mode

Let's open again our non-existant test4.txt file.

:edit test4.txt
Enter fullscreen mode Exit fullscreen mode

Now, let's try to save this file. The file is still in our Neovim buffer, but we want to see it on our file system. For that, we will use the write command.

:write
Enter fullscreen mode Exit fullscreen mode

As you can see, compared to earlier, we didn't provide a file name for the write command. This is because our opened buffer remembers the name of the file that we tried to opened. Simply using write is sufficient for Neovim to understand that it needs to persist the file on our file system using the test4.txt name.

Now let's quit Neovim and see those files.

:quit
Enter fullscreen mode Exit fullscreen mode
$ ls
file.txt test1.txt test2.txt test3.txt test4.txt
Enter fullscreen mode Exit fullscreen mode

Success! We successfully managed to create a file inside of Neovim.

Now, let's say you don't really remember the files that are located inside of your current working directory, you can actually use the tabulation key when using the edit command.

In fact, several other commands that we will see in this serie will be able to use the tabulation key. Try it yourself!

That's it for this article. You now know how to:

  • Open Neovim
  • Close Neovim
  • Edit a file
  • Save a file that has been opened

In the next article, we'll see what is the normal command that we talked about (the mode that allowed you to issue those commands), and some basic editing keybind that will help us append text to an opened buffer, among other things.

See you!

Top comments (0)