Skip to content

Instantly share code, notes, and snippets.

@ctindall
Created January 31, 2020 01:29
Show Gist options
  • Save ctindall/d6268f568721e0bbb073fd02ab6e2fbd to your computer and use it in GitHub Desktop.
Save ctindall/d6268f568721e0bbb073fd02ab6e2fbd to your computer and use it in GitHub Desktop.
(package-initialize)
(defun top-of-buffer ()
"'beginning-of-buffer' sets a mark. This doesn't, just like vi's 'gg' command."
(interactive)
(goto-char (point-min)))
(defun bottom-of-buffer ()
"like 'top-of-buffer' but takes you to the bottom"
(interactive)
(goto-char (point-max)))
(defun buffer-to-string (buffer)
(save-current-buffer
(set-buffer buffer)
(buffer-string)))
(defun make-dominating-project ()
;;idea ripped from https://nullprogram.com/blog/2017/08/22/
"Find the 'closest' (searching up the filesystem hierarchy) 'Makefile' and run 'make' in that directory."
(interactive)
(let ((default-directory (locate-dominating-file (file-name-directory buffer-file-name)
"Makefile")))
(compile "make")))
(defun http-make-request (url request-type encoding data)
(let ((url-request-method request-type)
(message encoding)
(url-request-extra-headers (list (cons "Content-Type" encoding)))
(url-request-data data))
(buffer-to-string (url-retrieve-synchronously url))))
(defun http-make-form-encoded-post (url data)
(message (string url " " data))
(http-make-request url "POST" "application/x-www-form-urlencoded" data))
(defun http-make-json-post (url data)
(http-make-request url "POST" "application/json" data))
(defun http-make-get (url)
(http-make-request url "GET" "" ""))
(global-set-key (kbd "C-x i") `subshell-insert)
(global-set-key (kbd "C-x <up>") `top-of-buffer)
(global-set-key (kbd "C-x <down>") `bottom-of-buffer)
(global-set-key (kbd "C-x <right>") `end-of-visual-line)
(global-set-key (kbd "C-x <left>") `beginning-of-visual-line)
(global-set-key (kbd "C-x c") `make-dominating-project)
(defun insert-divider ()
(interactive)
(insert "\n--------------------------------------------------------------------------------\n"))
(global-set-key (kbd "C-x -") `insert-divider)
(defun kill-matching-lines (regexp &optional rstart rend interactive)
"Kill lines containing matches for REGEXP.
See `flush-lines' or `keep-lines' for behavior of this command.
Ripped from https://www.emacswiki.org/emacs/KillMatchingLines
If the buffer is read-only, Emacs will beep and refrain from deleting
the line, but put the line in the kill ring anyway. This means that
you can use this command to copy text from a read-only buffer.
\(If the variable `kill-read-only-ok' is non-nil, then this won't
even beep.)"
(interactive
(keep-lines-read-args "Kill lines containing match for regexp"))
(let ((buffer-file-name nil)) ;; HACK for `clone-buffer'
(with-current-buffer (clone-buffer nil nil)
(let ((inhibit-read-only t))
(keep-lines regexp rstart rend interactive)
(kill-region (or rstart (line-beginning-position))
(or rend (point-max))))
(kill-buffer)))
(unless (and buffer-read-only kill-read-only-ok)
;; Delete lines or make the "Buffer is read-only" error.
(flush-lines regexp rstart rend interactive)))
(defun dos2unix (buffer)
"Automate M-% C-q C-m RET C-q C-j RET"
(interactive "*b")
(save-excursion
(goto-char (point-min))
(while (search-forward (string ?\C-m) nil t)
(replace-match (string ?\C-j) nil t))))
(defun go (username hostname)
"Opens an ansi-term buffer, uses it to ssh to `hostname', renames the bugger to `hostname', and moves focus there."
(interactive "Musername? \nMhostname? ")
(require 'term)
(let* ((termbuf (apply 'make-term hostname "ssh" nil
(list
(format "%s@%s" username hostname)))))
(set-buffer termbuf)
(term-mode)
(term-char-mode)
(switch-to-buffer termbuf)))
(global-set-key (kbd "C-x g") `go)
(defun split-horizontal-and-rebalance ()
(interactive)
(split-window-below)
(balance-windows))
(global-set-key (kbd "C-x 2") `split-horizontal-and-rebalance)
(defun delete-window-and-rebalance ()
(interactive)
(delete-window)
(balance-windows))
(global-set-key (kbd "C-x 0") `delete-window-and-rebalance)
;;display time in status bar
(setq display-time-24hr-format t)
(display-time)
(global-set-key (kbd "C-x G") 'magit-status)
(defun new-term (name)
"Opens a new ansi-term shell buffer named '*shell-$name*' and switches focus there."
(interactive "Mname? \n")
(require 'term)
(let* ((termbuf (apply 'make-term (format "shell-%s" name) (list "/bin/bash") )))
(set-buffer termbuf)
(term-mode)
(term-char-mode)
(switch-to-buffer termbuf)))
(global-set-key (kbd "C-x t") `new-term)
# start session with a terminal window isntead of that Emacs welcome screen
(ansi-term "/bin/bash")
(switch-to-buffer "*ansi-term*")
# always do the thing where we highlight the paren which matches the one at point
(show-paren-mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment