9

I use iterm in Mac as my command line terminal. In iterm I use tmux as the terminal manager. When I open my code files in Vim copying has become painful in this. To copy text in vim I need to hold "option" key and then select the text. When holding option there are multiple issues: 1) I am unable to scroll while in select mode 2) When I split my terminal into 2 panes, select using option copies across panes making it tough.

I am not sure about the reason for this issue and where to find a workaround. Could anyone help me with it?

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Kai
  • 953
  • 6
  • 16
  • 37
  • Try to use `macvim` for GUI, and `macvim -v` for CLI instead of `vim` (`brew install vim --override-system-vi`). Then put `set clipboard=unnamed` into the vim configuration file, and use the normal copying and pasting features (see `:help copy-move`) – Ruslan Osmanov Jan 23 '17 at 04:35
  • @RuslanOsmanov: Good addendum to my answer - it is probable that clipboard support is not baked into the default Vim. However, once you override system Vim, `vim` in terminal gives you terminal MacVim; not sure what `macvim -v` is, as `macvim` is not an existent executable. – Amadan Jan 23 '17 at 05:04

1 Answers1

11

You cannot depend on iTerm's clipboard support because it will not know anything about Vim's or tmux's splits. Use native Vim copy instead.

:help v
:help V
:help y
:help d
:help "*
:help clipboard

So e.g. in order to copy two lines, you can do "*2yy (to clipboard register, two line yank); or you can mark something using visual mode, then just "*y (to clipboard register, yank). If you want the clipboard register to be always automatically used unless another register is specified, you can add the following to your .vimrc:

set clipboard+=unnamed

Then a simple 2yy will copy two lines, and you can just paste it in iTerm or any other application.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 3
    There must be another step to get this to work in iTerm 2 because neither `"*yy` nor the `set clipboard` command will let me copy/paste from the clipboard in iTerm2 with Mac OS High Sierra. – LondonRob Jul 04 '18 at 15:09
  • 4
    @LondonRob: Do you have clipboard support in your Vim? (`:version`, then look for `+clipboard` or `-clipboard`). OSX default Vim doesn't have it compiled in, MacVim does, NeoVim does. – Amadan Jul 04 '18 at 15:35
  • Yes! This was my problem. I'll try and work out how to get these in terminal Vim. Thanks! – LondonRob Jul 04 '18 at 16:36
  • 1
    @LondonRob NeoVim and MacVim also are “terminal Vim”. MacVim _also_ has the GUI mode. But you can’t get this in default Vim. Best you can do in default Vim is piping into `pbcopy`. – Amadan Jul 04 '18 at 16:38
  • Brilliant! Thanks @Amadan, this fixed my problem! – codematix Mar 15 '22 at 08:43