How to edit a file ?
Edit a file is crucial to manage a server.
There are plenty of text editors. The default is vi, and it is amazing. You should learn how to use it. If you're already used to emacs, use "mg" command instead.vi(1)
Hare are some advices to start with vi.
To edit a file /etc/iloverocknroll, you'll enter:
You will see its content:
Most of the time, you'll do this :
- 1. Press "i" to enter "insert" mode and write.
- 2. Press "Esc" to exit insert mode.
- 3. Enter ":wq" then Return to write the file and quit.
Still here ? 😎
Read the next lines if you want to go a little further.
There a three modes :
- Visualize : you can mode in the file using h/j/k/l keys.
- Insert mode : you can enter text. Press "i" to enter insert mode, "esc" to leave it.
- Edit mode : You can enter commands after pressing ":" to replace patterns as example.
To save your changes, press ":" then enter "w" and "Return". You can exit vi using ":q". To do both at the same time, enter ":wq".
To cancel changes, enter "u". To cancel again, press "." : it repeat the last action.
To search in the file, enter "/" the write what you're looking for.
If you want to discard your changes, enter ":q!"
Other tips :
- cw : change one word
- c$ : change from the cursor to the end of line
- 3G : go to line numer 3
- ma : set under the cursor a make tagged "a". To quickly go back to this mark, enter "'a".
- dd : delete the line
- yy : copy the line in the buffer
- p : write the previously deleted or copied line
- d'a : delete between cursor and mark a
- y'a : copy from cursor to mark a.