Vim share Ex commands globally - vim

In Vim,
I can open Vim Ex command Window with q:
It seems to me all the commands are not shared in other instance of Vim.
e.g. I have other Vim instance in a second console.
I'm wondering whether I can share all the Ex commands that I have typed for last ten days

No, command history is not shared. Histories (as well as marks, jumps, opened files) are stored in a viminfo file. The contents are written when quitting Vim, and read during startup.
But (and here comes the interesting part), you can manually trigger these with the :rv[iminfo] and :wv[iminfo] commands. So, first execute :wv in the one Vim instance to export its history, then import via :rv in the second instance. This could even be automated via :autocmds.

Related

How to save changes in vim parameters

I would like to change some vim parameters so I can see the spaces+eol, and set the tabulations as 4 spaces. I found a few commands that allow me to do that but i have to type the commands every time I wanna use them. Is there a way so save them so they stay even when I close vim?
Put those commands in a vimrc file. Vim loads this on startup and runs the commands in it, as if you had typed them in.
See also: https://vi.stackexchange.com/questions/13398/are-there-any-comprehensive-docs-for-vimrc

vim paste in byobu fails if origin file is not closed

I connect my server(centos 7) with putty, and run vim in byobu.
Now I have 2 windows in byobu, and two files, a.py in window-1 and b.py in window-2.
I tried copy some text from a.py to b.py, but ran into some problems.
If I follow steps below, it works fine:
STEP1: In window-1, open a.py, press yy, then quit with :wq
STEP2: In window-2, open b.py, press p.
But if I keep a.py, and b.py open at first , or doest quit a.py after yy, paste fails.
It seems quite odd, did missed anything?
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version, my vim version is 8.2.1438.
You are able to paste across Vim sessions because of the [viminfo] file. By default, Vim stores the contents of non-empty registers (including the default register) into the viminfo file. That's why when you put (with p) right after you start Vim, you get the contents you last yanked (with yy) before you quit Vim last time.
The viminfo file is written right before Vim quits, and it's read during Vim initialization. That's why this won't work automatically if you start the second instance of Vim before you quit the first instance. Then the viminfo file will be read too early by the second instance (or written too late by the first instance) to allow you to put the yanked text across instances.
You can explicitly write the viminfo file with the :wviminfo or wv command. And you can explicitly read it with the :rviminfo or :rv command. So you could use these two commands in the two separate instances to transfer the register information through the viminfo file without having to quit them.
I have set clipboard=unnamedplus in .vimrc, and have +clipboard in vim --version.
If you're using the X11 clipboard integration, you could use that to share copied contents between Vim instances. Note that setting the 'clipboard' option only affects yanks, so to put from the clipboard register you'd have to use "+p.
But this assumes you have a valid X11 server to connect to, which is quite unlikely in your situation. First, since you're connecting through SSH using PuTTY, you would need to use SSH forwarding to export an X11 server from your client. Since you're using PuTTY, I'm assuming you're on a Windows box, which doesn't really run X11 by default, so you'd have to install one and run it (easier said than done.) Also, since you're running Byobu, you will also have trouble reconnecting to an existing Byobu session, which will quite likely be pointing at an invalid X11 server, using the connection information at the time the Byobu session was created, which may no longer match the new X11 forwarding connection information of the new SSH session... It'spossible to make this all work, but if you want my opinion, it's probably too much trouble, especially for this particular use case of two Vim instances in the same remote box.

VIM: Overwriting system vimrc

I work on multiple MAC OS X systems, which do not save changes after log out. As you know VIM is on every new mac, just type in vim in the terminal. I always bring my vimrc file with me, and the problem is that every single time I start vim i have to load it with :so command.
I want to use the fact that vim is available on every unix, but I also want to take advantage of the nifty integration with the terminal for doing quick tests, I just switch back to the terminal, but for that I need to close vim. When I reopen it, I again have to load the vimrc. And I am a newb, I don't even have plugins yet...
I want to keep the integration with the terminal and only develop a super quick way of introducing my changes to vim. Think of the conditions as if though you are sitting on newly installed operating system.
Thanks !
Not directly to your question, but you can always invoke vim with -u, which will let you specify your vimrc file instead of launching vim and then running :so.
The default location for your .vimrc file is ~/.vimrc (on the mac, ~ is /Users/_you_, eg /Users/matt). If you can write your file there, it will be loaded when vim starts up every time.
The system vimrc file on the mac is at /usr/share/vim/vimrc, but it sounds like these systems are not under your control, so you won't be able to write that file. Have a look at: How can I override ~/.vim and ~/.vimrc paths (but no others) in vim?, which uses the -u option to change the path vim looks for plugins under. But, in all cases, you must either be able to write your .vimrc into your home directory (which it sounds like your system does not permit) or specify the path to it at runtime (as with the command-line option I mention above, or with the normal command :so which you're currently using).
Depending on the kind of testing you need to do, you can always run shell commands within vim, by using the ! in normal mode. For instance, I frequently make changes to a python file in a buffer, and then (in normal mode) run !nosetests within vim—that writes terminal output into a temporary buffer at the bottom, and doesn't require me to leave or suspend vim. I can review the output, and any key-press takes me back to my buffer.
I don't have any experience with Mac-Os terminal. However I think if you could cp your .vimrc file to your home directory. everytime you start vim, vim will load the .vimrc file from your home directory.
If you want to swtich back to terminal from vim to do some testing/execute some commands and back to vim. you could consider to:
open multiple terminal windows
try something like screen or tmux. personaly I am using tmux, and it's very nice.
try Conque Shell plugin: http://www.vim.org/scripts/script.php?script_id=2771 I have this plugin installed too.
type Ctrl-z in vim to back to terminal
If you want to sync your .vimrc on different machines, you could put your .vimrc file in
a scm repository like gitHub, bitbucket... (I perfer this option, since you could have different branches for different settings)
dropbox
I hope this helps.
How is it possible that your changes are not saved after you log out? What would be the point of such a machine? An internet kiosk in an airport? Do you log as a user without a "home" directory?
If you have a "home" directory, just create a blank ~/.vimrc and put your settings there.
If you don't have a "home" directory but you are able to write somewhere else, create a blank vimrc file where you can, write your settings there and learn this command by heart:
$ vim -u /path/to/your/vimrc
If you don't have a "home" directory and you are really sure that you can't save anything on these machines, put your settings in a file somewhere online, preferably a place under your control, and learn this command by heart:
$ vim -u http://domain.name/yourvimrc
If you are lucky, the command you use will be remembered by your shell for you and it will be easy to issue it again without much typing.
For running your tests, you can either:
Hit <C-z> to suspend Vim. You are back at the prompt from where you started Vim and you can do your thing. Type $ fg to go back to Vim.
Type :sh to launch a new shell from the current directory. To go back to Vim, type $ exit.

