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.
-
1http://superuser.com/questions/16794/good-vim-tutorial – Jorge Castro Feb 02 '11 at 04:01
-
1https://twitter.com/iamdevloper/status/435555976687923200 :D – anishsane Mar 27 '15 at 09:35
-
Related across SE: How to exit the Vim editor?, this blog post, How to exit VIM and get back to 'normal' terminal?, How do I quit from Vi?, Exit vim more quickly, this tag wiki, and What are the differences between :wq
:x – Eliah Kagan Oct 07 '17 at 17:22and ZZ when exiting vim?
6 Answers
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

- 6,602

- 854
-
4
-
2You just forgot the “normal” mode. wow. Actually the
mode()
function in vim can return 18 distinct values. – Benoit Feb 02 '11 at 09:11 -
As alluded to by Benoit, this answer confusingly uses the term "command mode" to refer to what the vim documentation calls "normal mode". The vim docs consider command mode to be what you enter by pressing
:
in normal mode. If you hitesc
, you return to normal mode. -1 for being misleading to people trying to learn the standard terminology (and to consequently be able to make sense of the help system). – intuited Apr 04 '11 at 07:10 -
6@intuited Anyone can suggest edits, which is probably going to be more helpful in the long run than a down vote and good advice that gets buried in the comments. – Michael Martin-Smucker Feb 09 '12 at 19:59
-
Thanks for the information, it should also be shown somewhere in VIM so one can know... its so ridiculous to search it on internet. – Shridutt Kothari Jul 03 '15 at 15:21
-
@WardMuylaert Not exactly,
:x
only writes if the buffer has been modified, while:wq
writes always, – MannyC May 28 '20 at 14:54
First, hit the escape key.1
Then just type ZZ
(that's two capital Z's in a row).
Or, type :x
.
Either will save any edits and leave.
You can also use :wq
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.

- 23,120
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. man
and top
) or Ctrl+C (e.g. ping
and 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).

- 76,794
-
4I would actually contend that
nano
is a much more universal self-explanatory console text editor. – jondavidjohn Feb 02 '11 at 05:24 -
@jondavidjohn Your contention is moot. I agree that nano is unquestionably more universal, and I make no claim that it is any less self-explanatory than JOE. My preference for JOE is due to its expanded feature set, and my recommendation to inexperienced users is influenced by subtleties such as the use of the term Save in place of the archaic WriteOut. – ændrük Feb 02 '11 at 05:46
-
Generally you can use ^Z to suspend the process. Then you can kill it, ignore it, put it in the background, or bring it back. (kill %1 or kill %x, bg, fg). There is some unity of job control, anyway. – belacqua Feb 02 '11 at 07:02
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:
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
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.

- 30,245
-
@djeikyb: I appreciate that you make the distinction between basic desktop usage and advanced command-line text editing. Ubuntu's strength over other Linux distributions is in its ability to make the Linux desktop accessible by everyday computer users, whom to its credit are now increasingly perimtted the unique luxury of never needing to know what editors like Vim even are. – ændrük Feb 02 '11 at 08:21
-
... It is with this special freedom in mind that I make the decision to work as high above traditional Linux command-line methods as I have the patience for during everyday computer use. I make the effort to utilize only simple and accessible tools largely because I can afford to. It would be inaccurate to say that I simply don't like command-line text editors. To the contrary, I happen to be fond of Vim with its VimOutliner plugin, and I am indebted to it for its influence on the Vimperator/Pentadactyl project. – ændrük Feb 02 '11 at 08:21
-
... However, I see my personal use of these tools as a deviation from typical computer use. I am of the opinion that it would be a healthy development for the Ubuntu community to less readily suggest to newcomers that casual use of advanced tools with steep learning curves is the norm among everyday users. – ændrük Feb 02 '11 at 08:22
-
@Letseatlunch, if your goal is to become proficient in Vim, then by all means I encourage you to continue your pursuit of it. I only mean to communicate that, while, importantly, Ubuntu offers many such advanced tools that other operating systems lack altogether, learning to use them should never be a requirement for using Ubuntu. – ændrük Feb 02 '11 at 08:22
-
Great clarification! I'll edit my answer to better reflect this. Also, I specifically recommended gedit over nano because gedit is higher level with a familiar gui, and because nano was ridiculously confusing to me at first (what does the caret symbol mean?? how do i quit?? how do i save??). Which is funny now, considering vim is my editor of choice. – djeikyb Feb 02 '11 at 08:53
-
The old-school reason to use vi was that it was installed on every system. If your terminal settings were foobar, you might have to use
ed
, but if you were going from system to system, you knew that it would be available.... – belacqua Feb 02 '11 at 09:02 -
Thanks, djeikyb, and please don't take it too personally that I dropped this giganto-comment below your answer — it's something I've been wanting to clarify anyway and this just seemed the next logical place in the flow of dialog on the page. – ændrük Feb 02 '11 at 15:26
-
Haha, no worries, twas interesting to read, and it's not like you were attacking me. – djeikyb Feb 03 '11 at 23:11
if Esc :q!
does not work,
try first Ctrl+q (to unlock the screen which was locked with Ctrl+s)
then retry Esc :q!
(to quit without saving) or Esc :wq
(to save and quit)

- 21