Skip to content

Instantly share code, notes, and snippets.

@Boorj
Forked from IamPhytan/custom.js
Last active October 19, 2020 17:00
Show Gist options
  • Save Boorj/eb020e14487329431bdabc9141ee7ca1 to your computer and use it in GitHub Desktop.
Save Boorj/eb020e14487329431bdabc9141ee7ca1 to your computer and use it in GitHub Desktop.
Codemirror custom Keyboard Shortcuts / JetBrains keymap
// CodeMirror API - https://codemirror.net
/** Duplicate a current line in CodeMirror doc */
function cmDuplicateLine(cm) {
// get a position of a current cursor in a current cell
var currentCursor = cm.doc.getCursor();
// read a content from a line where is the current cursor
var lineContent = cm.doc.getLine(currentCursor.line);
// go to the end the current line
CodeMirror.commands.goLineEnd(cm);
// make a break for a new line
CodeMirror.commands.newlineAndIndent(cm);
// move caret to the left position
CodeMirror.commands.goLineStart(cm);
// filled a content of the new line content from line above it
cm.doc.replaceSelection(lineContent);
// restore position cursor on the new line
cm.doc.setCursor(currentCursor.line + 1, currentCursor.ch);
}
CodeMirror.keyMap.pcDefault["Ctrl-D"] = cmDuplicateLine;
CodeMirror.keyMap.pcDefault["Ctrl-Y"] = CodeMirror.commands.deleteLine;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment