50

I know the basic command ESC + SHIFT ZZ and another, ESC + :wq ENTER, but I don't want to quit Vim.

I want to save the file and the file should remain open in Vim.

4
  • 3
    The answer is :w but I'm pretty sure that's a duplicate. Commented May 18, 2016 at 15:56
  • I don't know if I should vote to close this question.... There could be many similar Qs coming, like "how to move curse up/down/left/right", "how to quit without saving", "how to undo/redo"... Commented May 18, 2016 at 15:59
  • 1
    @DarshanSharma The most important command in vim you should learn is not :w, it is :h, do :h wq you can see many possibilities to save/quit/discard.... Commented May 18, 2016 at 16:08
  • 1
    @DarshanSharma you may want to play vimtutor first. just type the command in your terminal. Commented May 18, 2016 at 16:16

3 Answers 3

104

ESC, :, w, Enter

The w is "write", the q is "quit"; use only "write".

Sign up to request clarification or add additional context in comments.

Comments

5

Here's another shorcut that works, if you're interested in saving a keystroke or two. Run the below commands (as long as your Vim(rc) config file matches the below target destination):

`$ echo "noremap ww :w<CR>" >> ~/.vimrc`
`$ source ~/.vimrc`    .

This should add a line to your Vim config file, noremap ww :w<CR>, which: (1.) Declares it's going to create a new Normal-Mode "Remap" (noremap); (2.) Designates what we can consider a new operator, or operating keystroke, ww, for the...; (3.) ...for the operand command, :w<CR>, which stands for "[Enter the] command mode, with ':', then key stroke 'w', following with a Carrige Return ()", and is the how the operator will behave when called. In case you're not familiar with Vim's Normal mode shortcuts: For example, using my example, you'd literally just type the letters 'ww', and read the bottom for confirmation that lines were written. (There are no "CTRL-... or CMD- or Shift-"-like combinations necessary.)

2 Comments

I really appreciate answers like this. It may go beyond the scope of the question, but I searched for and found this question because I'm still fairly novice with Vim, and am trying to learn basic things. This answer provided a bit of extra information in an easily digestible quantity to not only accomplish the task I set out to learn, but dip my toe into a slightly more advanced feature that would be less likely for me to end up deciding to search for explicitly. Thanks, @Levi Maes
That being said, it does appear that this specific remap appears to interfere with using w to navigate. w is supposed to jump to the beginning of a word, but that seems to be getting ignored with the ww remap. W to jump forward a word still works.
2

How about the :update / :up command? It's similar to the :write / :w command, but it doesn't actually write out the file if there were no changes made that need to be written out.

(Kind of like the difference between :x and :wq ; if there are no changes to save, :x just quits without writing out the file, whereas :wq writes out the file regardless of whether there are any changes.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.