How to close vim from the command line?

258,033

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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:
258,033

Related videos on Youtube

Letseatlunch
Author by

Letseatlunch

Updated on September 17, 2022

Comments

  • Letseatlunch
    Letseatlunch over 1 year

    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.

  • jondavidjohn
    jondavidjohn about 13 years
    I would actually contend that nano is a much more universal self-explanatory console text editor.
  • RusGraf
    RusGraf about 13 years
    @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.
  • belacqua
    belacqua about 13 years
    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.
  • RusGraf
    RusGraf about 13 years
    @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.
  • RusGraf
    RusGraf about 13 years
    ... 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.
  • RusGraf
    RusGraf about 13 years
    ... 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.
  • RusGraf
    RusGraf about 13 years
    @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.
  • Ward Muylaert
    Ward Muylaert about 13 years
    and :x is a shortcut for :wq
  • djeikyb
    djeikyb about 13 years
    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.
  • belacqua
    belacqua about 13 years
    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....
  • Benoit
    Benoit about 13 years
    You just forgot the “normal” mode. wow. Actually the mode() function in vim can return 18 distinct values.
  • RusGraf
    RusGraf about 13 years
    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.
  • djeikyb
    djeikyb about 13 years
    Haha, no worries, twas interesting to read, and it's not like you were attacking me.
  • intuited
    intuited about 13 years
    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).
  • Michael Martin-Smucker
    Michael Martin-Smucker about 12 years
    @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.
  • Shridutt Kothari
    Shridutt Kothari almost 9 years
    Thanks for the information, it should also be shown somewhere in VIM so one can know... its so ridiculous to search it on internet.
  • MannyC
    MannyC almost 4 years
    @WardMuylaert Not exactly, :x only writes if the buffer has been modified, while :wq writes always,