Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does :q1 do in Vim?

Tags:

vim

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.

like image 437
scubbo Avatar asked Feb 12 '18 23:02

scubbo


1 Answers

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
like image 74
Subash Avatar answered Sep 18 '22 01:09

Subash