Skip to content

Commit 67b99e9

Browse files
committed
add ctrl-y & ctrl-p for copy from/to clipboard
1 parent 8608321 commit 67b99e9

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

web/static/js/vimbin.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,64 @@ document.addEventListener("DOMContentLoaded", function () {
6161
matchBrackets: true,
6262
showCursorWhenSelecting: true,
6363
theme: getPreferredTheme(),
64+
lineWrapping: true, // Optional: enable line wrapping if desired
6465
});
6566

67+
editor.setOption("extraKeys", {
68+
"Ctrl-Y": function () {
69+
const selectedText = editor.getSelection();
70+
71+
if (!selectedText) {
72+
document.getElementById("vim-command-line").innerText =
73+
"No text selected to yank";
74+
setClearMessageTimer();
75+
return;
76+
}
77+
78+
navigator.clipboard
79+
.writeText(selectedText)
80+
.then(function () {
81+
document.getElementById("vim-command-line").innerText =
82+
"Yanked to clipboard";
83+
})
84+
.catch(function (error) {
85+
document.getElementById("vim-command-line").innerText =
86+
"Error yanking to clipboard: " + error.message;
87+
});
88+
89+
setClearMessageTimer();
90+
editor.focus();
91+
},
92+
"Ctrl-P": function () {
93+
navigator.clipboard
94+
.readText()
95+
.then(function (clipboardText) {
96+
if (clipboardText) {
97+
const cursor = editor.getCursor();
98+
editor.replaceRange(clipboardText, cursor);
99+
document.getElementById("vim-command-line").innerText =
100+
"Pasted from clipboard";
101+
} else {
102+
document.getElementById("vim-command-line").innerText =
103+
"Clipboard is empty";
104+
}
105+
})
106+
.catch(function (error) {
107+
document.getElementById("vim-command-line").innerText =
108+
"Error pasting from clipboard: " + error.message;
109+
});
110+
111+
setClearMessageTimer();
112+
editor.focus();
113+
},
114+
});
115+
116+
function setClearMessageTimer() {
117+
setTimeout(function () {
118+
document.getElementById("vim-command-line").innerText = "";
119+
}, 3000);
120+
}
121+
66122
editor.focus();
67123

68124
var vimMode = document.getElementById("vim-mode");

0 commit comments

Comments
 (0)