Quitting a File Without Saving Changes

When you just want to read a file without making changes, the "vi" command comes with a little bit of inherent risk, especially for those new to the editor.

For example, while I'm viewing this file in "Command" mode (having not pressed the "i" key to enter "Insert" mode) if I accidentally press the "d" key twice, the line that my cursor is on is deleted. This is actually a very useful command, but if you didn't know it existed, you might be real confused about what just happened. This is just one example of ways you can manipulate the document while in "Command" mode.

To quit the file without saving my changes, typing ":q" and pressing Enter won't work. I'll get the warning about unsaved changes. To really quit without saving changes, I'll follow their suggestion and add the "!" to the end of the command. So, the command I'll use is ":q!" and then press Enter.

Now, I'm back out of the file, without saving any changes.

Safely Reading Existing Text Files with the less Command

The safer way to view a file without making changes is to use the "less" command. less is a terminal program used to view, but not change, the contents of a file. I'll show you the very basics of using less, but if you want to learn more, Wikipedia has a good article on less.

To view the Test.txt file, I'll simply type "less Test.txt" and press Enter.

When I do that, the contents of the file are displayed in the window and the line that says "Test.txt (END)" just lets us know that we've reached the end of the file.

When we're done viewing the file, we simply type "q" and we're back to our file structure.