I know this is more of a general linux question but w/e. So when I enter a program like vim in the command prompt it displays all the text in the file and I can edit it etc. But I can't figure out how to close or save the file and get back to the command prompt without killing the process. Any help is appreciated.
In vim there are 3 different modes:
- Insert - allows typing and editing as normal
- Visual - used for selecting copy/paste etc.
- Normal - used for commands
To get back to Normal mode, you can always press esc.
Once you are at Normal mode Press
: to begin your command (you'll see it appear in the bottom left). The following commands are related to quiting vim:
:q- quit if no changes were made
:q!- quit and destroy any changes made
:wq- write changes (save) and quit
:x- similar to
:wq, only write the file if changes were made, then quit
First, hit the escape key.1
Then just type
ZZ (that's two capital Z's in a row).
Either will save any edits and leave.
You can also use
Alternately, you can type
:q (a.k.a, "quit, please") This will exit only if you haven't made edits.
If you've made edits, and you want to discard them and leave, type
:q! (a.k.a "quit, damn it!")
1 : This ensures you are in "command" mode. Which you want for typing commands, like those needed to exit.
Much to our inconvenience, there is no general method for exiting command-line programs like there is the "X" button for graphical programs.
Many command-line programs follow the theme of using either Q (e.g.
top) or Ctrl+C (e.g.
watch) to exit, but this varies considerably, especially among text editors:
- Vim in particular uses the obscure combination of
:q!then Enter, usually preceded by several presses of Esc for good measure.
- Emacs, another gem, prefers Ctrl+X followed by Ctrl+C.
Editors like this are traps for the inexperienced. My personal preference and recommendation is to, when forced to edit text on the command-line, use instead the more self-explanatory Joe's Own Editor (JOE).
Alongside jondavidjohn's answer, here are two links that have indispensable information about using vim.
This is a keyboard graphic that shows you what each key does depending on if you're in edit mode, command mode, or visual mode:
This is the best vim tutorial I've ever worked through. It's conversational and easy to understand, which is due to it's IRC/instant-messaging format.
Finally, there are some like aendruk who simply don't want to use advanced command line text editors. Vim in particular has a steep learning curve, and does actually take some initial effort to get used to. But it's very fast, and very powerful. If you have interest in linux beyond basic desktop usage, it's worth investing time learning a decent command line text editor like Vim or Emacs. If you just need to edit some text and don't care much beyond that, try typing
gedit filename.txt instead. It will launch a familiar graphical program, much like Notepad from Windows.