Skip to content

Instantly share code, notes, and snippets.

@kongliangzhong
Last active December 15, 2015 11:09
Show Gist options
  • Save kongliangzhong/5251214 to your computer and use it in GitHub Desktop.
Save kongliangzhong/5251214 to your computer and use it in GitHub Desktop.
emacs tips
#### EMACS TIPS ####
### cscope: ###
;; * Keybindings:
;;
;; All keybindings use the "C-c s" prefix, but are usable only while
;; editing a source file, or in the cscope results buffer:
;;
;; C-c s s Find symbol.
;; C-c s d Find global definition.
;; C-c s g Find global definition (alternate binding).
;; C-c s G Find global definition without prompting.
;; C-c s c Find functions calling a function.
;; C-c s C Find called functions (list functions called
;; from a function).
;; C-c s t Find text string.
;; C-c s e Find egrep pattern.
;; C-c s f Find a file.
;; C-c s i Find files #including a file.
;;
;; These pertain to navigation through the search results:
;;
;; C-c s b Display *cscope* buffer.
;; C-c s B Auto display *cscope* buffer toggle.
;; C-c s n Next symbol.
;; C-c s N Next file.
;; C-c s p Previous symbol.
;; C-c s P Previous file.
;; C-c s u Pop mark.
;;
;; These pertain to setting and unsetting the variable,
;; `cscope-initial-directory', (location searched for the cscope database
;; directory):
;;
;; C-c s a Set initial directory.
;; C-c s A Unset initial directory.
;;
;; These pertain to cscope database maintenance:
;;
;; C-c s L Create list of files to index.
;; C-c s I Create list and index.
;; C-c s E Edit list of files to index.
;; C-c s W Locate this buffer's cscope directory
;; ("W" --> "where").
;; C-c s S Locate this buffer's cscope directory.
;; (alternate binding: "S" --> "show").
;; C-c s T Locate this buffer's cscope directory.
;; (alternate binding: "T" --> "tell").
;; C-c s D Dired this buffer's directory.
##############
### Cursor Movement and Location ###
This reference guide lists the commands available within GNU Emacs for moving the cursor, and reporting its location.
NOTICE - New forum for all UNIX learners, users and programmers - click here
arrow keys (if available) N/A Move cursor.
----------------------------------------------------------------------------------------------------------------------------
C-a beginning-of-line Move to beginning of line.
C-e end-of-line Move to end of line.
C-f forward-char Move forward one character.
C-b backward-char Move backward one character.
M-f forward-word Move forward one word.
M-b backward-word Move backward one word.
C-n next-line Move down a line (creating the next line, if necessary).
C-p previous-line Move up a line.
M-r move-to-window-line Move to left margin, vertically centered in window.
M-< beginning-of-buffer Move to top of buffer.
M-> end-of-buffer Move to end of buffer.
M-x goto-char goto-char Read a number and go to that buffer position (start = 1).
M-x goto-line goto-line Read a number and go to that line (first line = 1).
C-x C-n set-goal-column Set current column as 'goal column'. From then on, the C-n and C-p commands jump to that column,
or the nearest possible column.
C-u C-x C-n N/A Cancel the 'goal column', and return to normal behaviour.
M-x what-page what-page Display page number and line number (within page) of cursor location.
M-x what-line what-line Display line number (within buffer) of cursor location.
M-x line-number-mode line-number-mode Display line number automatically (toggle).
M-= count-lines-region Print number of lines in current region.
C-x = what-cursor-postition Print character code, character position and column of the cursor location.
C-x C-@ goto-previous-position goto previous position
## scroll other window:
use M-PageUp (M-Prior) / M-PageDown
or command: M-x scroll-other-window
####################################
### Emacs Basic Tips:
## In xemacs, you can use C-SPC to set-mark-start, but in terminal, this key stroke is used for switching languages, so use C-@ instaed.
## in order to startup emacs quicker, use emacs -q -nw , will not load user's init file for emacs.
### common lisp samples:
## (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(remove-if-not #'(lambda (x) (= 0 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10))
### Emacs基础:文本删除、复制和恢复
delete-char (C-d)
delete-backward-char (DEL)
kill-word (M-d)
backward-kill-word (M-DEL)
kill-line (C-k)
kill-sentence (M-k)
backward-kill-sentence (C-x DEL)
yank (C-y) -- paste
yank-pop (M-y)
kill-region (C-w) -- cut
kill-paragraph
backward-kill-paragraph
kill-ring-save (M-w) -- copy
### 扩展
If mark active, copy region. Otherwise, copy current line.
(defun new-kill-ring-save (&optional m)
(interactive "p")
(if mark-active
(kill-ring-save (region-beginning) (region-end))
(if (> n 0)
(kill-ring-save (line-beginning-position) (line-end-position n))
(kill-ring-save (line-beginning-position n) (line-end-position)))))
## copy current line
(defun copy-line (&optional arg)
(interactive "p")
(let ((beg (line-beginning-position))
(end (line-end-position arg)))
(copy-region-as-kill beg end)))
## copy words at point
(defun copy-word (&optional arg)
(interactive "p")
(let ((beg (progn (if (looking-back "[a-zA-Z0-9]" 1) (backward-word 1)) (point)))
(end (progn (forward-word arg) (point))))
(copy-region-as-kill beg end)))
## copy paragraphes at point
(defun copy-paragraph (&optional arg)
(interactive "p")
(let ((beg (progn (backward-paragraph 1) (point)))
(end (progn (forward-paragraph arg) (point))))
(copy-region-as-kill beg end)))
insert file to curr buffer: C-x i
## format region:
C-M-\
e.g. C-x h C-M-\
## compile *.el to *.elc
emacs -batch -f batch-byte-compile *.el
## remoce ^M in file:
M-x replace-string C-q C-m Ret Ret
## grep-find exclue .svn directories:
(global-set-key [f3] 'grep-find)
(setq grep-find-command
"find . -path '*/.svn' -prune -o -type f -print | xargs -e grep -I -n -e ")
## open file use sudo in emacs:
C-x C-f /sudo::/etc/hosts
## reload .emacs while emacs still running:
M-x load-file ENTER
## emacs term
use term in emacs: M-x term
note: key bindings change: use C-c instead of C-x when your curr buffer is the term. typically, if you want to terminate a process in the terminal, type: C-c C-c, if you want to change buffer, use C-c b instead of C-x b.
## search curr word: C-s w
## comment/uncomment in java mode: C-c C-c / C-u C-c C-c
## use emacs daemon:
-- emacs --daemon
use emacsclient:
-- emacsclient -t // open in terminal
-- emacsclient -c // open GUI emacs window
you can define the following alias for convenience:
-- alias et='emacsclient -t'
-- alias ec='emacsclient -c'
## find a file without know exact directory:
M-x find-name-dired
## emacs shells:
-- M-x shell
-- M-x term // the best. use C-C instead of C-x in this buffer.
-- M-x eshell
## replace in multi files:
1) M-x find-name-dired: you will be prompted for a root directory and a filename pattern.
2) Press t to "toggle mark" for all files found.
3) Press Q for "Query-Replace in Files...": you will be prompted for query/substitution regexps.
4) Proceed as with query-replace-regexp: SPACE to replace and move to next match, n to skip a match, etc.
############## EMACS JAVA TIPS ################
-- find class definition in project:
--> for jdk class, just search in jdk source dir.
--> for third party jars, use find-grep on top of dependency jars directory.
e.g. for gradle, use gradle zip to collect all the dependencies in
target/distribution directory, unzip and then use find-grep on this dir.
###############################################
# delete upto non-whitespace character, or join multiple lines to one:
M-^ // M-x delete-indentation
## to make sure two words are seprated by just one space: M-SPACE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment