Windows command prompt set clipboard without trailing newline

Viewed 73

I need a command that sets the contents of the clipboard to whatever is piped to its input without adding a trailing newline. The following:

C:> *some_command* | clip.exe

adds a newline at the end, while:

C:> *some_command* | powershell.exe Set-Clipboard

does not seem to pass the standard input to Set-Clipboard. Invoking *some_command* via a powershell prompt is not an option. How can I do what I need?

Background: I am using WSL and I want to be able to copy/paste to the system clipboard from vim, tmux, etc. My current workaround uses clip.exe which adds a newline to whatever I yank. For example, in .vimrc I have:

let g:copy = 'clip.exe'
let g:paste = 'powershell.exe Get-Clipboard -Raw'
augroup myYank
  autocmd!
  autocmd TextYankPost * if v:event.operator == 'y' | call system(g:copy, @") | let g:lastyank='y' | else | let g:lastyank='' | endif
  "autocmd TextYankPost * if v:event.operator ==# 'y' | call system(g:copy, @") | endif
  "autocmd TextYankPost * call system(g:copy, @")
augroup END
function! Paste(mode)
  if g:lastyank == 'y'
    let @" = system(g:paste)
  endif
  return a:mode
endfunction
map <expr> p Paste('p')
map <expr> P Paste('P')
" map Ctrl-c and Ctrl-x as expected
func! GetSelectedText()
  normal gv"xy
  let result = getreg("x")
  return result
endfunc
noremap <C-c> :call system(g:copy, GetSelectedText())<CR>
noremap <C-x> :call system(g:copy, GetSelectedText())<CR>gvx
1 Answers

output your command in a variable then use $var.trim() | Set-Clipboard, but the best would be to generate the proper output first rather than correcting it down the line so maybe the string builder could help you in such cases?

Related