Cutting and pasting from vim and tmux - vim

Is cutting and pasting from a vim session started from tmux completely broke?
If I want to cut from a github web into my vim session or vice versa, it rarely works or if it does, it is completely broke.
I have had others confirm that they find this difficult.
I am running on the following:
OS X 10.8.2
Vim 7.3
tmux 1.7
I am referring to the normal cut and paste commands Cmd + c and Cmd + v.
Are there any fixes or workarounds?

In Vim, you don't use Cmd+c/Cmd+v to "copy" and "paste": you use y/p, possibly with a register ("*y/"ap) to "yank" and "put".
If your Vim has clipboard support built-in, "*y should be enough to yank from Vim and "*p should be enough to put from another application. * is the "clipboard" register.
Again, supposing Vim has clipboard support, adding set clipboard^=unnamed to your ~/.vimrc should synchronize Vim's default register and the clipboard register; allowing you to simply use y˘and p.
The tmux/vim combo has a long standing issue with system clipboard on Mac OS X. It is fortunately very quick and easy to fix.
To see if your Vim build has clipboard support, type this command in your shell:
$ vim --version | grep clipboard
A + before a feature means "supports", - means "doesn't support".

just to add a little bit help.
getting vim with clipboard support might be helpful

Related

How to copy(External Sources) and paste on Vim [duplicate]

