Search code examples
emacskeyboard-shortcutskey-bindings

emacs shortcut to move cursor to column, adding spaces if needed


Is there a way to make a key-binding such that emacs moves the cursor to a certain column (e.g. 100)? In my case that's where I tend to put inline comments, so it would be great to have a shortcut to add spaces from the end of the line of code, up to line 100. Preferably it would move the cursor (without adding spaces) if there was already text at the (e.g.) 100th line.


Solution

  • Here's a simple option for you - use M-g TAB(bound to move-to-column).

    This command won't add extra spaces if the line is not long enough. To add extra space you can use a slightly modified command:

    (defun go-to-column (column)
      (interactive "nColumn: ")
      (move-to-column column t))
    

    You can use a prefix argument to toggle between the two behaviours if you wish to combine them is a single command.