How do I edit files on the command line?

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Mon, 2006-08-07 17:15. :: Shell | Other

To edit files on the command line, you can use an editor such as vi. To open the file, run

vi /path/to/file

Now you see the contents of the file (if there is any. Please note that the file is created if it does not exist yet.).

The most important commands in vi are these:

Press i to enter the Insert mode. Now you can type in your text.

To leave the Insert mode press ESC.

To delete the character that is currently under the cursor you must press x (and you must not be in Insert mode because if you are you will insert the character x instead of deleting the character under the cursor). So if you have just opened the file with vi, you can immediately use x to delete characters. If you are in Insert mode you have to leave it first with ESC.

If you have made changes and want to save the file, press :x (again you must not be in Insert mode. If you are, press ESC to leave it).

If you haven't made any changes, press :q to leave the file (but you must not be in Insert mode).

If you have made changes, but want to leave the file without saving the changes, press :q! (but you must not be in Insert mode).

Please note that during all these operations you can use your keyboard's arrow keys to navigate the cursor through the text.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Mon, 2014-01-13 22:24.

It worked! Very precise explanation. Thank you. :)