Vim: vsplit and run external command - vim

I'm trying to map a command or function that splits a new window vertically, switches the cursor to the new window, and runs an external command on the <cWORD>.
When I do:
:vsplit|wincmd w|execute '!perldoc <cWORD>'
Vim seems to run the the external command first in the current window and then when the command exits, Vim creates a new window and switches focus to it.
If I break these commands up into separate lines in a function and call the function, the same thing happens. Is there a way to have Vim do what I want it to do?

My coworker Tye showed me how to do it:
let w=expand("<cWORD>") | vnew | execute "read !perldoc " . w|1
Save <cWORD> as variable w
vnew opens a new empty window
execute read !perldoc passing w as the variable. The output is read into the new window
Jump to the first line of the buffer in the new window

If you don't tell Vim otherwise, new vertical windows open on the left by default, leaving the cursor in the new window.
If that's not what you experience you should definitely investigate why.
After :vsplit, further commands are executed before the new window is rendered, in what you can imagine as a "virtual window".
This means that, however unsettling it can be, you won't see the new window before the next command. That's how Vim works and there's nothing you can do about it.
In the example below…
I have two perl files in the same directory,
in perl.pl there's a sample perl script I found online,
in warnings.pl there's a single word, warnings,
perl.pl is open in Vim with the cursor on strict,
I run :vsplit|!perldoc <cWORD>,
I expect to see the documentation on strict in my pager and the same buffer displayed in two vertical windows when I come back to Vim,
I close the left window,
I run :vsplit warnings.pl|!perldoc <cWORD>,
I expect to see the documentation on warnings in my pager and two different buffers displayed in two vertical windows when I come back to Vim.
Let's see:
Looks like everything worked as expected.
I don't see any but you probably have a good reason for opening that new window. I will suggest :help K and :help 'keywordprg anyway:
set keywordprg=perldoc

Related

Vim: Close 1 file in a split view?

I'm currently learning vim using vimtutor but whenever I try to open vimtutor there's always a split screen view of the previous file like this:
How can I close the bottom window?
You can press 'Ctrl w w' to switch between the window panes. Switch to the second window pane and in normal mode, type :q and enter. This should close the bottom window.
The screenshot looks like an empty file on the bottom; and on the top, the vim help screen describing vimtutor. I think you must have typed vim on the command line (which launched Vim with an empty file), then :help vimtutor (which brings up a Vim help page about vimtutor).
That would be why ctrl-w w and :q closed the whole vim editor. The ctrl-w w put you back in the new blank file; and :q executed a "quit" command.
Instead of launching vim that way, on the command line try typing vimtutor.
The top window has file type of "help" (this is shown in its status line). Help is normally viewed in splits, not as a single window. When other window is closed with :q and "help" becomes the last one, vim closes the whole tab or even the app. So don't do this.
If you seem the window is too small then maximize it vertically by pressing ctrl-w followed by "underscore". But it is really a matter of habit. No one bothers doing tab help vimtutor or such. Split view is just okay. So the best advice for you is "to comply".
You are not "using vimtutor"; you are using Vim and viewing the help for vimtutor.
What you get is absolutely normal and expected: the help is always displayed in a window so that you still have the thing you are working on visible while you check the documentation.
To actually use vimtutor, quit Vim and, when you are back at your shell, run the following command:
$ vimtutor
To keep only the current window press Ctrl+w+o. Mnemonic "only"

How to open a vim terminal in the full area of the vim buffer?

I really like the built-in :terminal in recent vim versions. I have one pain point though: sometimes (often?), I would like the terminal to take the full vim window size. However, usually, using :terminal only uses a partial split on half the vim window size, even if I start from an empty buffer. From an empty buffer, using ctrl-w o maximizes.
Any way to open at once into a "full size split" that takes the full vim buffer, instead of needing the extra ctrl-w o step?
You can do :tab terminal to open a terminal window in a new tab page.
You can do :terminal ++curwin to open a terminal in the current window.

How to split VIM window to show content and enable command

I am currently reading a manual about how to compile and run a program. Currently I have two terminals, one terminal shows the manual, the other terminal is where I follow the instructions and type the command. Is it possible to do those two things in one terminal. Just like show two files in one terminal. I want to read the instruction in the left side of the terminal window and type command in the right side of the terminal window. Any advice?
I don't know what's your problem, having two terminal emulator windows side by side is perfectly acceptable. If you really want to add complexity to your learning experience, you can try one of these terminal multiplexers:
tmux
dvtm
or, if such a thing exists for your platform, a terminal emulator with split windows like:
iTerm (Mac OS X)
Terminator (Linux)
In VIM you can create new "windows"/panes just type
:vnew
For vertical window and
:new
For horizontal window
If you want to know more about windows you can check this or if you want to know more about VIM features you can try this
you can use one of the following
:tabe filename to open a new tab containing filename or nothing for a blank tab.
gt to switch and gT to switch backwards.
:sp to split the current window ^w j to jump down and ^w k to jump up
or :vsp to split vertically (so you can read in the left) ^w h to jump left and ^w l to jump right
all key strokes are in command mode

Vim command for switching window

I am using vim editor in pycharm. This editor is invoked using terminal which is available in pycharm. My problem is that, when i split vim window (:sp or :vsp), I am unable to switch to another window.(ctrl-w ctrl-w). It is obvious as most keyboard shortcuts, are not working in this integrated environment though commands are working fine. What is the command to switch window?
For more details:
I tried 'wincmd R'. It just switching position. But cursor remains in same position i.e. in old window. So its not useful. Is there any argument for 'wincmd' to actual move focus from one window to another?
Did you read :help window-move-cursor and :help :wincmd?
The former has all the commands that allow you to move the cursor from one window to another and the latter is very clear about the relationship between <C-w><char> and :wincmd <char>.
If you already know <C-w>w, what do you think would be the right argument to :wincmd?

Gvim: Move tab to new window

The opposite question seems to be asked a lot: how to move a window into a new tab in an existing window. What I'm hoping is that a tab that I have open in gvim can be moved out into its own window or into another existing window.
Is this possible?
Thanks!
Same Vim instance
If that tab shows just a single window, you just have to note its buffer number (e.g. via :ls or :echo bufnr(''), or by including it in the statusline), and then close the tab via :close (:set hidden helps with modified buffers), then going to the target tab / window, and re-opening the buffer there via :buf N or :sbuf N.
If you need to support multiple windows in a tab page, you'd have to write a custom command / mapping that first remembers the buffers, and then applies the above steps for all of them.
Different Vim instances
Edit: The above is for movement within a single Vim instance. If you want to move a buffer to another GVIM instance, you first have to :bdelete it in the current Vim, to avoid swap file messages. Launching in new instances is easy:
:execute 'bdelete | !start gvim' shellescape(expand('%:p'), 1)
This passes the (full absolute) path of the current file to a fresh GVIM.
To move a file to an existing GVIM (you need to know its v:servername), you need to use the remote client-server communication (:help remote.txt), e.g. by sending a similar :drop command via remote_send(), like this:
:execute 'bdelete | call remote_send("GVIM1", ":drop " . ' . string(fnameescape(expand('%:p'))) . '. "\<CR>")'
Here is how you can "move" the current buffer to a second GVim instance:
:!gvim --remote %
:bw
Note that Vim must be built with the +clientserver option.
No, it is not possible.
You cannot move a vim tab into a window, no matter new or existing. Because a vim tab page is a collection of windows. You cannot move a collection of windows into one single window.

Resources