Working with vim on multiple monitors

I have three monitors.
I normally run one maximized xterm on each monitor, attached to the same GNU screen session.
Can a similar model be used for vim? Is it possible to have three vims running, all sharing the same "vim session":
Each vim window showing a different vim tab
vim settings shared between all windows
Not getting the "Swap file ... already exists!" error message if I open the same file on two tabs.
I have a working solution where two vim instances communicate.
http://github.com/codeape2/vim-multiple-monitors
It uses the SwapExists autocmd to instruct the other instance to open a file if an existing swap file is detected.
You can use vim under screen.
$ screen
$ vim
# on another terminal
$ screen -x
# the same vim screen
If your terminal emulator supports tabbing (e.g. gnome-terminal), you can use it as a tabbing (IMHO, gnome-terminals' tabbing support is better than vim's own, except perhaps you can't copy and paste among different vim sessions, however you can instead use the system copy-paste buffer: "+y and "+p).
This does not work if you're using gvim though.
No, a Vim instance is limited to a single application window on your desktop. The different Vim instances have independent sessions.
In addition, be aware that if you open the same file in two different Vim instances, not only do you get the 'Swap file exists' message, but the two instances do not share a buffer, so changes made in one are independent of the other.
If a changed file is saved in one instance you will get a message when you return to the file in another Vim instance that 'the file has changed since editing started' and asking you if you want to reload the file (which would load changes as saved by the other instance, disregarding any changes you had made in the current instance).
Neovim is a project that aim to give vim attach/detach feature like tmux.
This feature isn't yet implemented (june 19th 2015), but this may come soon.
There is many other core features for this project that you can see at neovim.io.
:set noswapfile
:set autoread
Buffers aren't shared this way but vim reads file changes automatically if you save it. And you don't get that 'Swap file exists' message.

What's the best (Linux-compatible) buffer/session manager for Vim?

Is there a good project / session manager for vim? A session (or project) is a named lists of files, e.g. "bitonic_sort" could identify files "~/A/bitonic_sort.sk", "~/B/bitonic_sort.smt2", etc.
(rationale) I have a project where I need to edit files from many different locations, and it is too cumbersome to open them manually each time I resume work. (so, it looks like things like nerdtree brought up at this sister question, Favorite (G)Vim plugins/scripts?, won't work). I also need separate sessions (i.e. lists of files) for different projects, not just a recent document list.
After all documents have been loaded as buffers, any enhancements to switching between them is a plus (e.g. start typing a name, and matching documents are displayed). Thanks in advance.
Vim has built-in session manager. To save your current session use:
:mks session1.vim
This basically create a Vim script named session1.vim, which will restore your opened file if you source it or start Vim like this:
vim -S session1.vim
To overwrite your saved sessions, use :mks! your_saved_session.vim. Combine with a custom key map and this will be the solution. For more about Vim session read :help :mks. Vim also has views manager which is quite similar. Read more from: :help :mkview
For switching between buffers, you can use FuzzyFinder; but I prefer this key map:
nmap <C-tab> :bn<CR>
imap <C-tab> <ESC>:bn<CR>i
Add it to .vimrc and I can use Ctrl + Tab to switch between buffers just like Firefox tabs. Hope this help.
I'm not sure exactly what you're asking for... but if you want to turn on tab-completion when opening files in vim, add this to your ~/.vimrc:
" Auto-complete file names after <TAB> like bash does.
set wildmode=longest,list
set wildignore=.svn,CVS,*.swp
Also, take a look at screen. From the man page:
When screen is called, it creates a single window with a shell in it (or the specified command) and
then gets out of your way so that you can use the program as you normally would. Then, at any time,
you can create new (full-screen) windows with other programs in them (including more shells), kill
existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between
windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All
windows run their programs completely independent of each other. Programs continue to run when their
window is currently not visible and even when the whole screen session is detached from the user's
terminal. When a program terminates, screen (per default) kills the window that contained it. If
this window was in the foreground, the display switches to the previous window; if none are left,
screen exits.
It's pretty much like having several xterms open, except unlike graphical xterms you can access your screen session if you access your machine remotely (e.g. by sshing to it). You could leave up several different instances of vim in separate screens with all the files you want open, and just never exit them.
The very basic setup I use is one vim window, one compile window, and one testing/debugging window.
And since we're talking about vim, check out this post: Post your Vim config. Lots of cool tweaks and spiffy stuff in there.
You can try the vim-workspace plugin, its session management features are automated and relatively simple (compared to vim-session): https://github.com/thaerkh/vim-workspace

Resources