dylnmc
dylnmc

Reputation: 4020

vim specify window in split like %

If I want to open a split in vim, I do :sp % - that will, in this case, do a horizontal split with the file in my current buffer and window. :vsp is the same thing but vertically.

Now lets say I already have a vertical split and I am in the window to the right. What would I do if I wanted to horizontally split the file in the window to the right without specifying its path. I believe I have seen this with + or ,, but I cannot seem to find it, and don't know much about this murky part of vim.

Visual representation or what I want

+~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~+
| ./test1           | ./test2           |
| Stuff in my other | I am currently in |
| window            | this file ...     |
| bla foo           |                   |
| bla               |                   |
| bla bar           |                   |
|                   |                   |
|                   | :sp ./te[tab]     |
+~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~~~+

The above works, but I would like to to specify the window to the left without the filename.

Any thoughts?

Upvotes: 1

Views: 82

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45177

Split the current file

You can do the following to split the current file with out specifying the file name or %.

:sp
:vsp

If commands aren't your thing you can use window mappings:

<c-w>s
<c-w>v

Split new file

Split with :split and :vsplit with a filename.

:split foo.txt
:vsplit bar.txt

Just like :edit you can do the following:

  • <tab> completion
  • List completions via <c-d>
  • Use globs, e.g :sp *foo or :sp **/*foo
  • Use % tricks. e.g. :e %<.h, :e %:h/bar.txt. See :_%
  • Alternative file via #. e.g :e #. Note: <c-^> is probably a better option

Split to an already open buffer

Use :sbuffer as a split variant of :buffer.

:sb foo
:sb 7

Use :vert sb for vertical splits.

Behold the power of :b:

  • Uses <tab> completion
  • Use <c-d> to list out completion
  • Use partial file name. e.g. :b foo. Works great with <tab>.
  • Globbing. e.g. :b foo*bar or :b foo/**/bar
  • Might want to use 'hidden' via set hidden
  • Split variant of :b is :sb.
  • Also accepts a buffer number

A common mapping:

nnoremap <leader>b :ls<cr>:b<space>

Window management

Moving a split can be accomplished via window mappings

  • <c-w>J move split down
  • <c-w>K move split up
  • <c-w>H move split left
  • <c-w>L move split right

For more help:

:h window
:h :sp
:h :sb
:h :b
:h 'hidden'
:h :_%
:h CTRL-W

Upvotes: 3

Related Questions