Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

6 Answers 6

up vote 27 down vote accepted

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
share|improve this answer
    
and :x is a shortcut for :wq –  Ward Muylaert Feb 2 '11 at 8:24
1  
You just forgot the “normal” mode. wow. Actually the mode() function in vim can return 18 distinct values. –  Benoit Feb 2 '11 at 9: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 hit esc, 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 4 '11 at 7: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. –  michaelms Feb 9 '12 at 19:59

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.

share|improve this answer

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).

share|improve this answer
2  
I would actually contend that nano is a much more universal self-explanatory console text editor. –  jondavidjohn Feb 2 '11 at 5: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 2 '11 at 5: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 2 '11 at 7:02

Alongside jondavidjohn's answer, here are two links that have indispensable information about using vim.

  1. 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

  2. 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.

    http://www.vi-improved.org/tutorial.php

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.

share|improve this answer
    
@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 2 '11 at 8: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 2 '11 at 8: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 2 '11 at 8: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 2 '11 at 8: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 2 '11 at 8:53

Want exit without save:

  1. Hit ESC
  2. Hit ZQ

Exit with save:

  1. Hit ESC
  2. Hit ZZ
share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.