How to Exit a File in Terminal (Bash/Zsh)

If you are new to editing files with Terminal programs such as VI, VIM, or Nano, then it could be difficult at first to understand how to exit a file that is being opened in edit mode.

Let us take a look at how to exit a file based on each editor type.


Table of Contents


Note: It should not matter what kind of shell you are using bash/zsh, but will matter on which operating system you are on Ubuntu Linux, macOS or Windows.


Exit a file using Nano Editor

    The Nano editor is the most easy to use editor for the Terminal. If you have opened a file using nano command, then you can exit by pressing the Keys "Control + X"

    If you have made no changes to the file, the application will exit and return to your bash/zsh shell prompt.

    Press Control + X to Exit File in Nano Editor

    If you have made changes to the file, then when you press Control + X you will be asked a question,

    Saving File Changes Nano Editor

    Save modified buffer?

    Y Yes
    N No
    ^C Cancel

    • If you want to save changes and exit then press Control + Y

    • If you do not want to save changes and exit then press Control + N

    • If you do not want to exit the file and continue editing then press Control + C


Exit File using Vi/Vim

    Vi/Vim is a more powerful and advanced text editor, it could be a bit difficult at first how to exit its file editing modes as compared to Nano.

    • Step 1: Press Escape key to get into the command mode.
    • Step 2:
      • If you want to save and exit, then type :wq (write and quit) and press Enter.
        Vi Vim Save and Quit File Command
      • If you do not want to save changes and exit, then type :q! and press Enter.
        Vi Vim Quit File without Saving

    You should return back to the terminal prompt.


Exit File when using less or more commands

    If you are less or more command to view the file contents in the terminal, then simply press Q to return back to the Terminal prompt.

    Comments & Discussion

    Facing issues? Have questions? Post them here! We're happy to help!