Unlike other editors, vim stores copied text in its own clipboard. So, it's very hard for me to copy some text from a webpage and paste it into the current working file. It so happens I have to either open gedit or type it manually.
Can I make vim paste from and to the system's clipboard?
Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard') returns 0. In this case, vim is not compiled with the +clipboard feature and you'll have to install a different version or recompile it. Some linux distros supply a minimal vim installation by default, but if you install the vim-gtk or vim-gtk3 package you can get the extra features nonetheless.
The "* and "+ registers are for the system's clipboard (:help registers). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OSX or Windows, the "* register is used to read and write to the system clipboard. On X11 systems both registers can be used. See :help x11-selection for more details, but basically the "* is analogous to X11's _PRIMARY_ selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+ is analogous to X11's _CLIPBOARD_ selection (which is the clipboard proper).
If all that went over your head, try using "*yy or "+yy to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.
You might like to remap this to something more convenient for you. For example, you could put vnoremap <C-c> "*y in your ~/.vimrc so that you can visually select and press Ctrl+c to yank to your system's clipboard.
You also may want to have a look at the 'clipboard' option described in :help cb. In this case you can :set clipboard=unnamed or :set clipboard=unnamedplus to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will override it.
To paste you can use "+p or "*p (again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>* or <C-r><C-p>+. See :help i_CTRL-R_CTRL-P.
It's also worth mentioning vim's paste option (:help paste). This puts vim into a special "paste mode" that disables several other options, allowing you to easily paste into vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste to enable it, paste your content and then type :set nopaste to disable it.) Alternatively, you can use the pastetoggle option to set a keycode that toggles the mode (:help pastetoggle).
I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your vim chops.
See :help clipboard for more detailed information.
You can paste into vim by gnome-terminal's shortcut for paste.
Place the file in insert mode and use
Ctrl+Shift+v.
Remember beforehand to
:set paste
to avoid messing with the indentation.
I believe that this question deserves a more objective and pictorial answer:
Entering Paste Mode
ESC
:set paste
press i
SHIFT + Insert (with a text copied on your clipboard)
Leaving Paste Mode
ESC
:set nopaste
press i
You pasted the text and you're able to type again.
For my that configuration works for copying and pasting
" copy and paste
vmap <C-c> "+yi
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <ESC>"+pa
Linux
On my Linux system, the + and * registers map to an X11 selection, which can be pasted with the middle mouse button. When :set clipboard=unnamed and :set clipboard=unnamedplus are used, then the registers map to the clipboard, and can be pasted with CTRL-V.
The specifics seem to be somewhat configuration and system dependent, so your mileage will definitely vary. It should definitely get you pointed in the right direction, though.
See Also
http://vim.wikia.com/wiki/Accessing_the_system_clipboard
This would be the lines you need in your vimrc for this purpose:
set clipboard+=unnamed " use the clipboards of vim and win
set paste " Paste from a windows or from vim
set go+=a " Visual selection automatically copied to the clipboard
clipboard
There is a special register for storing this selection, it is the "*
register. Nothing is put in here unless the information about what text is
selected is about to change (e.g. with a left mouse click somewhere), or when
another application wants to paste the selected text. Then the text is put
in the "* register. For example, to cut a line and make it the current
selection/put it on the CLIPBOARD:
"*dd
Similarly, when you want to paste a selection from another application, e.g.,
by clicking the middle mouse button, the selection is put in the "* register
first, and then 'put' like any other register. For example, to put the
selection (contents of the CLIPBOARD):
"*p
registers E354
> There are nine types of registers:
> 1. The unnamed register ""
> 2. 10 numbered registers "0 to "9
> 3. The small delete register "-
> 4. 26 named registers "a to "z or "A to "Z
> 5. four read-only registers ":, "., "% and "#
> 6. the expression register "=
> 7. The selection and drop registers "*, "+ and "~
> 8. The black hole register "_
> 9. Last search pattern register "/
Paste from clipboard
1. Clipboard: Copy
2. Vim insertmode, middle mouse key
Check for X11-clipboard support in terminal
When you like to run Vim in a terminal you need to look for a version of Vim that was compiled with clipboard support.
Check for X11-clipboard support, from the console, type:
% vim --version
If you see "+xterm_clipboard", you are good to go.
http://vim.wikia.com/wiki/Accessing_the_system_clipboard
The X server maintains three selections, called:
PRIMARY, SECONDARY and CLIPBOARD
The PRIMARY selection is conventionally used to implement copying and
pasting via the middle mouse button. The SECONDARY and CLIPBOARD
selections are less frequently used by application programs.
http://linux.die.net/man/1/xsel
Copy To OS Clipboard
Select text in visual mode, press "*y
Paste From OS Clipboard
Press "*p
Didn't have +clipboard so I came up with this alternative solution using xsel:
Add to your ~/.vimrc:
vnoremap <C-C> :w !xsel -b<CR><CR>
Shift + Right Click -> Paste
did the trick for me
I tried the suggestions above and none of them worked in my environment. (Windows PuTTY clone over ssh)
Some additional googling turned up:
https://unix.stackexchange.com/questions/110684/copy-paste-into-sshd-vim-from-local-windows-clipboard
One of the comments suggested using SHIFT+INSERT which did the trick for pasting from my desktop's clipboard into Vim's buffer. Ctrl-C was already working to copy to the desktop's clipboard from Vim.
The simplest solution to this, that also works between different Linux machines through ssh is:
Check whether vim supports X-11 clipboard: vim --version | grep clipboard. If it reports back -clipboard and -xterm_clipboard you should install either vim-gtk or vim-gnome (gvim on arch linux)
Add the following lines to your .vimrc:
set clipboard=unnamedplus
set paste
If you login on a different machine via ssh, use the option -Y: ssh -Y machine
Now copying and pasting should work exactly as expected on a single, and across different machines by only using y for yank and p for paste. NB modify .vimrc on all machines where you want to use this feature.
What simply worked for me in Linux (Ubuntu 20.04)
Copying to system clipboard:
Select what you want to copy in Visual mode.
Type "+y
Press Enter
Paste something form system's clipboard:
Move to the place where you want to paste the copied text in vim.
Just type
"+P to paste before cursor OR
"+p to paste after cursor.
To know more how this works: Copy and Paste to/from Vim from/to Other Programs!
A quick note for people whose Vim installation does not support the * and + registers. It is not necessary to download a new Vim installation to paste from the clipboard. Here is an alternative method:
Install parcellite (a clipboard manager with a low memory footprint);
In your .vimrc file, add the following:
command Clip r !parcellite -c
Restart vim.
Now when you type in :Clip as an ex command, the contents of the clipboard will be pasted in at the cursor. You can also map the new command to a function key so as to be able to do this with one keystroke.
These key combinations work on any OS.
Select target Text using the mouse, and refer to the key sequences to copy, cut, and paste.
copy: Ctrl + Insert
paste: Shift + Insert
cut: Shift + Del
paste: Shift + Insert
This works for me: Ctrl+Shift+V
If you are using a mouse first do
:set paste
Then right click mouse and the contents in buffer will be pasted
I ran into this issue on a mid-2017 Macbook Pro running vim within iTerm2 as my primary development environment.
As other answers have suggested, I ran vim --version and noticed that it returns -clipboard, which means that the version of vim that shipped with my machine hasn't been compiled with the clipboard option.
The homebrew package for vim appears to compile with the clipboard option, so the fix for me was to:
Run brew install vim
Add set clipboard+=unnamed to my ~/.vimrc file
Close and reopen iTerm2
Following on from Conner's answer, which was great, but C-R C-p + and C-R C-p * in insert mode is a bit inconvenient. Ditto "*p and "+p from command mode.
a VIM guru suggested the following to map C-v to what C-r C-p + does.
You could have :inoremap <C-v> <C-o>"+p for insert mode only
if you really wanted to override blockwise visual mode (not recommended by him as visual
mode is good) you could have map <C-v> "+p
With Vim 8+ on Linux or Mac, you can now simply use the OS' native paste (ctrl+shift+V on Linux, cmd+V on Mac). Do not press i for Insert Mode.
It will paste the contents of your OS clipboard, preserving the spaces and tabs without adding autoindenting. It's equivalent to the old :set paste, i, ctrl+shift+V, esc, :set nopaste method.
You don't even need the +clipboard or +xterm_clipboard vim features installed anymore. This feature is called "bracketed paste". For more details, see Turning off auto indent when pasting text into vim
If you are using vim in MAC OSX, unfortunately it comes with older verion, and not complied with clipboard options. Luckily, homebrew can easily solve this problem.
install vim:
brew install vim --with-lua --with-override-system-vim
install gui verion of vim:
brew install macvim --with-lua --with-override-system-vim
restart the terminal to take effect.
append the following line to ~/.vimrc
set clipboard=unnamed
now you can copy the line in vim with yy and paste it system-wide.
If you have it, try removing this from your vimrc:
set mouse=a
It messes with the paste functionality.
On top of the setting :set clipboard=unnamed, you should use mvim -v which you can get with brew install macvim if you're using vim on Terminal.app on Mac OS X 10.9. Default vim does not support clipboard option.
Based on #lis2 answer, I use a simpler configuration that will not force Insert mode at the end:
" Copy and paste
if has('clipboard') && !has('gui_running')
vnoremap <C-c> "+y
vnoremap <C-x> "+d
vnoremap <C-v> "+p
inoremap <C-v> <C-r><C-o>+
endif
Mind that all these override default Vim mappings:
v_CTRL-C: stop Visual mode
v_CTRL-X: subtract [count] from number
v_CTRL-V: blockwise Visual mode
i_CTRL-V: insert next non-digit literally, which is also mapped to i_CTRL-Q
As an alternative, one can use keys inspired in the "yank", "delete" and "put" Vim verbs: <C-y>, <C-d> and <C-p> respectively. These would only override one default mapping:
i_CTRL-P: backwards search keyword for completion
The other solutions are good if you want to change your vimrc, etc... However I wanted an simple way to copy from vim to my system keyboard. This is what I came up with.
Select the text you want to copy with visual mode v
Press : (it will automatically expand to show :'<,'>)
Type y * or y + (depending on your system) to yank the selected text to the system clipboard
Since vim 8 right click enables visual mode by default. This prevents the "normal" copy & paste (call it a "defect by design" https://github.com/vim/vim/issues/1326).
Fix it by doing:
echo "set mouse-=a" >> ~/.vimrc .
Exit and restart vim.
It may also be worth mentioning, on OSX using Vim, you can select text with the mouse, Cmd-C to copy to OSX system clipboard, and the copied text will be available in the clipboard outside of Vim.
In other words, OSX treats it like it were a regular window, and this is where the much-maligned Apple "Command" button comes in handy.
B-30
There are two simple ways to do this. Make your file in insert mode and
1) press the middle button (the scroll wheel) in your mouse, or
2) Ctrl + Shift + V
What you really need is EasyClip. It will do just that and so much more...
After entering the vim window, press I to enter into insert mode. Then move your cursor to the desire location and press Ctrl + Insert button simultaneously to paste from the clipboard.

