Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active November 25, 2021 15:42
Show Gist options
  • Save YumaInaura/6585e10fd96dcf1b0088f20c40cd448e to your computer and use it in GitHub Desktop.
Save YumaInaura/6585e10fd96dcf1b0088f20c40cd448e to your computer and use it in GitHub Desktop.
Vim—Edit clipboard text directly ( Example using Mac OS pbcopy / pbpaste )

Vim—Edit clipboard text directly ( Example using Mac OS pbcopy / pbpaste commands )

Ease way

Put blow line in your rc file ( e.g ~/.bashrc ~/.zshrc )

Restart your shell and hit pbvim.

function pbvim (){
  readonly pbvim_tmp_file=./.pbvim.tmp

  pbpaste | vim - +"w $pbvim_tmp_file"
  cat "$pbvim_tmp_file" | tr -d "\012"| pbcopy

  rm -f "$pbvim_tmp_file"
}

ccc/pbvim at master · YumaInaura/ccc

Steps

Step. Read text from STDIN by VIM

pbpaste | vim -

But in above pattern VIM has no filename so can not save edit.

Step. Give some filepath to VIM

pbpaste | vim - +"w some_filepath"

This shell command execute "vim command" as :w some_filepath immediately before editing.

So you will able to save editing in some_filepath.

But vim does not send STDOUT edited text.

Step. Use temporary file and STDOUT

  1. Touch temporary file
  2. Send pbpaste STDIN to VIM
  3. Save edited text to temporary file
  4. STDOUT from edited temporary file and STDOUT to pbcopy
  5. Remove temporay file

This is a first example of this article.

Versions

  • VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 7 2018 21:58:18)
  • Max OS X High Sierra

Ref

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment