📜 Spacemacs (Emacs25)

July 4, 2017  

I recently started using Emacs, specifically, the community Spacemacs distribution. Here are some of the most useful functions I have found so far. For the latest version, please consult my dotfiles on Github.

Visual Line Mode

I prefer soft wrapping my lines. Note two parts of this configuration. Firstly, I set variables that make buffers soft wrap. Next, I change my evil binds to operate on visual lines rather than logical lines.

(setq word-wrap 1)
(global-visual-line-mode t)
;; Make evil-mode up/down operate in screen lines instead of logical lines 
(define-key evil-motion-state-map "j" 'evil-next-visual-line)
(define-key evil-motion-state-map "k" 'evil-previous-visual-line)
;; Also in visual mode
(define-key evil-visual-state-map "j" 'evil-next-visual-line)
(define-key evil-visual-state-map "k" 'evil-previous-visual-line)

Insert date/timestamp Emacs Keybinding

I find this function to be very useful and was surprised it wasn’t already in emacs. Bind to whatever you would like and get a date.

(defun insert-current-datetime () (interactive)
	(insert (shell-command-to-string "echo -n $(date '+%A (%B %d) @ %H:%m')")))
(global-set-key "\C-x\M-d" `insert-current-datetime)

Open Orgmode heading in right window

I found this gem from Stack. Essentially, I wanted a way to quickly preview the contents of the org heading content in the right window. Sadly, in its current form it does break links.

(defun org-tree-open-in-right-frame ()
(interactive)
(org-tree-to-indirect-buffer)
(windmove-right))

(add-hook 'org-mode-hook
	(lambda ()
	(define-key evil-normal-state-local-map (kbd "S-<return>") 'org-tree-open-in-right-frame)
	(define-key evil-normal-state-local-map (kbd "<return>") 'org-tree-to-indirect-buffer)
))

Toggle NeoTree for Directory Viewing

I find this provides a more natural IDE experience

(global-set-key [f8] 'neotree-toggle)

General Config (old)

I think these are quite sensible defaults for new users using Emacs for Orgmode.

(setq save-interprogram-paste-before-kill t)
(setq cua-keep-region-after-copy t) ;; Standard Windows behaviour
(transient-mark-mode 1) ;; No region when it is not highlighted
(cua-mode t)
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(define-key cua-global-keymap [C-return] nil)  ;;rebind rectangle mark as I want to use C-return for R/ESS and
(delete-selection-mode 1)
(setf org-blank-before-new-entry '((heading . nil) (plain-list-item . nil)))
(setq org-bullets-mode nil)
(setq org-blank-before-new-entry nil)
(setq org-support-shift-select t)
(define-key org-mode-map (kbd "C-M-<return>") 'org-insert-subheading)

Smooth Scrolling in Spacemacs

This seems to do the job on all my computers

;; Scrolling
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ;; one line at a time
(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq mouse-wheel-follow-mouse 't) ;; scroll window under mouse
(setq scroll-step 1) ;; keyboard scroll one line at a time

Move lines up and down with alt-arrow key in Spacemacs

Does the job - jobs’a’job

;; M-up and M-down move lines
(defun move-line (n)
  "Move the current line up or down by N lines."
  (interactive "p")
  (setq col (current-column))
  (beginning-of-line) (setq start (point))
  (end-of-line) (forward-char) (setq end (point))
  (let ((line-text (delete-and-extract-region start end)))
    (forward-line n)
    (insert line-text)
    ;; restore point to original column in moved line
    (forward-line -1)
    (forward-char col)))

(defun move-line-up (n)
  "Move the current line up by N lines."
  (interactive "p")
  (move-line (if (null n) -1 (- n))))

(defun move-line-down (n)
  "Move the current line down by N lines."
  (interactive "p")
  (move-line (if (null n) 1 n)))

(global-set-key (kbd "M-<up>") 'move-line-up)
(global-set-key (kbd "M-<down>") 'move-line-down)