(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] Assign keyboard shortcuts to adjust buffer width and height
These are f*ucking handy!
[emacs] Toggle read only mode
To ensure you don't accidentally change a file, enable the read-only mode.
OR
Very useful when using gdb
M-x toggle-read-only
OR
C-x C-q
Very useful when using gdb
[emacs] search within a file
To search within a file
.. and it takes regular expressions as well.
M-x occur
.. and it takes regular expressions as well.
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
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)
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.
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.
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)
Backup of .emacs
(global-set-key "%" 'match-paren)
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
(defun my-linux-c-mode ()
"C mode with adjusted defaults for use with the linux kernel."
(c-set-style "linux"))
(add-hook 'c-mode-hook 'my-linux-c-mode)
(server-start)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(show-paren-mode t)
'(transient-mark-mode t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
Update: 05/23/2013
Over the last six years I have made many more changes to the .emacs file. Latest update for backup!
;; ================
;; General settings
;; ================
;; Display the time
(setq display-time-day-and-date t
display-time-24hr-format t)
(display-time)
;; Inhibit startup splash screen
(setq inhibit-startup-message t)
;; enable row and column numner mode
(setq line-number-mode t)
(setq column-number-mode t)
;; Disable automatic backups
(setq backup-inhibited t)
;; show trailing whitespace
(setq-default show-trailing-whitespace t)
;; enable global clipboard to copy, paste into emacs
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
;; Disable tool-bar-mode and other custom changes!
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(comint-scroll-show-maximum-output nil)
'(ediff-merge-split-window-function (quote split-window-vertically))
'(ediff-split-window-function (quote split-window-vertically))
'(tool-bar-mode nil)
'(delete-selection-mode t))
;; Start the server for emacsclient -n to work
(server-start)
(set-background-color "black")
(set-foreground-color "DarkOliveGreen3")
(set-cursor-color "green yellow")
;; full screen mode using [F11]
(defun toggle-fullscreen (&optional f)
(interactive)
(let ((current-value (frame-parameter nil 'fullscreen)))
(set-frame-parameter nil 'fullscreen
(if (equal 'fullboth current-value)
(if (boundp 'old-fullscreen) old-fullscreen nil)
(progn (setq old-fullscreen current-value)
'fullboth)))))
(global-set-key [f11] 'toggle-fullscreen)
;;
;; Map M-_ to delete trailing whitespace
;;
(define-key global-map (kbd "M-_") 'delete-trailing-whitespace)
;; Make new frames fullscreen by default. Note: this hook doesn't do
;; anything to the initial frame if it's in your .emacs, since that file is
;; read _after_ the initial frame is created.
;; NOTE: This is screwing up the emacsclient connections for new frames!
;; (add-hook 'after-make-frame-functions 'toggle-fullscreen)
;; Set c-x p as previous-multiframe-window
(global-set-key (kbd "C-x p") 'previous-multiframe-window)
;; Set a 10pt font
(set-face-attribute 'default nil :height 100)
;; Disable upcase-region
(put 'upcase-region 'disabled nil)
;; Kill process without query!
(add-hook 'comint-exec-hook
(lambda ()
(process-kill-without-query (get-buffer-process (current-buffer)))))
;; ====================
;; Programming settings
;; ====================
;; highlight text beyond 80 characters! Love this one!!
(setq whitespace-style '(trailing lines-tail space-before-tab
indentation space-after-tab)
whitespace-line-column 80)
(global-whitespace-mode 1)
;; Paren mode
(show-paren-mode 1)
;; Use vim style parenthesis matching
(global-set-key "%" 'match-paren)
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
;; Insert spaces instead of tabs
(setq-default indent-tabs-mode nil)
;; Use TABs on Makefile mode
(defun my-makefile-mode-hook()
(setq indent-tabs-mode t))
(add-hook 'makefile-mode-hook 'my-makefile-mode-hook)
;; Custom C\C++ style at work!
(c-add-style "my-style"
'("bsd"
(c-basic-offset . 2)
(c-offsets-alist . (
(topmost-intro . 0)
(substatement . +)
(substatement-open . 0)
(case-label . +)
(access-label . -)
(inclass . +)
(inline-open . 0)))))
;; Use my-style for C and C++ modes. Also enable hungry state to
;; eat out any trailing whitespace
(defun my-c++-mode-hook ()
(c-set-style "my-style")
(auto-fill mode)
(c-toggle-hungry-state 1))
(defun my-c-mode-hook ()
(c-set-style "my-style")
(auto-fill mode)
(c-toggle-hungry-state 1))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-hook 'my-c-mode-hook)
;; set gtags-mode by default for C and C++ modes
(add-hook 'c-mode-hook
'(lambda ()
(gtags-mode t)
))
(add-hook 'c++-mode-hook
'(lambda ()
(gtags-mode t)
))
;; Use C++-mode to edit *.h files (not C-mode)
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
;; Use my-style indentation for MDL files
(setq auto-mode-alist (cons '("\\.mdl\\'" . c-mode) auto-mode-alist))
[Emacs] Fun with rectangles
Lets have some fun with emacs rectangles. Lets assume you have the following text:
012345678901234567890123456789
val1 = 10;
val2 = 20;
val3 = 30;
Now, you need to add "(int)" in front of all the numbers. You can do it one line at a time, or you could use rectangles in emacs. Move the cursor to the start of 10, set mark, make the selection until start of 30. Hint: To set the mark, just hit Ctrl+Space and take your cursor to where you need to stop. Something like this,
012345678901234567890123456789
val1 = 10;
val2 = 20;
val3 = 30;
Ok, now lets insert the string using "string-insert-rectangle" and enter the string that we want to be inserted, in our case "(int)"
M-x string-insert-rectangle
String insert rectangle (default void ): (int)
012345678901234567890123456789
val1 = (int)10;
val2 = (int)20;
val3 = (int)30;
Now, say you wanted to give a space between the (int) and the number. Mark the rectangle as show below:
012345678901234567890123456789
val1 = (int)10;
val2 = (int)20;
val3 = (int)30;
We will use the "open-rectangle" command now.
M-x open-rectangle
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
Cool, now let's delete the spaces in the front using "delete-rectangle". Make the rectangle and...
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
M-x delete-rectangle
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
Now, just for fun, we are going to cut a rectangle and yank it somewhere else. Lets remove 1,2,3 from the variable names and put them in the front (bad idea, I know). Lets select the rectangle and call the "kill-rectangle" command
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
M-x kill-rectangle
012345678901234567890123456789
|val = (int) 10;
val = (int) 20;
val = (int) 30;
Move the cursor (show above as | ) to where you want to yank the rectangle now (in our case, the beginning of the word) and run "yank-rectangle"
M-x yank-rectangle
012345678901234567890123456789
1val = (int) 10;
2val = (int) 20;
3val = (int) 30;
Now, of course there are key-binding available (its Emacs for heaven's sake, even if there is no key-binding you can make your own).
From the Emacs manual
012345678901234567890123456789
val1 = 10;
val2 = 20;
val3 = 30;
Now, you need to add "(int)" in front of all the numbers. You can do it one line at a time, or you could use rectangles in emacs. Move the cursor to the start of 10, set mark, make the selection until start of 30. Hint: To set the mark, just hit Ctrl+Space and take your cursor to where you need to stop. Something like this,
012345678901234567890123456789
val1 = 10;
val2 = 20;
val3 = 30;
Ok, now lets insert the string using "string-insert-rectangle" and enter the string that we want to be inserted, in our case "(int)"
M-x string-insert-rectangle
String insert rectangle (default void ): (int)
012345678901234567890123456789
val1 = (int)10;
val2 = (int)20;
val3 = (int)30;
Now, say you wanted to give a space between the (int) and the number. Mark the rectangle as show below:
012345678901234567890123456789
val1 = (int)10;
val2 = (int)20;
val3 = (int)30;
We will use the "open-rectangle" command now.
M-x open-rectangle
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
Cool, now let's delete the spaces in the front using "delete-rectangle". Make the rectangle and...
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
M-x delete-rectangle
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
Now, just for fun, we are going to cut a rectangle and yank it somewhere else. Lets remove 1,2,3 from the variable names and put them in the front (bad idea, I know). Lets select the rectangle and call the "kill-rectangle" command
012345678901234567890123456789
val1 = (int) 10;
val2 = (int) 20;
val3 = (int) 30;
M-x kill-rectangle
012345678901234567890123456789
|val = (int) 10;
val = (int) 20;
val = (int) 30;
Move the cursor (show above as | ) to where you want to yank the rectangle now (in our case, the beginning of the word) and run "yank-rectangle"
M-x yank-rectangle
012345678901234567890123456789
1val = (int) 10;
2val = (int) 20;
3val = (int) 30;
Now, of course there are key-binding available (its Emacs for heaven's sake, even if there is no key-binding you can make your own).
From the Emacs manual
- C-x r k
- Kill the text of the region-rectangle, saving its contents as the
“last killed rectangle” (
kill-rectangle
). - C-x r d
- Delete the text of the region-rectangle (
delete-rectangle
). - C-x r y
- Yank the last killed rectangle with its upper left corner at point
(
yank-rectangle
). - C-x r o
- Insert blank space to fill the space of the region-rectangle
(
open-rectangle
). This pushes the previous contents of the region-rectangle to the right. - C-x r N
- Insert line numbers along the left edge of the region-rectangle
(
rectangle-number-lines
). This pushes the previous contents of the region-rectangle to the right. - C-x r c
- Clear the region-rectangle by replacing all of its contents with spaces
(
clear-rectangle
). - M-x delete-whitespace-rectangle
- Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
- C-x r t string
- Replace rectangle contents with string on each line
(
string-rectangle
). - M-x string-insert-rectangle
- Insert string on each line of the rectangle.
Using Shift+Arrow keys in Emacs from GNU screen
While inside screen, when using the Shift+Left, Shift-Right, etc keys in Emacs (1) to mark and select, if you see the 2A 2B 2C and 2D characters instead you are probably still using the default TERM which is set to "screen".
Simple solution, use xterm-vt220 or xterm-color instead. You can either create an alias for emacs or change your .screenrc file.
$ alias em='export TERM=xterm-vt220 && emacs -nw'
OR in your ~/.screenrc add,
term xterm-vt220
Simple solution, use xterm-vt220 or xterm-color instead. You can either create an alias for emacs or change your .screenrc file.
$ alias em='export TERM=xterm-vt220 && emacs -nw'
OR in your ~/.screenrc add,
term xterm-vt220
----------------------------------------------------------------
(1) You need to use Emacs 23 or above.
[Bash] Sending stdout and stderr to single file
Remember: To send both the stdout and stderr messages from the console to a file do:
$ app > log.file 2>&1
If you want to send everything to a black-hole just replace
$ app > /dev/null 2>&1
If ever confused about the syntax, do
Update:
It is good idea to use tee, which lets you see the output as well as write it to a file.
$ app > log.file 2>&1
If you want to send everything to a black-hole just replace
log.file
with /dev/null
.$ app > /dev/null 2>&1
If ever confused about the syntax, do
man bash
and search for REDIRECTION
for more gyanUpdate:
It is good idea to use tee, which lets you see the output as well as write it to a file.
$ app 2>&1 | tee file.log
Using [Flex] on OS X
If you are trying to use flex and you get the following error:
Undefined symbols for architecture x86_64:
"_yywrap", referenced from:
_yylex in cceJ8DuJ.o
_input in cceJ8DuJ.o
ld: symbol(s) not found for architecture x86_64
You need to link with libfl.a. Apparently, OSX does not have libfl.a. Use libl.a instead.
$ gcc lex.yy.c -o flexout -ll
Or create a symlink
$ cd /usr/lib
$ ln -s libl.a libfl.a
Undefined symbols for architecture x86_64:
"_yywrap", referenced from:
_yylex in cceJ8DuJ.o
_input in cceJ8DuJ.o
ld: symbol(s) not found for architecture x86_64
You need to link with libfl.a. Apparently, OSX does not have libfl.a. Use libl.a instead.
$ gcc lex.yy.c -o flexout -ll
Or create a symlink
$ cd /usr/lib
$ ln -s libl.a libfl.a
Subscribe to:
Posts (Atom)