15

I just noticed that entering :q1 closes the file I currently have open (I found this by typo-ing :q!)

Based on commands like 2dd (delete two lines) or 2j (move down two lines), I would have expected :2q to "quit two files" (although, if I open two files with vi -O testfile1 testfile2, :2q only closes one of them - and so does :q2).

Is the number after :q simply discarded? Or is it having some effect that I haven't been able to determine?

And, more generally - how could I answer this question for myself? I checked usr_21.txt| Go away and come back in Vim help, but found nothing about this behaviour. I even checked the famous question, but no-one had mentioned anything about this.

1

1 Answer 1

18

The q command with number closes the given split in that position.

:q<split position> or :<split position>q will close the split in that position.

Lets say your vim window layout is as follows:

-------------------------------------------------
|               |               |               |
-------------------------------------------------
|               |               |               |
|               |               |               |
|    Split 1    |    Split 2    |     Split 3   |
|               |               |               |
-------------------------------------------------

If you run q1 command it will close the first split. q2 will close the second split and vice versa.

The order of split position in the quit command does not matter. :2q or :q2 will close the second split.

If the split position you pass to the command is greater than the number of current splits, it will simply close the last split.

For example, if you run the q100 on the above window setup where there are only 3 splits, it will close the last split (Split 3).

This is whats listed in the vim documentation. You can find the documentation by typing :help :close then scroll up to find the Closing a window section.

If [count] is greater than the last window number the last
window will be closed:
¦ ¦ :1quit  " quit the first window
¦ ¦ :$quit  " quit the last window
¦ ¦ :9quit  " quit the last window
     " if there are fewer than 9 windows opened
¦ ¦ :-quit  " quit the previous window
¦ ¦ :+quit  " quit the next window
¦ ¦ :+2quit " quit the second next window
2
  • This does work, however I am not seeing it documented for :q with a [count] under :h :q. Is there another place it is documented? Commented Feb 13, 2018 at 0:01
  • 1
    You can do :help :close. Then scroll up a bit. Its documented in Closing a Window section.
    – Subash
    Commented Feb 13, 2018 at 0:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.