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.
-
1superuser.com/questions/16794/good-vim-tutorial– Jorge CastroFeb 2, 2011 at 4:01
-
1twitter.com/iamdevloper/status/435555976687923200 :D– anishsaneMar 27, 2015 at 9: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<cr> :x<cr> and ZZ when exiting vim?– Eliah KaganOct 7, 2017 at 17:22
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
-
4
-
2You just forgot the “normal” mode. wow. Actually the
mode()
function in vim can return 18 distinct values.– BenoitFeb 2, 2011 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 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).– intuitedApr 4, 2011 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. Feb 9, 2012 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. Jul 3, 2015 at 15:21
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.
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).
-
4I would actually contend that
nano
is a much more universal self-explanatory console text editor. Feb 2, 2011 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ükFeb 2, 2011 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.– belacquaFeb 2, 2011 at 7: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.
-
@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ükFeb 2, 2011 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ükFeb 2, 2011 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ükFeb 2, 2011 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ükFeb 2, 2011 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.– djeikybFeb 2, 2011 at 8:53
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)