Copying text outside of Vim with set mouse=a enabled

After enabling set mouse=a, text copied inside of Vim will not paste outside of Vim. Does anybody know of a way to fix this?
Here, selecting text with the mouse turns on visual mode and disables the Copy option in the popup menu:
Press shift while selecting with the mouse. This will make mouse selection behave as if mouse=a was not enabled.
Note: this trick also applies to "middle button paste": if you want to paste in vim text that was selected outside, press shift while clicking the middle button. Just make sure that insert mode is activated when you do that (you may also want to :set paste to avoid unexpected effects).
OS X (mac):
hold alt/option while selecting (source)
Use ", +, y after making a visual selection either with the keyboard or the mouse. You shouldn’t be using the terminal’s copy command anyway, because that copies what the terminal sees instead of the actual content. Here is what this does:
",+ tells Vim to use the register named + for the next delete, yank or put. The register named + is a special register, it is the X11 clipboard register. (On other systems, you would use * instead, I think, see :help clipboard and :help x11-selection)
y is the yank command, which tells Vim to put the selection in the register named previously.
You could map it like this:
:vmap <C-C> "+y
And then highlight something with the mouse and press Control-C to copy it.
This feature only works when Vim has been compiled with the +xterm_clipboard option. Run vim --version to find out if it has.
Instead of set mouse=a use set mouse=r in .vimrc
On OSX use fn instead of shift.
In Ubuntu, it is possible to use the X-Term copy & paste bindings inside VIM (Ctrl-Shift-C & Ctrl-Shift-V) on text that has been hilighted using the Shift key.
Another OSX-Mac option is to uncheck View->Allow Mouse Reporting (or press ⌘-R to toggle it.) This allows you to toggle between mouse interaction and mouse selecting, which might be useful when selecting and copy/pasting a few bits because you don't have to hold a modifier key to do it.
Note for Multiline with line numbers:
I usually have line numbers enabled so this will also copy the line numbers if you select multiple lines. If you want to copy multiple lines without the line numbers disable the numbers with :set nonu and then you can :set nu to re-enable them after you're done copying.
Holding shift while copying and pasting with selection worked for me
You can use :set mouse& in the vim command line to enable copy/paste of text selected using the mouse. You can then simply use the middle mouse button or shiftinsert to paste it.
I accidently explained how to switch off set mouse=a, when I reread the question and found out that the OP did not want to switch it off in the first place. Anyway for anyone searching how to switch off the mouse (set mouse=) centrally, I leave a reference to my answer here: https://unix.stackexchange.com/a/506723/194822
Compilation settings that vim was compiled with, are part of the issue. vim --version shows these.
In OSX, the default vim has -clipboard But you need +clipboard
On osx you can and apparently generally should, use macvim. You can do brew cask install macvim That one has +clipboard.
Them you'll have two vims.
~$ ls -l /usr/bin/vim <--- default vim
-rwxr-xr-x 1 root wheel 1745984 15 Jul 2017 /usr/bin/vim
~$ ls -l /usr/local/bin/vim <-- macvim, installed recently via that mentioned brew line.
lrwxr-xr-x 1 apple admin 42 16 May 23:32 /usr/local/bin/vim -> /Applications/MacVim.app/Contents/bin/mvim
~$
running vim will run macvim 'cos /usr/local/bin should be before /usr/bin in the path, though you can check with which vim.
running vim(to run macvim), is fine but you may want to map vi to macvim 'cos otherwise running vi stays at the default vim! You can rewrite or delete(with rm) and recreate the vi sym link, with ln. And to do that without an 'operation not permitted" error, you have to (temporarily) disable SIL. https://apple.stackexchange.com/questions/208478/how-do-i-disable-system-integrity-protection-sip-aka-rootless-on-macos-os-x .
macvim has +clipboard as shown by vim --version
Here is a working ~/.vim/vimrc with just the required lines.
:set mouse=a
:map <leader>c "+y
:map <leader>v "+p
The default leader key is backslash.
I read a suggestion that one should use the leader key.. (certainly control has many keys already in use, so the suggestion was to not use control. I don't know if that applies to command key too, but anyhow).
With that mentioned mapping, \c will do "+y which will copy from the register known as +, to the clipboard. And \v will paste from the register known as +.
So that's a copy/paste that works between windows.
Another OS may require "* rather than "+
Add set clipboard=unnamed to your .vimrc. So it will use the clipboard register '*' instead of the unnamed register for all yank, delete, change and put operations (note it does not only affect the mouse).
The behavior of register '*' depends on your platform and how your vim has been compiled (or if you use neovim).
If it does not work, you can try with set clipboard=unnamedplus, but this option only makes sense on X11 systems (and gvim therefore).
If you are using, Putty session, then it automatically copies selection.
If we have used "set mouse=a" option in vim, selecting using Shift+Mouse drag selects the text automatically.
Need to check in X-term.
em...
Keep pressing Shift and then click the right mouse button
Also worth mentioning, by having set mouse=nvi, when doing a selection and then pressing : <ESC> you will get the mouse selection copied to the primary selection clipboard (equivalent to a "*y).
Reference: help mouse
Main advantage of this method is the fact that if you have multiple vertical splits, it will only select from the current buffer. Using <Shift> as mentioned in the main answer, will, in this case, copy from all 3 files at the same time which is not exactly what one would want, expect or need.
A good workaround which is worth adding:
GPM daemon can be used which is a a cut and paste utility and mouse server for virtual consoles. It will provide functionalities across all the virtual consoles!
Copy-Paste actions can be done by <CTRL-C>/<CTRL-V>.
sudo apt-get install gpm
MAN pages of GPM
set set mouse=a in vi, using MobaXterm, after installing vim-gtk3 on server, dragging with mouse and Ctrl + Insert works, but seems it only work with MobaXterm
after installing vim-gtk3, vi will link to it
lala#kubu:~$ sudo apt install gvim
[sudo] password for lala:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package gvim is a virtual package provided by:
vim-gtk3 2:8.2.2434-3ubuntu3.2
vim-athena 2:8.2.2434-3ubuntu3.2
You should explicitly select one to install.
E: Package 'gvim' has no installation candidate
lala#kubu:~$ which vi
/usr/bin/vi
lala#kubu:~$ file /usr/bin/vi
/usr/bin/vi: symbolic link to /etc/alternatives/vi
lala#kubu:~$ file /etc/alternatives/vi
/etc/alternatives/vi: symbolic link to /usr/bin/vim.gtk3
lala#kubu:~$
In ESC mode, when set mouse=a, select the text using mouse. This would enable the visual mode in vim. Then you can press 'y' to yank the selected text and 'p' to paste it wherever you want. This happens only within vim.

How do I run a terminal inside of Vim?

I am used to Emacs, but I am trying out Vim to see which one I like better.
One thing that I like about Emacs is the ability to run a terminal inside Emacs. Is this possible inside of Vim? I know that you can execute commands from Vim, but I would like to be able to run a terminal inside of a tab.
Outdated from August 2011
Check out Conque Shell (also on GitHub). Lets you run any interactive program inside vim, not just a shell.
I'm not sure exactly what you're trying to achieve (I've never used Emacs), but you can run commands in Vim by typing:
:! somecommand [ENTER]
And if you want to type in several commands, or play around in a shell for a while, you can always use:
:! bash (or your favourite shell) [ENTER]
Once the command or shell terminates, you'll be given the option to press Enter to return to your editor window
Vim is intentionally lightweight and lacking in the ability to do non-editorish type things, just as running a full-blown shell inside a Vim pane/tab, but as mentioned above there are third-party addons such as vim-shell that allow you to do that sort of thing.
Typically if I want to switch between Vim and my shell (Bash), I just hit CTRL+Z to pause the Vim process, play around in my shell, then type 'fg' when I want to go back to Vim - keeping my editor and my shell nice and separate.
Updated answer (11 years later...):
I would recommend using tmux instead of screen as suggested in the original answer below, if you choose to use that solution.
Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.
I would definitely recommend screen for something like this. Vim is a text editor, not a shell.
I would use Ctrl+AS to split the current window horizontally, or in Ubuntu's screen and other patched versions, you can use Ctrl+A|(pipe) to split vertically. Then use Ctrl+ATab (or equivalently on some systems, Ctrl+ACtrl+I which may be easier to type) to switch between the windows. There are other commands to change the size and arrangement of the windows.
Or a less advanced use of screen is just to open multiple full-screen windows and toggle between them. This is what I normally do, I only use the split screen feature occasionally.
The GNU Screen Survival Guide question has a number of good tips if you're unfamiliar with its use.
The way that I get around this is:
pause Vim with Ctrl + Z,
play in the terminal,
then return to exactly where you left with Vim by just typing the command fg.
If enabled in your version of Vim, a terminal can be started with the :term command.
Terminal window support was added to Vim 8. It is an optional feature that can be enabled when compiling Vim with the +terminal feature. If your version of Vim has terminal support, :echo has('terminal') will output "1".
Entering :term will place you in Terminal-Job mode, where you can use the terminal as expected.
Within Terminal-Job mode, pressing Ctrl-W N or Ctrl-\ Ctrl-N switches the mode to Terminal-Normal, which allows the cursor to be moved and commands to be ran similarly to Vim's Normal mode. To switch back to Terminal-Job mode, press i.
Other answers mention similar functionality in Neovim.
:sh then Ctrl+D to get back in (bash)
Update:
You could map Ctrl+D in vim to run :sh, which allows you to toggle between bash and vim quickly.
noremap <C-d> :sh<cr>
The main new feature of Vim 8.1 is support for running a terminal in a Vim window.
:term will open the terminal in another window inside Vim.
:term
Added in Vim 8.1.
Keep in mind that whenever a terminal window is active, most keystrokes will simply be passed to the terminal instead of having their usual functions. Ctrl-W and its subcommands are the main exception. To send a literal ^W input to the terminal, press Ctrl-W .. You can also open the Vim : command line by pressing Ctrl-W :. The other Ctrl-W commands work as normal, so managing windows works the same no matter what type of window is currently selected.
Eventually a native :terminal command was added to vim in 2017.
Here is an excerpt from the :terminal readme:
This feature is for running a terminal emulator in a Vim window. A
job can be started connected to the terminal emulator. For example, to
run a shell:
:term bash
Or to run build command:
:term make myprogram
The job runs asynchronously from Vim, the window will be updated to
show output from the job, also while editing in another window.
This question is rather old, but for those finding it, there's a new possible solution: Neovim contains a full-fledged, first-class terminal emulator, which does exactly what ConqueTerm tried to. Simply run :term <your command here>.
<C-\><C-n> will exit term mode back to normal-mode. If you're like me and prefer that escape still exit term mode, you can add this to your nvimrc:
tnoremap <ESC><ESC> <C-\><C-N>
And then hitting ESC twice will exit terminal mode back to normal-mode, so you can manipulate the buffer that the still-running command is writing to.
Though keep in mind, as nvim is under heavy development at the time I'm posting this answer, another way to exit terminal mode may be added. As Ctrl+\Ctrl+n switches to normal mode from almost any mode, I don't expect that this answer will become wrong, but be aware that if it doesn't work, this answer might be out of date.
https://github.com/neovim/neovim
I know that I'm not directly answering the question, but I think it's a
good approach. Nobody has mentioned tmux (or at least not as a
standalone answer). Tmux is a terminal multiplexor like screen. Most
stuff can be made in both multiplexors, but afaik tmux it's more easily
to configure. Also tmux right now is being more actively developed than
screen and there's quite a big ecosystem around it, like tools that help
the configuration, ecc.
Also for vim, there's another plugin: ViMUX, that helps a lot in
the interaction between both tools. You can call commands with:
:call VimuxRunCommand("ls")
That command creates a small horizontal split below the current pane vim
is in.
It can also let you run from a prompt in case you don't want to run the
whole command:
<Leader>vp :VimuxPromptCommand<CR>
As it weren't enought, there are at least 6 'platform specific plugins':
vim-vroom: runner for rspec, cucumber and test/unit; vimux support via g:vroom_use_vimux
vimux-ruby-test: a set of commands to easily run ruby tests
vimux-cucumber: run Cucumber Features through Vimux
vim-turbux: Turbo Ruby testing with tmux
vimux-pyutils: A set of functions for vimux that allow to run code blocks in ipython
vimux-nose-test: Run nose tests in vimux
Here is a nice "use case": Tests on demand using Vimux and Turbux with Spork and Guard
Someone already suggested https://github.com/Shougo/vimshell.vim, but they didn't mention why. Consequently, when I came away from this question I wasted a lot of other time trying the other (much higher ranked) options.
Shougo/vimshell is the answer. Here's why:
In addition to being a terminal emulator, VimShell allows you to navigate through terminal output in normal and visual mode. Thus, if a command you run results in output that you'd like to copy and paste using the keyboard only...VimShell covers this.
None of the other options mentioned, including the :terminal command in NeoVim do this. Neovim's :terminal comes close, but falls short in at least the following ways as of 2/18/2017:
Moves the cursor to the end of the buffer, instead of at the last keeping it in the same spot like VimShell does. Huge waste of time.
Doesn't support modifiable = 1 see a discussion on this at Github, so helpful plugins like vim-easymotion can't be used.
Doesn't support the display of line numbers like Vimshell does.
Don't waste time on the other options, including Neovim's :terminal. Go with VimShell.
It's possible to open a new tab with a terminal in vim since 2017 as #fjardon said:
Just type: :terminal. It will open a tab by default above your current tab.
If you want it to open in another place you can try the following options:
:below terminal : open the terminal below current tab.
:below vertical terminal : open the terminal always vertically to the right.
You can play with these until you find what you like. After this you can set a map in your .vimrc configuration file, for me, I use:
nmap <leader>tt :below vertical terminal<CR>
This way I can type <space>tt (space my leader key) to open it quickly.
As a side note:
You can switch between your tabs (terminal and other buffers) with Ctrl+W Ctrl+W.
You can enter an editable mode in your terminal if you want to copy your commands with Ctrl+W N and go to normal terminal mode with i or a.
Cheers!
You might want to take a look at the :sh command (see :help sh in Vim).
Various commands
No, you cannot:
http://vimdoc.sourceforge.net/htmldoc/tips.html#shell-window
By far, I have tried a lot of solutions mentioned here, what I really wanted is to keep the terminal open while coding a similar experience in VsCode. Then I came across this solution which is working perfectly for me.
Before Installing:
I am using Nvim 0.5 but I think it can work for any version and checked also on vim
I am using macOS Catalina Version 10.15.7
Setup your integrated terminal
Step -1-
Create a script with the name myQuickTerminal.vim or whatever name you want.
Put the following script
"==============================================================================
"
" ▒█▀▀█ █░░█ ░▀░ █▀▀ █░█   ▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ░▀░ █▀▀▄ █▀▀█ █░░
" ▒█░▒█ █░░█ ▀█▀ █░░ █▀▄   ░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▀█▀ █░░█ █▄▄█ █░░
" ░▀▀█▄ ░▀▀▀ ▀▀▀ ▀▀▀ ▀░▀   ░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▀▀▀ ▀░░▀ ▀░░▀ ▀▀▀
"
"==============================================================================
" " This is a script that will trigger a terminal quickly than the FloatTerminal
" open new split panes to right and below
"link: https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
"==============================================================================
set splitright
set splitbelow
" turn terminal to normal mode with escape
tnoremap <Esc> <C-\><C-n>
" start terminal in insert mode
au BufEnter * if &buftype == 'terminal' | :startinsert | endif
" open terminal on ctrl+n
function! OpenTerminal()
split term://zsh
resize 10
endfunction
nnoremap <leader> n :call OpenTerminal()<CR>
NOTE: if you want to run bash instead of zsh for a particular reason then replace zsh with bash.
Step -2-
Lets source it, put this in init.vim for neovim or `.vimrc' for vim
source $HOME/.config/nvim/modules/mySpecialScripts/myQuickTerminal.vim
This will be preloaded ahead as you save and resource it, you can use source $MYVIMRC for quick reloading the init.vim file.
Step -3-
I mapped as you can see in the script n to open a terminal in a new pane, my is the (Space bar) and once I click (space + n) a terminal will be triggered and I will enjoy writing my code while the terminal is opened.
To quit insert mode in the terminal, press Esc.
Now, to switch to the code editor pane, use CTRL+w w. This shortcut can get annoying once you have more than two panels open, so I added the following shortcuts too.
I mapped these too for quick jumping among opened panes, use these
" Better window navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Optional
If you want your terminal to exit the current buffer with key. you can use
tnoremap <Esc> <C-\><C-n>:q!<CR>
But since I want to switch in between several buffers I use without close the terminal I use instead:
tnoremap <Leader><Esc> <C-\><C-n>:q!<CR>
Final results
Reference:
https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
Only way I know of is by using vim-shell, a third-party patch.
I use this now, you may can try. VimShell
Split the screen and run command term ++curwin to run the terminal inside the Vim buffer. Following command does both and worked for me:
:bo 10sp | term ++curwin
If you are interested in quick answer, here is it: :vert term. It will split your screen vertically and open up terminal.
Try vterm, which is a pretty much full feature shell inside vim. It is slightly buggy with its history and clear functions, and still in development, but it still is pretty good
Assuming your version of vim supports +term command first, set shell for vim to use in one command (e.g. set=/usr/bin/zsh), and then run the command +term (i.e. bo 15vs +term). you may have to do some additional maneuvering of your windows (i.e. deleting one and rotating), but you'll have your terminal.
With vim 8.1.3741, just type :terminal to start a terminal inside of vim.
Try map :nnoremap ]t :terminal<CR> to do that quicker!
I acknowledge that I am not strictly answering your question, but what has worked better for me when using Vim and Terminals in the same window is Tmux (which is kind of a "run in the background software" like, similar to screen, although this one works better with splits and tabs).
This post will help you to understand how they work together: 'Tmux and Vim — even better together'.
This way we can convert Vim into a powerful IDE

copying text from vim to windows

I am trying to copy a selection of text from vim to another windows application...say firefox or notepad for example. However I can't seem to do this with the y command. I have windows hotkeys enabled so by pressing ctrl-c I can achieve this but was hoping to get rid entirely of these windows commands in Vim.
Is this possible? also what about vice versa copying from windows and pasting into vim
I have
set clipboard=unnamed
in my ~/.vimrc. Then "yy", "D", etc, yank directly to the Windows clipboard. It also works in MacVim. For Linux gvim, you have to remember to prefix these operations with "+
I don't see any harm in using Windows command keys in GVim. Alternatively, you can also use the hotkey "+y for yanking (copying) and "+p for pasting to and from the system clipboard. This works on most platforms (Vim instances that are not directly attached to an X server on unix are a bit more difficult).
as per above answers:
"*y
"*p
Using ctrl-v will make entering control characters tough in insert mode, and ctrl-v is visual-block in normal mode, although so is ctrl-q.
Personally have found the following quite natural & little finger friendly
vmap <a-c> "*y
imap <a-p> "*p
Don't use windows bindings as you then won't be able to be as good in Vim on other platforms.
Use "*y - copy to system clipboard. It works well on Mac Os, Windows, Linux.
If you want to copy from a window (or from vim), try :
:set paste
But be careful other options may be modified. Everything is detailed in :
:help paste
Hope it helps.
For Windows 7, you need to edit the vim config file at:
C:\users\<User>\vimfiles\vimrc
just add the following line to the config file:
set clipboard=unnamed
Save the file, open it in another editor that uses standard Windows conventions, then copy and paste as you usually would.

Why is pasting a long one-liner very slow in Vim's insert mode?

My Macbook was stuck yesterday, when I tried to paste 1200 lines of 80 characters to Vim. It was much faster to download the file, and not to paste the text.
I have thought that this problem might be the reason, why internet operators allow slower uploading than downloading.
If you paste it into a terminal window, Vim thinks you're typing it out by hand, and it will try and update the display as you go. You can access your clipboard (on OS X) using the pbpaste and pbcopy commands, so you can just do this in Vim:
:read !pbpaste
or in a shell:
bash$ pbpaste | vim -
If you were using GUI Vim, you would use the "* register to paste (this is what the context menu does):
"*P <- in normal mode
Pasting into the terminal window is usually a bad idea, try and use pbpaste where you can.
:read !pbpaste
If you are using Linux use:
xsel --clipboard --output
or:
xclip -selection clipboard -o
instead of pbpaste.
That is "normal". It's slow because redrawing the text thousands of times is slow.
As you paste the long line in, it's constantly update the display (because of how vim deals with text, or how the terminal is handing vim text, I guess).
I tried pasting the text in vim (using iTerm) and it has the same issue, it takes a while to paste. I tried :set paste and :set nowrap and still as slow. Pasting the line straight into a terminal is equally slow
With the dpaste link you mention, there is a plain-text link, which you could just wget and edit:
curl http://dpaste.com/115362/plain/ | vim -
I favor set paste/nopaste like Masi suggested.
In .vimrc, you can map some character to toggle paste (if often needed).
i.e.
set pastetoggle=§
did you try paste mode? set paste / set nopaste?
if you :syntax off you can sometimes improve an in place paste of a long single line file. An example would be a machine generated xml file.
you can probably disable vim's redraw whilst pasting as well, look at :he redraw , but it's always worth using command line stuff as If you are repeating the procedure or similar you can always automate it with a script / vim macro
I don't know if this is a Mac issue or something else, but I have no problems whatsoever with pasting that amount of text in Vim. I have tried on Windows and Linux, and haven't seen any problems.
I have successfully edited files of several hundred megs (log files) in Vim (loading is slow, but once the text is read everything is pretty snappy).
But if it's on the web, you should have tried:
:e http://link/to/file
Then if necessary save it as a local file.
And if it's slow because of the redrawing, look at this option:
*'lazyredraw'* *'lz'* *'nolazyredraw'* *'nolz'*
'lazyredraw' 'lz' boolean (default off)
global
{not in Vi}
When this option is set, the screen will not be redrawn while
executing macros, registers and other commands that have not been
typed. Also, updating the window title is postponed. To force an
update use |:redraw|.
And if it's a local file, then pasting is not necessary: try
:read file
instead.
If you use Apple Terminal try an other terminal, like iTerm.
Sometimes, the "build-in" terminal is not really reactive for common task. Don't know why...

Resources