How to Exit a File in Vi / Vim Editor in Linux

In this article, we will learn how to exit Vi/Vim (after referred to as Vim) text editor using simple commands. In a previous article, we explained a simple tip on how to save a file in Vi or Vim after making changes to a file.

Before we move any further, if you are new to Vim, then we recommend reading through these 10 reasons why you should stick to using Vi/Vim text editor in Linux.

To open or create a new file using Vi/Vim, simply type the commands below, then press i to switch to insert mode (insert text):

$ vim file.txt
OR
$ vi file.txt
Press 'i' to Insert Mode in Vim Editor
Press ‘i’ to Insert Mode in Vim Editor

After making changes to a file, press [Esc] to shift to the command mode and press :w and hit [Enter] to save a file.

Save File in Vim
Save File in Vim

To exit Vi/Vim, use the :q command and hit [Enter].

Exit File in Vi Editor
Exit File in Vi Editor

To save a file and exit Vi/Vim simultaneously, use the :wq command and hit [Enter] or :x command.

Save and Exit File in Vi
Save and Exit File in Vi

If you make changes to a file but try to quite Vi/Vim using ESC and q key, you’ll receive an error as shown in the scrrenshot below.

Vi Error Quitting File
Vi Error Quitting File

To force this action, use ESC and :q!.

Force Quit Vi File in Linux
Force Quit Vi File in Linux

Additionally, you can use shortcut methods. Press the [Esc] key and type Shift + Z Z to save and exit or type Shift+ Z Q to exit without saving the changes made to the file.

Having learned the above commands, you can now proceed to learn advanced Vim commands from the links provided below:

  1. Learn Useful ‘Vi/Vim’ Editor Tips and Tricks to Enhance Your Skills
  2. 8 Interesting ‘Vi/Vim’ Editor Tips and Tricks for Every Linux Administrator

In this article, we learned how to exit Vim text editor using simple commands. Do you have any questions to ask or any thoughts to share? Please, use the feedback form below.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

2 Comments

Leave a Reply
    • @Micheal

      Many thanks for the useful suggestion, it works really quick. It’s mentioned some where in the article, by the way.

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.