Exit vi linux with saving

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):

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

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

Exit File in Vi Editor

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

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

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

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:

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 You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Читайте также:  Средство устранения неполадок usb для windows 10

We are thankful for your never ending support.

Источник

How To: Vi / Vim Save And Quit The Editor Command

Tutorial details
Difficulty level Easy
Root privileges No
Requirements vi / vim
Est. reading time Less than 1 min

vi or vim (Vi IMproved) is text editor or a programmers text editor. You can edit programs or configuration files on the Linux/Unix server. Vim is upwards compatible to Vi. This page explains how to save a file in Vim or vi and quit the editor under Linux or Unix-like systems.

Vim Save And Quit The Editor Command

The procedure to save a file in vim / vi and quit the editor is as follows:

  1. Open the terminal application in Linux or Unix
  2. Next, open a file in vim / vi, type: vim filename
  3. To save a file in Vim / vi, press Esc key, type :w and hit Enter key
  4. One can save a file and quit vim / Vi by pressing Esc key, type 😡 and hit Enter key

Let us see all commands and examples in details.

How to open a file in Vim / Vi

You can start vi/vim by typing the following command:
vim file
vi file
vim [options] filename
vim helloworld.sh

Understanding vim modes

When we start vim/vi text editor, we are in normal mode. This mode allows use to use vim commands and navigate around the file as per our needs. In order to edit text, you need to enter the insert mode by pressing i key. This way, we can edit our program or configuration files such as /etc/nginx/nginx.conf.

Of course, you can go back to normal mode by pressing Esc key again.

How to save and quit the vi or vim text editor

To save and quit the vi or vim editor with saving any changes you have made:

  1. If you are currently in insert or append mode, press Esc key.
  2. Press : (colon). The cursor should reappear at the lower left corner of the screen beside a colon prompt.
  3. Enter the following command (type 😡 and press Enter key):
  • Press ENTER key
  • This will quit the editor, and all changes you have made to the document will be saved to the file.
  • Use x to save a file and exit:

    Fig.01: Vi / vim save and quit demo.

    How to trash all changes in vi/vim

    If you do not want to save any changes, first press Esc key. To exit Vim without saving changes press :q! followed by ENTER key.

    Getting more help about vim commands

    Want to learn vim text editor? Start the vim tutor by typing the following command at the Linux, macOS, and Unix-like system:
    $ vimtutor
    $ vimtutor it
    $ vimtutor fr

    vimtutor in action

    • No ads and tracking
    • In-depth guides for developers and sysadmins at Opensourceflare✨
    • Join my Patreon to support independent content creators and start reading latest guides:
      • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
      • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
      • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
      • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
      • How to protect Linux against rogue USB devices using USBGuard

    Join Patreon

    Summary of vim/vi commands

    Command Description
    Press the ESC key
    Type :q!
    Press the ENTER key
    Exit vim without saving changes i.e. discards any changes you made.
    Press the ESC key
    Type :wq
    Press the ENTER key
    Save a file and exit.
    Press the ESC key
    Type 😡
    Press the ENTER key
    Save a file and exit.

    Conclusion

    You learned how to save a file in vim and exit the text editor using various vim commands. See the vim site or the following pages for more information.

    🐧 Get the latest tutorials on Linux, Open Source & DevOps via

    Category List of Unix and Linux commands
    Documentation help • mandb • man • pinfo
    Disk space analyzers df • duf • ncdu • pydf
    File Management cat • cp • less • mkdir • more • tree
    Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
    Linux Desktop Apps Skype • Spotify • VLC 3
    Modern utilities bat • exa
    Network Utilities NetHogs • dig • host • ip • nmap
    OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
    Package Manager apk • apt
    Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
    Searching ag • grep • whereis • which
    Shell builtins compgen • echo • printf
    Text processing cut • rev
    User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
    WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

    Comments on this entry are closed.

    w!= save without exiting
    q!= exit without saving

    To save and quit:
    If you are currently in insert or append mode, press Esc key.
    Press: (semi-colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
    Enter the following command (type wq! and press Enter key).
    🙂

    Press: (colon) The cursor should reappear at the lower left corner of the screen beside a colon prompt.
    colon is right.

    none of these commands are working

    Hit the ‘esc’ key and it will take you out of the cursor mode and give you a cursor at the lower left hand of the screen. Now enter wq! and it will save and quit.

    Worked for me (Ubuntu 16.4). Thanks

    Thanks for sharing knowledge!
    Worked just perfectly!

    worked for me too on Ubuntu. thank you!

    The theme become so popular in the entire Www, so I’ve created fun page

    Источник

    How to Save a File in Vi / Vim & Exit

    Home » SysAdmin » How to Save a File in Vi / Vim & Exit

    Vim (Vi IMproved) is an open-source text editor for Unix or Linux systems. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI.

    Take the first step in learning Vim with the basic saving commands. Follow this guide and learn how to save a file in Vim / vi and exit the editor.

    • Ubuntu 18.04 Bionic Beaver
    • A user with sudo privileges
    • Access to a terminal/command line
    • The apt tool, pre-loaded in Ubuntu and other Debian-based distros.

    Using Vim Modes

    When Vim launches, files are opened in command mode by default. This means that you can move around and edit the file, but cannot insert new text.

    All alphanumeric keys in this mode are equal to commands, and pressing them will not display their value on the screen. For example, pressing the letter w will move the cursor one word forward.

    To type text, you must be in insert mode. To switch to insert mode, press the i key. Now you can type text into a file.

    To switch back to command mode, press the ESC button.

    How to Save Changes in Vim

    Vim Save and Quit Command

    To save a file and exit Vim:

    1. Switch to command mode by pressing the ESC key.

    2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

    3. Type x after the colon and hit Enter. This will save the changes and exit.

    Alternatively, a command that will also save a file and exit the text editor is:

    In which case the w stands for write (=save) and q stands for quit.

    How to Save a File in Vi / Vim Without Exiting

    To save a file without exiting in Vim:

    1. Switch to command mode by pressing the ESC key.

    2. Type : (colon). This will open the prompt bar in the bottom left corner of the window.

    3. Type w after the colon and hit Enter. This will save in Vim the changes made to the file, without exiting.

    You can also rename an existing file by adding the new name after the commands.

    In the picture below, we renamed the existing example1.txt into newexample.

    Exit Without Saving Changes in Vi / Vim

    To exit Vim without saving changes:

    1. Switch to command mode by pressing the ESC key.

    2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

    3. Type q! after the colon and hit Enter to exit without saving the changes.

    This article explained how to save files in Vi / Vim, with multiple command options.

    The more comfortable you feel using keyboard shortcuts, the more you will understand why Vim earned its worldwide reputation. Next learn how to copy and paste in Vim!

    Источник

    Читайте также:  Windows safe mode disable services
    Оцените статью