[emacs] Assign keyboard shortcuts to adjust buffer width and height

These are f*ucking handy!

(global-set-key (kbd "<f5>") 'balance-windows)
(global-set-key (kbd "<f6>") 'shrink-window)
(global-set-key (kbd "<f7<") 'enlarge-window)
(global-set-key (kbd "<f8>") 'enlarge-window-horizontally)
(global-set-key (kbd "<f9>") 'shrink-window-horizontally)

[emacs] Toggle read only mode

To ensure you don't accidentally change a file, enable the read-only mode.

M-x toggle-read-only

OR

C-x C-q

Very useful when using gdb

[emacs] Unwrap long line

To unwrap long lines

M-x toggle-truncate-lines

[emacs] search within a file

To search within a file

M-x occur
.. and it takes regular expressions as well.

[emacs] diff between buffers

To diff between buffers:

M-x ediff-buffers

Get rid of ESC[ in shell

When connected over putty and using screen I find that the output of less contains the "ESC[" character all over.

To fix this, add the following in your .bashrc

$ export LESS="-eirMX"

[Emacs] Fun with windows

An emacs window is different from the regular windows people are used to in a GUI. When you start emacs you create a "frame" which can be split horizontally and vertically into windows.

If you use emacs in the shell ("emacs -nw") then it is a pain to use the default keyboard shortcuts to change to window sizes. Solution is very simple. Map those commands to other shortcuts like the function keys.

(Use C-x e or eval-buffer to execute the lisp change in the .emacs file)

;; Resize the windows
(global-set-key [f5] 'balance-windows)
(global-set-key [f6] 'shrink-window)
(global-set-key [f7] 'enlarge-window)
(global-set-key [f8] 'enlarge-window-horizontally)
(global-set-key [f9] 'shrink-window-horizontally)

[Emacs] Playing with buffers and windows

I always work with two windows in split mode under a single Emacs frame which are split vertically. I wanted to create an easy way to toggle between the horizontal and vertical splits for two windows and also have the capability of swapping the buffers in the two windows.

Here are two functions that do the same. Note that these will only work for two windows.

;; Swap windows
(defun swap-windows ()
  (interactive)
  (if (= (count-windows) 2)
      (let* ((this-win-buffer (window-buffer))
             (next-win-buffer (window-buffer (next-window))))
        (set-window-buffer (next-window) this-win-buffer)
        (set-window-buffer (selected-window) next-win-buffer))))

(define-key ctl-x-4-map "s" 'swap-windows)

The swap-windows() function swaps the buffers between the two windows while the toogle-window-split() toogles between the vertical and horizontal split. I think I found the toogle-window-split() on StackOverflow and wrote the swap-windows() based on that.


;; Toggle buffer splits from vertical to horizontal
(defun toggle-window-split ()
  (interactive)
  (if (= (count-windows) 2)
      (let* ((this-win-buffer (window-buffer))
             (next-win-buffer (window-buffer (next-window)))
             (this-win-edges (window-edges (selected-window)))
             (next-win-edges (window-edges (next-window)))
             (this-win-2nd (not (and (<= (car this-win-edges)
                                         (car next-win-edges))
                                     (<= (cadr this-win-edges)
                                         (cadr next-win-edges)))))
             (splitter
              (if (= (car this-win-edges)
                     (car (window-edges (next-window))))
                  'split-window-horizontally
                'split-window-vertically)))
        (delete-other-windows)
        (let ((first-win (selected-window)))
          (funcall splitter)
          (if this-win-2nd (other-window 1))
          (set-window-buffer (selected-window) this-win-buffer)
          (set-window-buffer (next-window) next-win-buffer)
          (select-window first-win)
          (if this-win-2nd (other-window 1))))))

(define-key ctl-x-4-map "t" 'toggle-window-split)