Set Vim window size and selection from command line - vim

I want to launch two files from the command line and I want the window to be split horizontally. This can be achieved with the following command:
vim -o index.html index.1
Which gives me the following output split evenly
What I want, instead, is for the top window to be substantially larger, like so
How do I achieve this? Also, currently, the top window is selected, which is what I want. However, if it wasn't selected, or I wanted the bottom window selected, how would I go about achieving this?

You can open the two files with
vim index.html +10sp index.1
After vim has read index.html it executes the ex command 10sp. That means that the current window is split into two with the new window being 10 rows high. After that index.1 is read by vim and loaded into the currently active window.
If you already set splitbelow in your .vimrc index.1 is loaded into the bottom window that is active at the same time.
For example if you want the top window to be active and splitbelow is set you can append the corresponding ex command to the line
vim index.html +10sp index.1 +"wincmd k"

One possible solution would be to make the top window 80% of the available size:
vim +'execute "resize" (&lines / 10) * 8' -o file1 file2
See :help :resize and :help 'lines' for more information.
Small clarification: & is used in front of options to retrieve its value. Thus &lines holds the value of what you could set yourself, e.g.:set lines=100. In this case, &lines gets set by Vim on-the-fly even when you resize the window that holds Vim.

Related

Restore size of splits in a vim session

Here i found a link to this script which saves the screen size and position when exiting vim. If i use :mksession ~/session.vim in fullscreen mode with several splits, exit vim (gvim) and open it like: gvim -S ~/session.vim it messes up the sizes of the splits (pretty randomly. All splits have different sizes). They are all shifted to the left. Is there already a way of doing this properly?
Have you verified your 'sessionoptions'? It should include 'resize' and 'winsize' at the very least to get what you're looking for. :help 'ssop' will give you the list of options to set for what gets saved into session files.

How to have different title and tab title in vim

As of now I have put the following in my .vimrc file in order to display the name of the currently open file in the screen title of my Terminal:
autocmd BufEnter * let &titlestring = expand("%:#")
set title
Is it possible to also set a specific tab title? That is, when I have a number of vim tabs open in Terminal on my Mac I would Terminal to display the directory path in the Terminal header when I have selected the tab related to the file and I would like to display only the name of the file in the header of the tab.
Some comments to your goal
Personally I hardly use tabs in vim. What you want to have can be achieved by setting some options. Before I talk about the options, I would say that what you want to do could be inconvenient. Because in vim, tab is a collection of windows, which means, it can have splitted windows, and in a tab page, you can show more than one buffers. That is, in a tab page, it is not always showing one file. As you are switching from split to split in a tab page, both your terminal title and the tab label could be changing.
For example, you have 3 dirs, /one /two and /three and two tab pages in your vim.
Now you have:
tab1:
/one/file1
tab2 (in split-windows):
/one/file1
/two/file1
/three/file1
When you are in tab1, your terminal title would be /one, tab1-label would be file1, fine. But when you are in tab2, your terminal title will change as you are switching from split to split. In this example, the tab-label would be same file1, so you have to check terminal window and the tab label to know which buffer/file you are currently editing. I don't think this is convenient.
Answer to your question
Okay, if you want that to happen, :h setting-tabline and check the example in the help text, it shows how to set the label of tabs. This should achieve your goal for vim in Terminal.
FYI, to get only parent dir name you can use %:p:h. to get only filename, you can use %:p:t.
Plus, some other things you may want to check:
There is another autocommand event: TabEnter .
:h setting-tabline if you want your tabline to show some complex text or other highlighting
if you want to play with filename/dirname further, you can check :h expand() and :h filename-modifiers

in VIM, How to set the initial window size via file browser?

