You could use a userscript to do that.
I created one for you which will give Ctrl+K on Stack Overflow the behavior that you are used to: Cut from cursor to end of line (to clipboard, if allowed by browser).
- Install Tampermonkey for your browser
- Click the Tampermonkey icon in the toolbar and select "Create new script"
- Paste the script below
- Click the save button
The script:
// ==UserScript==
// @name Cut from cursor instead of creating code block
// @version 0.1
// @description Cut from cursor on Ctrl+K on Stack Overflow instead of creating code
// @author CherryDT
// @match https://stackoverflow.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict'
document.addEventListener('keydown', e => {
// If the key in question is Ctrl+K
if (e.which === 75 && e.ctrlKey) {
// Prevent Stack Overflow's default behavior
e.stopImmediatePropagation()
// If cursor is in a textarea or an input field...
const field = document.activeElement
if (field.tagName === 'TEXTAREA' || field.tagName === 'INPUT') {
// Find the end of the current line
let lineEnd = field.value.indexOf('\n', field.selectionStart)
// ...in case there is no newline at the end...
if (lineEnd === -1) lineEnd = field.value.length
// ...in case there was a CRLF...
if (lineEnd > 0 && field.value[lineEnd - 1] === '\r') lineEnd--
// Select rest of line
field.setSelectionRange(field.selectionStart, lineEnd)
// Copy, if possible
try {
document.execCommand('copy')
} catch (e) {
console.warn('Failed to copy', e)
}
// Remove text
field.setRangeText('', field.selectionStart, field.selectionEnd, 'start')
// Prevent browser default action
e.preventDefault()
}
}
}, { capture: true }) // Handle in capture phase to be "faster" than Stack Overflow
})()
A few notes:
- If you want to have this behavior on all sites and not just Stack Overflow, change the
@match
line from https://stackoverflow.com/*
to *://*/*
- If you don't want to move the cut text to the clipboard but just remove it, delete the
try
block.