9 ways to Close or Exit Vim Editor in Linux Command Line?

Hello guys, today, I am going to talk about Vim or VI editor, one of the most important tools for people working in Linux. You might not know but many developers struggle to get out of commands like Vim and telnet. I can say that becuase there was a time when I spend a good amount of time coming out from the telnet window. I tried everything from quit, exit, to Ctrl+C without any luck but a google search saved me. I documented my experience here and today I am going to talk about another common problem, how to come out from the Vim editor, or how to close the VI editor in Linux.

For those, who work in VI on daily basis, this is a trivial task but those who happen to open VI once in a while, getting out of Vim Editor can be a nightmare given different modes of VI editor. For example, even if you know the right command but if you are in edit mode it will not work and that will seed doubts in your mind.

So, what is the command to Exit Vim?

Well, its wq for saving and quit and q! for quit without saving. There is a nice trick to remember it as well.

wq = write (to disk) and quit
q! = quit without writing (to disk) 

Now that you know to come out from Vi editor in Linux, let's go in a little bit more detail to find out how to exit Vim editor when you are in edit mode. Btw, if you are curious to learn more about Vim Editor, then you should definitely check out the Vim Masterclass course on Udemy. It's packed with awesome information about this magical, powerful VI editor.

9 ways to Close Vim Editor in Linux Command Line

Here are some of the common and uncommon ways of closing Vim editor in Linux, you don't need to learn all unless you want to become the Vim Yoda or master of Vim editor but just knowing that there are many ways to exit Vim will help you to come out when you stuck.

1. How to exit Vim from edit mode

If you are in edit mode, first press the <Esc> key. This will bring you out from the edit mode

Then enter :wq + <Enter> to save and exit.

It's rare that you would ever want to, but to exit without saving, you can just run

:q! + <Enter>

2. How to exit VIM from command Mode

Now that you know how to exit Vim editor from edit mode and in general, let's learn some rare tricks to become the Linux and VIM master. Yes, there are many more commands to close the Vim editor:

Given you're in command mode:

:wqa will write, quit all buffers (in case you have more than one)

:x will also save and quit

:ex as above

ZZ will save and quit

ZQ will just quit

:1,5wq will save only lines 1 to 5 and quit

Btw, If you have trouble understanding any of these commands, I suggest you go through Smash into Vim on Pluralsight to learn VIM from scratch, its an awesome resource.

How to exit Vim Editor in Linux Command Line?

Any other way to Close VI Editor in Linux?

Yes, there's even more. A lot more. Interested? :h quit will take you to the Vim help section on quitting.

And, if you are feeling more powerful, To exit without saving, I suggest

:!kill -9 $PPID

To remember when to use :wq and :q! without getting confused, I always say it out loud.

wq = write (to disk) and quit
q! = quit without writing (to disk) 

I  remember, :wq as "write and quit" and:q! As "quit, without saving".

And, if you thinking that you are alone in this list of programmers and developers who stuck on VIM and not able to close the terminal, think again, there are a lot more as seen in this tweet:

9 ways to Close or Exit Vim Editor in Linux Command Line?

That's all about how to close the VIM editor in Linux. In this article, you have learned how to quit vim editor from both edit and command mode. All you need to remember is wq for write(save) and quit and q! means just quit without saving. It can be easy if you work for a Vim editor for a week or so. Then you will not forget for the whole of your life. Now, you don't need to Google to quit from Vim editor anymore.

Further Learning
Vim MasterClass
Smash into Vim on Pluralsight
Linux Administration Bootcamp

Other Linux Command Tutorials You may like:
  • 10 examples of date command in Linux (examples)
  • 5 examples of sort command in Linux (examples)
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of chmod command in UNIX (examples)
  • 10 examples of curl command in Linux (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 10 examples of tar command in UNIX (examples)
  • 10 examples of Vim in UNIX (examples)
  • 5 examples of kill command in Linux (examples)
  • 10 Books every Linux Power user should read (books)
  • 10 examples of lsof command in Linux (examples)
  • 10 simple tips which save 50% of my time in Linux (tips)
  • 10 examples of the xargs command in Linux (examples)
  • 10 examples of cut command in Linux (examples)
  • 5 Courses to Learn Shell Scripting in Linux (courses)

Thanks for reading this article so far. If you like this Linux command tips then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you want to learn Linux from scratch and looking for some free resources, then you can also check out this list of free Linux courses for Programmers and Developers. It contains some of the free online courses from Udemy, Pluralsight, and Coursera to learn Linux online.

No comments:

Post a Comment