Creating, Editing and Saving Text Files in the Command Line With VI
Terminals comes with some built-in programs that you can use to read and write text files. Some common default text editors include VI and Nano. I'll be showing you how to use VI to edit text files in this series, but if you're interested in learning about some others, This Wikipedia article has some good info.
Some Basic Commands for VI
There are a ton of commands that you can use to manipulate files in VI. I'm only going to show you the most basic commands, but if you'd like to further your knowledge, the University of Hawaii College of Engineering has a great index of VI commands.
These are the commands we'll use:
vi = Opens the VI editor to create a new text file or edit an existing text file
i = switch from command mode to insert mode
esc (the escape key) = switch from insert mode to command mode
w = write file (or save the file)
q = quit file (or close the file)
u = undo the last change
ctrl-r (hold the control key and press "r") = redo the last undone change
Let's start out by creating a new text file. The command is simple, it's just "vi <filename.extension>" So, if we want to create a file named Test.txt we type "vi Test.txt" and press Enter.
When we do that we're presented with a blank screen. In some editors, you can just start typing at this point, but VI has two modes, "Command" mode and "Insert" mode. When you're in "Command" mode you can do things like save the file or close the file. In order to enter text, we need to enter "Insert" mode.
To get into "Insert" mode, you press the "i" key. When you do that, you'll see the text "-- INSERT --" at the bottom of the window which tells you that you are in "Insert" mode. Now we can start typing.
I'll just enter "This is a test text document."
Now, we need to get out of "Insert" mode, and we do that by pressing the Escape (esc) key. When you do that the "-- INSERT --" text disappears, so you know that you've left "Insert" mode and are back in "Command" mode.
When you're ready to save and/or close the file, you need to press the ":" key (by holding shift and pressing the ; key), then enter the letter that corresponds to the action you'd like to take.
If we just type "q" to close the file, when we press Enter, we'll see a message that says "E37: No write since last change (add ! to override)". What this means is that there is unsaved (or unwritten) data. We need to write out (or save) the file before we quit.
Typing ":w" and pressing Enter will save the file, and at the bottom of the window we'll see the text ""Test.txt" [New] 1L, 30C written". This tells us that we saved the file "Test.txt", that it was a new file, contains only 1 line, and there are 30 characters in the document.
(Note: This includes a hidden character that indicates the end of a line. This character is automatically added to the end of each line, and used by text editors to know how to properly display the file contents on the screen, but is hidden from view.)
Now, when we type ":q" and press Enter, we are taken back to our file structure. If we type "ls", we'll see the new file in the list. And if you take a look in the finder, you'll see the new text file.