Skip to main content
replaced http://stackapps.com/ with https://stackapps.com/
Source Link

Edit: this script now lives on StackAppsthis script now lives on StackApps.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

Edit: this script now lives on StackApps.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

Edit: this script now lives on StackApps.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

deleted 801 characters in body
Source Link
Kip
  • 31.2k
  • 11
  • 75
  • 132

Edit: this script now lives on StackApps.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

My script doesn't work exactly the same as the question/answer text editors, but it's similar. One difference (which I think is an improvement) is that hitting Ctrl+B when no text is selected just puts ** in the window, as opposed to putting **strong text** with "strong text" selected. This means you can use the shortcuts like you would in a word processor: hit Ctrl+B to enter bold mode, type what you want, then hit Ctrl+B again to back out. (Actually upon further examination, the question/answer text editors behave similar to this, I'd just never tried it.)

If you find any problems, please leave a comment here and I'll look into it.

Install script (requires GreaseMonkey)

View source

Update History

* 2009.07.28 - fixed buggy detection of ctrl key, used much better built-in "ctrlKey" attribute of keypresssed event object. * 2009.08.16 - added Ctrl+K (backtick) support.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

My script doesn't work exactly the same as the question/answer text editors, but it's similar. One difference (which I think is an improvement) is that hitting Ctrl+B when no text is selected just puts ** in the window, as opposed to putting **strong text** with "strong text" selected. This means you can use the shortcuts like you would in a word processor: hit Ctrl+B to enter bold mode, type what you want, then hit Ctrl+B again to back out. (Actually upon further examination, the question/answer text editors behave similar to this, I'd just never tried it.)

If you find any problems, please leave a comment here and I'll look into it.

Install script (requires GreaseMonkey)

View source

Update History

* 2009.07.28 - fixed buggy detection of ctrl key, used much better built-in "ctrlKey" attribute of keypresssed event object. * 2009.08.16 - added Ctrl+K (backtick) support.

Edit: this script now lives on StackApps.

Since this request is declined and Jeff is unlikely to be swayed by a bounty, I have written a GreaseMonkey script to add this functionality. I've added Ctrl+B and Ctrl+I support. I agree that it would be a good idea for this to be incorporated into the base code, but I guess it is not a high priority.

deleted 2357 characters in body
Source Link
Kip
  • 31.2k
  • 11
  • 75
  • 132
// ==UserScript==
// @name           comment textarea keyboard shortcuts
// @namespace      stackoverflow
// @description    Adds Ctrl+B (bold), Ctrl+I (italic), and Ctrl+K (code) keyboard shortcuts to comments
// @include        http://stackoverflow.com/questions/*
// @include        http://meta.stackoverflow.com/questions/*
// @include        http://serverfault.com/questions/*
// @include        http://superuser.com/questions/*
// ==/UserScript==

(function(){
  function GM_wait()
  {
    if(typeof unsafeWindow.jQuery == 'undefined') {
      window.setTimeout(GM_wait,100);
    } else {
      $ = unsafeWindow.jQuery;
      letsJQuery();
    }
  }
  GM_wait();
  function letsJQuery()
  {
    $('textarea[name=comment]').live('keydown', function(e) {
      if(e.ctrlKey && (e.which == 66 || e.which == 73 || e.which == 75))
      {
        //Ctrl+B or Ctrl+I or Ctrl+K
        var text = $(this).val();
        var before = text.substring(0,this.selectionStart);
        var selected = text.substring(this.selectionStart,this.selectionEnd);
        var after = text.substring(this.selectionEnd,text.length);
        
        var selLen = selected.length;
        var markup = '';
        if(e.which == 66)
          markup = '**';
        else if(e.which == 73)
          markup = '*';
        else
          markup = '`';
        var mLen = markup.length;
        
        var replace = '';
        if(selLen == 0)
        {
          replace = markup;
        }
        else if(selLen > 2*mLen
                && selected.substring(0,mLen) == markup
                && selected.substring(selLen - mLen, selLen) == markup)
        {
          replace = selected.substring(mLen, selLen - mLen);
        }
        else
        {
          replace = markup + selected + markup;
        }
        
        $(this).val(before + replace + after);
        if(selLen > 0)
        {
          this.selectionStart = before.length;
          this.selectionEnd = before.length + replace.length;
        }
        else
        {
          this.selectionEnd = this.selectionStart = before.length + replace.length;
        }
        
        return false;
      }
    });
  }
})();

Install script (requires GreaseMonkey)

View source

// ==UserScript==
// @name           comment textarea keyboard shortcuts
// @namespace      stackoverflow
// @description    Adds Ctrl+B (bold), Ctrl+I (italic), and Ctrl+K (code) keyboard shortcuts to comments
// @include        http://stackoverflow.com/questions/*
// @include        http://meta.stackoverflow.com/questions/*
// @include        http://serverfault.com/questions/*
// @include        http://superuser.com/questions/*
// ==/UserScript==

(function(){
  function GM_wait()
  {
    if(typeof unsafeWindow.jQuery == 'undefined') {
      window.setTimeout(GM_wait,100);
    } else {
      $ = unsafeWindow.jQuery;
      letsJQuery();
    }
  }
  GM_wait();
  function letsJQuery()
  {
    $('textarea[name=comment]').live('keydown', function(e) {
      if(e.ctrlKey && (e.which == 66 || e.which == 73 || e.which == 75))
      {
        //Ctrl+B or Ctrl+I or Ctrl+K
        var text = $(this).val();
        var before = text.substring(0,this.selectionStart);
        var selected = text.substring(this.selectionStart,this.selectionEnd);
        var after = text.substring(this.selectionEnd,text.length);
        
        var selLen = selected.length;
        var markup = '';
        if(e.which == 66)
          markup = '**';
        else if(e.which == 73)
          markup = '*';
        else
          markup = '`';
        var mLen = markup.length;
        
        var replace = '';
        if(selLen == 0)
        {
          replace = markup;
        }
        else if(selLen > 2*mLen
                && selected.substring(0,mLen) == markup
                && selected.substring(selLen - mLen, selLen) == markup)
        {
          replace = selected.substring(mLen, selLen - mLen);
        }
        else
        {
          replace = markup + selected + markup;
        }
        
        $(this).val(before + replace + after);
        if(selLen > 0)
        {
          this.selectionStart = before.length;
          this.selectionEnd = before.length + replace.length;
        }
        else
        {
          this.selectionEnd = this.selectionStart = before.length + replace.length;
        }
        
        return false;
      }
    });
  }
})();

Install script (requires GreaseMonkey)

View source

added Ctrl+K support; added 16 characters in body
Source Link
Kip
  • 31.2k
  • 11
  • 75
  • 132
Loading
fixed buggy detection of ctrl key, used much better built-in "ctrlKey" attribute of keypresssed event object.
Source Link
Kip
  • 31.2k
  • 11
  • 75
  • 132
Loading
Bounty Ended with 100 reputation awarded by CommunityBot
Source Link
Kip
  • 31.2k
  • 11
  • 75
  • 132
Loading