Editing files on Emacs
Emacs is a text editor. So its main function is to edit text. You can open a file with C-x C-f
(find-file) and type the name of the file you want to open, for example:
C-x C-f myfile
That will create a buffer1 which your file’s contents.
There is another ways to visit a file in Emacs. for example:
(file-find-read-only): This will open a file but won’t allow you to make changes on it.
C-x 4 f
(find-file-other-window): This splits your Emacs window vertically and puts the selected file you gave on the buffer bellow
C-x 5 f
(find-file-other-frame): This opens another Emacs window2 and puts the selected file on a new buffer.
Opening remote files
Emacs comes with TRAMP which can be used to open remote files. To open a remote file. Press C-x C-f
(find-file) and follow this syntax:
/method:host:filename /method:user@host:filename /method:user@host#port:filename
If you don’t gave a method (put - as method) Emacs do the following:
hoststarts with ftp, Emacs will use ftp
useris “ftp” or “anonymous” Emacs will use ftp.
- If the variable
tramp-default-methodis ftp, Emacs will use ftp.
- If ssh-agent is running, Emacs will use ftp.
- Otherwise, Emacs will use ssh.
Try opening this remote file:
To do this, press C-x C-f
(find-file) and type this /ftp:firstname.lastname@example.org:/readme.txt
Emacs will ask for a password. This password is
This is a read only ftp server, but if it allowed write, you could write the remote file with C-x C-s
To add text to a buffer, you just type what you want to add on the buffer, unlike Vi/Vim Emacs do not have an “INSERT” mode.
When you finish typing and want to save what you have done press C-x C-s
Reverting edits on Emacs
Emacs, like every other text editor, allows to undo what you’ve done.
If you have done massive changes to a file and want to revert it, type
M-x revert-buffer Emacs will ask confirmation. Since this reverts to the saved version of the buffer. If you saved the file after some changes, this method will not work. You can undo your last change to the file with some shorcuts: C-x u, C-/ and C-_