Yank text from VIM editor from one session to another session - vim

I use Putty to connect to a server and I use 2 sessions, because I want to compare 2 .sh files and I find it easier to have both files on different windows.
I am using VIM as a text editor and want to yank a line from the file of the first session to the file of the second session.
I am using V"+y to yank and then p to paste, but it only works if I close the file in the current session and open the other file in the same session.
Is it even possible to yank text from one session and paste it in another?

You can have two windows in one single Vim "session":
# two windows stacked vertically
$ vim -o file1 file2
# two windows stacked horizontally
$ vim -O file1 file2
And you can even diff them:
$ vim -d file1 file2
So it seems to me that your initial goal, as described, doesn't warrant the use of two separate Vim "sessions" at all.
See :help -o, :help -O, :help diff.
To yank between two concurrent Vim "sessions" or one Vim "session" and another program, the bare minimum you need is a clipboard-enabled Vim but it is not clear what you call "session" (is it a Vim session or a shell session?) so I doubt that it will be enough in your case.

It turned out that I had mouse mode enabled and when I turn it off I can simply Ctrl + C the needed section and add it to the other session. Didn't know the mouse mode makes such a difference.

Related

Yank in tmux between two editors

So, I am trying to copy from one file to another:
- They are both in the same session of tmux
Here is how I get the problem:
tmux new -s vim
Now I split the window in two:
C-b + %
And now I open two files in vim, one in each pane. When I try to yank and paste some lines from one file to the other it doesn't work.
For that I am using the "p" key
If you have different instances of vim running in tmux, then you could copy between them using tmux's copying commands.
For instance:
ctrl-b [ to enter copying mode
<space>, then move around with the arrow keys to highlight some text
<return> to finish
ctrl-b ] to paste (including in a different window or pane)
Update: See this blog post for more details. The exact answer may depend on your key bindings.
You can make the two vim instances share the same clipboard.
First check that your vim supports clipboard by typing vim --version, if you see +clipboard in the output, then in your ~/.vimrc, add
set clipboard=unnamed
now you can freely yank and paste between tmux panes and windows.

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.

How to synchronize cursor movement in multiple split windows?

I remember Notepad++ has this feature, which is really helpful to compare files. I want to realize the similar one in Vim. When moving cursor in one split, cursors in the other split windows will move automatically.
Depending on what you're after, I see 2 possibilities.
1) Diffing windows
If you want to compare files, have a look at :help diff. E.g.
You're on the shell:
$ vimdiff file1 file2 or $ vim -d file1 file2
You have one window open already:
:vertical diffsplit foo.c
You have 2 windows open already:
Do :diffthis in each of them.
2) Scrollbinding windows
You might also like :help 'scrollbind' and :help scroll-binding, if you don't want to jump into diff mode. Thus in the easiest case it's only doing :set scrollbind in both windows.

Vim automatic scroll buffer

I'm trying to do something very simple:
I have two buffers in Vim, one with source code (B1), another one with a text file a.txt (B2). The source code in B1 is run with a custom shortcut in Vim, filling a.txt with text. I want Vim to automatically scroll B2 every time it updates, even if my cursor is in B1. So, I just want that the buffer B2 to behave the way tail -f does.
Doing this with ConqueTerm is not an option, since buffer B2 can be a preview buffer from other plugin.
In general, vim events fire in response to user input. They don't just run continuously in the background.
This post details some tricks you could repurpose to write some customization code to reload your "tail -f" buffer and scroll to the bottom periodically.
Rather than trying to do this all in vim, here is a different proposal that would achieve a similar effect using GNU Screen (one terminal area with vim editing a file file1, another running tail -f against a different file file2):
download/install GNU Screen (perhaps apt-get install screen)
run screen
run vim file1
type Ctrl-A S to split the terminal down the middle horizontally (or, in recent versions of screen, Ctrl-A | to split it down the middle vertically)
type Ctrl-A Tab to switch to the other side of the split
type Ctrl-A c to start a new terminal on this side
run tail -f file2
Now one side of the split shows vim file1 and the other side shows tail -f file2. Here's an image of the result.
This command moves to the window containing a.txt, reloads it (edit), moves to the end (+$) and then jumps back to the previous window. You can add it to the shortcut that runs your source code to get a.txt to update whenever the code is run.
:exe bufwinnr("a.txt") . "wincmd w" | edit +$ | exe winnr("#") . "wincmd w"
Remember to escape the bars (\|) and add a <CR> at the end if you use this in a mapping.
This will require your shortcut to run the code in foreground, and Vim will be unresponsive till it is done (unless you press Ctrl-C). If you want Vim to reload a.txt automatically whenever it is changed (thus bypassing the need for running the code in foreground), you will need an external mechanism for file change detection (such as inotifywait on Linux). As far as I know, Vim only checks for external changes (with autoread) when you move the cursor to that window.

Can you use vi with multiple file names specified on command line?

Is it possible to specify multiple file names on the command line when starting vi?
Yep just use :n to go to the next file in the list, and :N to go to the previous.
You can open multiple files by using globbing E.g., vi *.html will open all the HTML files in your current directory. And, as Alex says in his answer, you can navigate back and forth through the files with :n and :N.
If you want to open multiple files at the same time, you can also use the split command.
Open the first file as usual, then, then use the command :split yourfile.ext
You should now see both files at the same time in a split-screen view. You can do this with more than 2 files (but I'm not sure what the limit is).
Now, you can navigate between the windows with ctrl-w and the arrow keys. So, if you're in the bottom pane, and you want your cursor in the upper pane, you'd first press ctrl-w, then press the up-arrow key.
Also, you can resize one of the panes by adding or subtracting rows/lines in that view. So if you're in the upper pane and you want it to be 5 lines larger, you'd press 5, then ctrl-w, then +. Same for reducing with the - key.
I'm sure that there are many other commands you can use, but these are the ones I use.
Good luck!
Also convenient:
vim -p file1 file2 file3
That will open up vim with tabs containing each file specified. You can jump between files with
gt
and
gT
If you do
:set mouse=a
you can also click on the tabs to open or drag them (although hardcore vim users would frown on this :) )

Resources