88

I'm new to vi. I started vi on my Ubuntu machine and I can't quit. I see the editor and I can write text, at the bottom line there is a label "recording".

How do I quit the vi editor?

4

6 Answers 6

101

vim is a modal editor. Hit the ESC key to get into Normal (command) mode then type :q and press Enter.

To quit without saving any changes, type :q! and press Enter.

See also Getting out in Vim documentation.

5
  • 9
    Thanks: I tried that commands, but after running them I was prompted with: E37: No write since last change (add ! to override). I have nothing to save.
    – Jonas
    Oct 20, 2010 at 22:49
  • 14
    Use :wq if you want to save what you have edited and then exit. :q! if you want to exit and leave your changes behind.
    – Steven D
    Oct 20, 2010 at 22:50
  • Or if you've written something you want to make a copy of, :w newFileName.txt, then sort out the read-only issue outside of vi or vim. Oct 20, 2010 at 23:17
  • 5
    quit :q ; force quit :q! ; quit and save :wq or :x
    – fromnaboo
    Oct 6, 2012 at 10:52
  • 2
    @Jonas, next time try :qall!, I suspect you have open more files within one single buffer.
    – Alan Dong
    Oct 4, 2015 at 4:03
12

I use ctrl+[ to generate the esc sequence, this keeps me from having to move my fingers from the home row (remember the esc key was in a different place when vi was invented. :wq will write all files regardeless of necessity. I suggest using ZZ (which is shift+z twice) which will only write if a change has been made in the file. Also :xa is the same as ZZ except if you have more than 1 file open in the editor instance (such as vim tabs). note: I'm not sure all this is 100% compat with all vi clones, but I know it works with vim

2
  • 1
    Thanks. The ZZ command seems to be useful. I can see the point with Ctrl+[ but I'm using a swedish keyboard layout and I have to press AltGr+8 to get [ so that would be Ctrl+AltGr+8 for me :(
    – Jonas
    Oct 21, 2010 at 1:17
  • Also note ZQ is effectively the same as :q!. However, this is not standard. ZZ, on the other hand, is specified in POSIX.
    – Wildcard
    Nov 18, 2016 at 1:59
4

The quit from the vi is another way is Esc :x.

The option is used for save and quit at the same time.

1
  • 2
    Note that like ZZ, :x will write the file only if its content has changed, unlike :wq.
    – jlliagre
    Nov 18, 2016 at 1:55
2

As Sinan said, vim is a modal editor. If you want to know whether that works for you you should maybe invest some time and run vimtutor which is an interactive way to learn vim. (It also covers how to exit, what the modes mean and what you can do in each mode).

-2

After modification, please press ESC and the given command :wq!.

This will forcefully write the new modification on the read-only file. Earlier it was not working because the file is read-only.

2
  • 1
    Why are you answering a question from 4 years ago, with basically a rehash of the top answer? It's wrong anyway: this won't force write a read-only file- it will try to write, might fail, and then will force quit without saving.
    – Chris
    Nov 18, 2016 at 3:56
  • I tried with the previous comments and it was not working. I thought it would be useful to share what I have done. It is not important when the question was posted, but whether the solutions are applicable or not.
    – Arif A.
    Nov 21, 2016 at 15:00
-3

try combination of control and Z

1
  • 2
    This will keep vi in background;
    – Archemar
    Dec 13, 2016 at 12:37

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