What version of bat are you using?
bat 0.12.1Windows 10 1809Describe the bug you encountered:
bat can't read the content of /dev/clipboard inside git bash/MSYS2
$ bat /dev/clipboard
[bat error]: '/Device/Null': The system cannot find the path specified. (os error 3)
Reading the path with cygpath didn't help since it displays a NUL path
$ cygpath.exe --windows /dev/clipboard
\\.\NUL
$ bat "$(cygpath.exe --windows /dev/clipboard)"
[bat error]: Incorrect function. (os error 1)
Describe what you expected to happen?
Behave just as cat and read the system clipboard
How did you install bat?
Downloaded bat-v0.12.1-x86_64-pc-windows-msvc.zip from github releases
Thank you for reporting this. Unfortunately, I don't have an environment to test this.
Could you try to run bat -pp /dev/clipboard? Maybe it's just a problem with showing the path in the header.
How do other programs (besides cat) react to this path? Maybe less, vim, or a GUI editor?
What does
ls -alh /dev/clipboard
show?
MSYS2 is based on Cygwin, right? If so, you're probably running into an interoperability issue between the Cygwin compatibility layer and the native Windows bat binary.
Non-cygwin executables (like bat) aren't able to access common *NIX paths like /dev or /proc, and cygpath isn't able to translate them to a Windows path because they don't have a direct equivalent within the Windows filesystem. A more familiar example would be like trying to run something like notepad.exe /dev/clipboard from cmd or the run dialog.
As a workaround, could you try the following?
bat < /dev/clipboard
In theory, that should make Cygwin-aware Bash redirect the contents of the /dev/clipboard to bat's stdin.
$ bat -pp /dev/clipboard
[bat error]: '/Device/Null': The system cannot find the path specified. (os error 3)
$ ls -alh /dev/clipboard
crw-rw-rw- 1 mochoa 1049089 13, 254 Nov 30 2006 /dev/clipboard
notepad failed to open /dev/clipboard less -f did show the system clipboardVim (the one bundle with MSYS2) is able to open it as an empty file, I'm able to make changes and it did reflect in the system clipboard after writing the filebat < /dev/clipboard shows nothing, and cat < /dev/clipboard did show the clipboardgrep, tail, head, echo 'str' > /dev/clipboardNeovim and Vim (both compiled from MSVS) failed to write the fileOkay. That mostly lines up with the behavior I was expecting (except MSVS vim). That is really weird that bat can't handle the FD redirection, though. Maybe it's implemented differently under the hood than I anticipated?
Just to rule out anything else, could you try:
cat /dev/clipboard | bat
Yep, cat /dev/clipboard | bat works as expected
Sorry for the late reply. Considering that bat isn't really native to MSYS2, I'm not entirely sure how this can be solved. Maybe a MSYS2 build of bat if that's even possible?
I had a bit of time to experiment with this recently. /dev/clipboard is considered a character device, which is something that only exists inside the MSYS2 environment. It seems that the only way to get its contents from non-MSYS2 executables is to pipe it through STDIN like cat /dev/clipboard | other_program or other_program < <(cat /dev/clipboard).
I still can't think of any solution to this, but maybe a function inside the git-bash equivalent of a .bash_profile can help accomplish something similar?
batclip() {
local args=()
if [[ -n "$1" ]]; then
args+=("--language=$1")
fi
bat "${args[@]}" < <(cat /dev/clipboard)
}
Apparently, cygwin/msys2 also has a clip command which could also be used as a workaround?
clip | bat
Would be faster to type anyways.
Closing this for now, unless there is further feedback from @Mike325
Most helpful comment
I had a bit of time to experiment with this recently.
/dev/clipboardis considered a character device, which is something that only exists inside the MSYS2 environment. It seems that the only way to get its contents from non-MSYS2 executables is to pipe it through STDIN likecat /dev/clipboard | other_programorother_program < <(cat /dev/clipboard).I still can't think of any solution to this, but maybe a function inside the git-bash equivalent of a
.bash_profilecan help accomplish something similar?