Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I open a new tab to left of the current tab?

Issuing :tabnew somefile will open somefile in a new tab to the right of the current tab. Can I somehow get Vim to open a tab to the left of the current tab?

Update: The suggested answers do allow me to open a new tab left, but they break file name auto completion, which is a no-go.

like image 708
bitmask Avatar asked Nov 07 '12 18:11

bitmask


People also ask

How do I open a new tab to the left?

The default keyboard shortcuts are: - Alt+T to open a new tab to the right of the current one. - Alt+H to open a new tab to the left of the current one.

How do I change the tab arrangement?

Google altered this workaround with a subsequent update to Chrome for Android, so now there's an extra set of steps to follow: Type "chrome://flags/#temporary-unexpire-flags-m88" into the address bar. Tap on the drop-down menu in the Tab Grid Layout entry. Select "Enabled"


3 Answers

Since Vim 7.4.530 (2014), you can use negative values for [count] in :[count]tabnew to open tabs. To open a tab directly to the left of the current tab, use:

:-1tabnew

Documentation: https://vimhelp.appspot.com/tabpage.txt.html#:tabnew

:[count]tabe[dit]                               :tabe :tabedit :tabnew
:[count]tabnew
                Open a new tab page with an empty window, after the current
                tab page.  If [count] is given the new tab page appears after
                the tab page [count] otherwise the new tab page will appear
                after the current one. 
                    :tabnew     " opens tabpage after the current one
                    :.tabnew    " as above
                    :+tabnew    " opens tabpage after the next tab page
                                " note: it is one further than :tabnew
                    :-tabnew    " opens tabpage before the current one
                    :0tabnew    " opens tabpage before the first one
                    :$tabnew    " opens tabpage after the last one

Similar functionality is also available for :tabclose, :tabonly, :tabmove, see the commit linked above. If this does not work, use :version to check if your Vim is up-to-date and/or use :help tabnew to check whether the documentation looks like the one cited here.

like image 74
Rob W Avatar answered Oct 02 '22 18:10

Rob W


To utilize the behavior @romainl described without having to resort to knowing current tab page number use the following command:

command -nargs=* -bar Tabnew :execute (tabpagenr()-1).'tabnew '.<q-args>

. Note: it is perfectly save to use 0tabnew: this does what intended and makes new tab the first one, even though there is no tab page that has number below 1.

If you are sure you never use this command with ++opt or +cmd you can use -complete=file just after -bar. Note: besides its name it is not a completion option because it as well does filename expansion (and shows errors in case -nargs=1 and globs expanded in too many filenames). Unfortunately this behavior is not even mentioned in documentation.

like image 26
ZyX Avatar answered Oct 02 '22 17:10

ZyX


You can use a [count]. Supposing you are at tab #4, :3tabnew creates a new tab on the left of the current tab.

Keep in mind, though, that tabs are always created to the right of the current tab or tab #[count]. :3tabnew effectively means "create a new tab after tab #3".

like image 40
romainl Avatar answered Oct 02 '22 17:10

romainl