how to create a new file or buffer in emacs editor

If you have been using emacs as your text editor then one of the first things you probably want to know is how to create a new file or buffer in the editor. As with many of the modern editors, emacs has the concept of a buffer and a file.

A buffer is the "object" that the editor holds on to and contains the contents of a file when it was last visited. The file is the object that is saved to the file system and most times the buffer is backed by a file. In emacs, you can create a file on the file system and then open it in a buffer or choose to create a buffer and then save it to a file at a later time.

creating a buffer

Creating a buffer with out a backing file is sometimes useful for various reasons. You may not know ahead of time as to whether you will save the file and if so where you will do that. For throwaway notes, you could very well use the scratch buffer but you need multiple scratch buffers to keep everything separate.

The concept of buffer is not unique to emacs and many editors do use it when creating a new file although it might be called something else. In order to create a new buffer in emacs, you can use the switch-to-buffer command.

M-x switch-to-buffer

The default shortcut for the switch-to-buffer command is C-x b.

When you use this command the editor will prompt you for a name which will be the name of the buffer. This will help you to identify between the buffers that are open and allow you to switch between them.

Once you have edited your buffer you can save the buffer to a file using the save-buffer command. The is the same command that you use to save a named buffer to the file. However as this buffer does not have a backing file, it will prompt you for a file name on saving.

creating a file

The command find-file can be used to open an existing file or to create a new file. A new file is created if the file by the given name does not exist.

M-x find-file

The default shortcut for the find-file command is C-x C-f.

Once you have created either a buffer or a file, you will edit it just as you would in any other editor. Once you have done editing use the save-buffer (C-x C-s) command, which will save the buffer to the backing file.