I'm using vim to browse through different folders. (i.e., issue :tabe . command in vim)
However, when I open the file either in split window (pressing 'o') or in vertical split window(pressing 'v'), the newly opened window size is really small. (while meanwhile, vim's file browser's windows stays pretty big which I don't really need).
I know that I can manually change the window size by Ctrl+W with either -/+ or split, or for vertical split, or '=' to make the window sizes equal; but that's too troublesome.
I want to check if there're existing ways to set the default size of the window opened using "o" or "v" to be bigger?
Thanks a lot ;)
The new window should take half of the height or half of the width of the current window which can give you small windows pretty quickly.
The option that defines that proportion can be found in :help netrw:
let g:netrw_winsize = 75
Note that using this method will make any further split even smaller than before:
let g:netrw_winsize = 50 (default)
|-netrw--------------------------------------------|
|-netrw------------------|-file--------------------|
|-netrw------|-file------|-file--------------------|
let g:netrw_winsize = 75
|-netrw--------------------------------------------|
|-netrw------|-file--------------------------------|
|-netrw|-file|-file--------------------------------|
The "problem", here is that netrw splits its own window, not the previous window.
Netrw can be used to open the file under the cursor in the current window, in a split window or in another tab but the way it splits its own window makes it hard to use it the way you want. AFAIK, the most common usage is:
open netrw with :Ex,
navigate,
hit <CR> to open the file under the cursor in the current window,
edit,
re-open netrw in its latest state with :Rex (for Peter Rincker),
GOTO 2
An alternative is to use :Vex to open netrw in a vertical split and use P to open the file in the previous window.
I'm afraid Netrw is not really designed to work like what you seem to want it to work. IMO, netrw is more like an "open…" dialog than the kind of file explorer pane you can see in most editors/IDEs. I'd suggest you either get used to it or try NERDTree which has only a subset of netrw's features but is designed to be more like those file explorer panes.
Actually the split size is not relative to netrw, it's the default size vim sets for newly created splits, so if you want to resize the actual split which is in you case the navigator (netrw) you can use this commend:
:vertical resize 30
There is :Lex now to open a left explorer. You can place let g:netrw_winsize=30 in your vimrc file to keep the explorer small.

Vim window resizing

I split my windows in Vim horizontally. But after I split, the new window occupies half of the original window size, I want to change this and make sure the new window occupies almost the entire old window.
Basically if I open three files using horizontal split in Vim, I should see three status bars at the bottom of the screen the third file occupying the rest of the screen. The files as I already know can be navigated through Ctrl+W+arrow keys. So if I navigate to second file now, I should see one status bar at the bottom and one status bar at the top.
Kindly let me know how to configure the same. I looked up online all I could find is options to dynamically change the size or resize, but I want static one-time config (for example, in vimrc or bashrc).
If you set the winheight option to 999, the current window occupies as much of the screen as possible, and all other windows occupy only one line (I have seen this called "Rolodex mode"):
set winheight=999
You can type in command mode :res[ize] +N or :res[ize] -N where N is the amount in which your window will grow or shrink respectively.
Go to point 6 (Window resizing) http://vimdoc.sourceforge.net/htmldoc/windows.html but the article has everything on windows management in VIM
Hope this helps!
You might prefer just using vim tabs, which work rather like how you described.
Try this:
vim -p file1 file2 file3
Then use :tabn and :tabp to cycle fwd and back through the tabs.
I also like this mapping in .vimrc to use ctrl-l and ctrl-h to cycle fwd and back respectively:
noremap <C-h> :tabp<CR>
noremap <C-l> :tabn<CR>
Command for split window:
:[N]sp[lit] [++opt] [+cmd]
Where N is height of new window. Default is to use for half the height current window. Same thing for vertcal splitting:
:[N]vsp[lit] [++opt] [+cmd]
N is width for split window.
And so on:
[N]new, [N]vnew
For details read the
:help split
But I can't understand why you do not use buffers?

Vim: Lock top line of a window

Is it possible in Vim to lock the top line of a window so that the first line in buffer is always seen on top of the window?
I have a file, say, dump of a database table. On the first line there are names of columns, other lines contain data. I want to be able to scroll contents up and down, and always see column names.
N.B. Lines can be lengthy, so I use nowrap and want column names and contents to scroll right and left simultaneously. That's why :1split doesn't suit -- unless there's a way to scroll two windows at the same time.
Thanks.
Thanks guys! Let me summarize the actual commands that did the job for me:
:1spl # create an extra window with only 1 line
:set scrollbind # synchronize upper window
ctr+W , arrowDown # switch to other window
:set scrollbind # synchronize lower window
:set sbo=hor # synchronize horizontally
Split your window, decrease the top window height, set the top most line to be the first one and get back to the working window.
:split
:resize 1
gg
Ctrl-w w
You can scroll two windows at the same time, so I think you can do what you want by splitting your window, and locking the scrolling behaviour. See :scrollbind and this tip for more details. Note that you have to lock each window in order that they move in sync.

Resources