When in the Insert state, I would like the key combination "ii" to switch to the Normal state. I tried the method described here, but it does not work for "ii". It works for other combinations I have tried (i.e. "jj" and "jk") but not "ii".
Here is my .emacs.
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(add-to-list 'load-path "~/.emacs.d")
(require 'follow-mouse)
(turn-on-follow-mouse)
(package-initialize)
(require 'evil)
(evil-mode 1)
(require 'key-chord)
(setq key-chord-two-keys-delay 0.5)
(key-chord-define evil-insert-state-map "ii" 'evil-normal-state)
(key-chord-mode 1)
EDIT 2014-08-07 11:57 EST I just found out that, after entering the Insert state, I need to type at least one printable character other than "i", and then the binding will work. Does anyone know why that might be?
Related
I want autocomplete or company to work with C code and I need structure context level autocomplete i.e if I have a struct:
typefdef struct HOST_ {
IP4_ADDRESS ip4;
IP6_ADDRESS ip6;
MAC_ADDRESS mac;
} HOST;
... and then I have code:
HOST host;
host->
Then if I hit tab then I should get a drop down menu giving me all the (and only the) three options.
This is what I did:
(defun my-tab-del-commands(key-map)
"This function sets the <tab> and <backspace> keymaps according to the special commands
that I have set. This needs to be set per mode keymap where it is needed. We cannot
use the global keymap because that would override command completion in minibuffer"
;; Set TAB in major-mode keymap to "tab-to-tab" stop
;; Use tab2tab stop and not just add +4 spaces (tab stop defined by tag-stop-list)
(define-key key-map (kbd "<tab>") 'tab-to-tab-stop)
;; Define Key for <backtab>
(define-key key-map (kbd "<S-tab>") 'backward-move-to-tab-stop)
(define-key key-map (kbd "<backtab>") 'backward-move-to-tab-stop)
;; Define C-tab as my-indent-system
(define-key key-map (kbd "<C-tab>") 'my-indent-system)
;; Also provide a duplicate "\C-ci" in case C-TAB is overridden by any minor mode
(define-key emacs-lisp-mode-map "\C-ci" 'my-indent-system)
;; Shift backspace moves by one tab stop till no more whitespace
(define-key key-map (kbd "<S-backspace>") 'backspace-whitespace-to-tab-stop)
;; Alt backspace is c-hungry-delete-backwards
(define-key key-map (kbd "<M-backspace>") 'c-hungry-delete-backwards)
)
;; Simple Backend of semantic with ede as project manager
;; Lets try semantic (CDET) backend for now only.
;; Later learn clang or rtags or other big stuff to handle the whole project
(defun backend:semantic-ede ()
"Setup for semantic-mode with ede as project management.
Simple stuff for starters"
(semantic-mode 1)
(global-semanticdb-minor-mode 1)
(global-semantic-idle-scheduler-mode 1)
(setq-mode-local c-mode semanticdb-find-default-throttle
'(project unloaded system recursive))
(semantic-add-system-include my-lp-build-path)
(semanticdb-enable-gnu-global-databases 'c-mode t)
(semanticdb-enable-gnu-global-databases 'c++-mode t)
(require 'ede)
(global-ede-mode)
(let ((makefile (format "%s/Makefile" my-mp-source-path)))
(when (file-exists-p makefile)
(ede-cpp-root-project "openflow" :file makefile)))
)
;; There are two "schools of thought" of intelligent autocomplete
;; (a) auto-complete package and (b) company package.
;; This is auto-complete
(defun completion:auto-complete ()
"Setup auto-complete with triggers. We still need to hook with C mode"
;; Then autocomplete
(require 'auto-complete)
(require 'auto-complete-config)
(global-auto-complete-mode t)
;; auto complete mod
;; should be loaded after yasnippet so that they can work together
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(ac-config-default)
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
;; set the trigger key so that it can work together with yasnippet on tab key,
;; if the word exists in yasnippet, pressing tab will cause yasnippet to
;; activate, otherwise, auto-complete will
(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")
(global-auto-complete-mode t)
)
(defun my-smart-c-auto-complete()
;; Now tie up autocomplete with semantic
;; Later hook it up to c-mode-hook via c-mode-config func
(require 'auto-complete-c-headers)
(add-to-list 'ac-sources 'ac-source-words-in-same-mode-buffers)
(add-to-list 'ac-sources 'ac-source-dictionary)
(add-to-list 'ac-sources 'ac-source-abbrev)
(add-to-list 'ac-sources 'ac-source-semantic)
(add-to-list 'ac-sources 'ac-source-c-headers)
)
;; This is company
(defun completion:company ()
"Setup comany mode"
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
)
(defun tab-side-story ()
(interactive)
;; First load and init Yasnippet - This sets the TAB first to Ysnippet
;; This will work on LISP & SHELL SCRIPTS also
;; (require 'yasnippet)
;; (yas-global-mode 1)
;; Second, use whatever completion mechanism you wanna use auto-complete or company
;; This will work on LISP also!
;;(completion:auto-complete)
(completion:company)
;; Then this function sets tab command
(my-tab-del-commands c-mode-base-map)
)
;; Completion mechanism needs a backend
(backend:semantic-ede)
(tab-side-story)
;; define My Great C style with linux as the parent style (choose google if you wish)
(c-add-style "my-great-c-style"
'("linux" ; use linux as parent style
(indent-tabs-mode . nil) ; tabs are spaces
(c-syntactic-indentation . t) ; auto-syntax
(c-syntactic-indentation-in-macros . t) ; ditto for macros
(c-tab-always-indent . nil) ; Otherwise our complex TAB will not work!
(tab-width . 4) ; if there is a <tab> expand to 4 spaces
(backward-delete-function . nil) ; DO NOT expand tabs when deleting
(c-continued-statement-offset . 4))) ;continued statement offset 4
(defun my-c-mode-config ()
(c-set-style "my-great-c-style")
(when (= emacs-major-version 23)
(gtags-mode 1))
(when (= emacs-major-version 24)
(tab-side-story)
;; (my-smart-c-auto-complete)
(ggtags-mode 1)
(setq ggtags-update-on-save nil))
(electric-indent-mode)
(setq c-basic-offset 4)
(setq c-tab-always-indent nil))
;; Needs to be hooked - do it here instead of common area
(add-hook 'c-mode-hook 'my-c-mode-config)
Sorry for the very long code but most of them are my comments and notes jotted down so far less "code" than it seems. In this I have set it for company mode though I have auto-complete.
Both are not working! Something is clashing with something.
I am trying to properly configure emacs to write my Clojure code. I based my Emacs configurations on this good blog post.
However, I did change a few settings like the theme he is using etc. I have been checking to get Auto-Complete (Eldoc? I am not sure) to display the docstring of the functions in Clojure, and from my own code.
I want to see the documentation like in that screenshot:
However, I cannot get the "yellow" documentation box to appear. I am not sure if this is due to a misconfiguration in my .emacs file, or if it is a command I have to use or...
Here is my .emacs file:
(add-to-list 'custom-theme-load-path "~/.emacs.d/lib/noctilux-theme")
(require 'package):
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/")
'("melpa" . "http://melpa.milkbox.net/packages/"))
;; Initialize all the ELPA packages (what is installed using the packages commands)
(package-initialize)
;; Set bigger fonts
(set-default-font "Courier New-13")
;; Setup to have a french keyboard layout working
(require 'iso-transl)
;; Show parenthesis mode
(show-paren-mode 1)
;; rainbow delimiters
(global-rainbow-delimiters-mode)
;; paredit
(add-hook 'clojure-mode-hook 'paredit-mode)
(add-hook 'nrepl-mode-hook 'paredit-mode)
(global-set-key [f7] 'paredit-mode)
;; theme
(load-theme 'noctilux t)
;; clojure-mode
(global-set-key [f9] 'cider-jack-in)
(add-hook 'clojure-mode-hook 'turn-on-eldoc-mode)
;; nrepl
(add-hook 'nrepl-interaction-mode-hook 'nrepl-turn-on-eldoc-mode)
(setq nrepl-popup-stacktraces nil)
(add-to-list 'same-window-buffer-names "*nrepl*")
(add-hook 'nrepl-mode-hook 'paredit-mode)
;; Auto complete
(require 'auto-complete-config)
;(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(setq ac-delay 0.0)
;(setq ac-use-quick-help t)
(setq ac-quick-help-delay 0.0)
;(setq ac-use-fuzzy 1)
;(setq ac-auto-start 1)
;(setq ac-auto-show-menu 1)
(ac-config-default)
;; ac-nrepl
(require 'ac-nrepl)
(add-hook 'nrepl-mode-hook 'ac-nrepl-setup)
(add-hook 'nrepl-interaction-mode-hook 'ac-nrepl-setup)
(eval-after-load "auto-complete" '(add-to-list 'ac-modes 'nrepl-mode))
(defun set-auto-complete-as-completion-at-point-function ()
(setq completion-at-point-functions '(auto-complete)))
(add-hook 'auto-complete-mode-hook 'set-auto-complete-as-completion-at-point-function)
(add-hook 'cider-repl-mode-hook 'set-auto-complete-as-completion-at-point-function)
(add-hook 'cider-mode-hook 'set-auto-complete-as-completion-at-point-function)
;; scroll one line at a time (less "jumpy" than defaults)
(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
Update based on #syohex's comment bellow
Here is what I see in my CLJ buffers:
In the NREPL, I do see a "v" instead of a "d" (by the way, what does these letters means?). As I said in my comment, in NREPL, I do see the yellow box appearing, then when the popup appears, then the doc yellow box disapear. In the CLJ code buffers, the yellow box never open.
Final Update
After some more testing, everything is working as expected. When the popup first shows, the yellow box disapear. However, when I start selecting different choices, then it reappears, at the right place.
Also, it started working in the CLJ buffers, and I see the letter "v" instead of "d" as seen in the sreenshot above. Maybe I forgot to start the NREPL, would have to re-test.
In any case, everything is working as expected.
nrepl-mode-hook, nrepl-interaction-mode-hook, nrepl-mode are obsoleted. You should use cider-mode-hook, cider-repl-mode-hook, cider-mode instead of them respectively.
And you should set ac-quick-help-delay to value more than 0(for example 0.5).
Patch is below.
--- nconf-orig.el 2014-05-21 16:51:40.056185465 +0900
+++ conf-new.el 2014-05-21 16:53:11.936182181 +0900
## -43,7 +43,7 ##
;(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
(setq ac-delay 0.0)
;(setq ac-use-quick-help t)
-(setq ac-quick-help-delay 0.0)
+(setq ac-quick-help-delay 0.5)
;(setq ac-use-fuzzy 1)
;(setq ac-auto-start 1)
;(setq ac-auto-show-menu 1)
## -51,9 +51,10 ##
;; ac-nrepl
(require 'ac-nrepl)
-(add-hook 'nrepl-mode-hook 'ac-nrepl-setup)
-(add-hook 'nrepl-interaction-mode-hook 'ac-nrepl-setup)
-(eval-after-load "auto-complete" '(add-to-list 'ac-modes 'nrepl-mode))
+(add-hook 'cider-mode-hook 'ac-nrepl-setup)
+(add-hook 'cider-repl-mode-hook 'ac-nrepl-setup)
+(add-to-list 'ac-modes 'cider-mode)
+(add-to-list 'ac-modes 'cider-repl-mode)
(defun set-auto-complete-as-completion-at-point-function ()
Actually this configuration and the former ac-nrepl is deprecated, refer to the new package ac-cider
(require 'ac-cider)
(add-hook 'cider-mode-hook 'ac-flyspell-workaround)
(add-hook 'cider-mode-hook 'ac-cider-setup)
(add-hook 'cider-repl-mode-hook 'ac-cider-setup)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes 'cider-mode))
the yellow box appears first, then when the autocompletion popup
appears, the yellow box with the docs disapear.
This behavior is caused by(setq ac-quick-help-delay 0.5) since 0.5s is too fast
Change 0.5 to a bigger number (minimal value to work is 0.9 on my computer, but this may dependent on your computer's performance) or
delete (setq ac-quick-help-delay 0.5) Since this would use the default: about 2.5s, it can resolve the problem.
I am viewing this in 2020 and ac-cider is deprecated but you can use company-mode instead.
Use M-x package-install company to install and follow the cider docs to configure it for cider-repl-mode and cider-mode
I'm using emacs with sr-speedbar, but can't control its width. When I resize the emacs window, the sr-speedbar always expands with it. I have tried this both with xemacs and emacs.
My .emacs file below:
(require 'package)
(package-initialize)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(setq c-default-style "linux"
c-basic-offset 4)
(iswitchb-mode 1)
(setq inhibit-splash-screen t)
(load-theme 'zenburn t)
(require 'ecb)
(setq stack-trace-on-error t)
(desktop-save-mode 1)
;(defun toggle-fullscreen ()
; (interactive)
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
;)
;(toggle-fullscreen)
(require 'sr-speedbar)
(setq
sr-speedbar-right-side nil
sr-speedbar-width-x 10
sr-speedbar-width-console 10
sr-speedbar-max-width 10
sr-speedbar-delete-windows t)
(sr-speedbar-open)
(put 'dired-find-alternate-file 'disabled nil)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
;; Single char cursor movement. (assuming you are on qwerty)
(global-set-key (kbd "M-j") 'backward-char)
(global-set-key (kbd "M-l") 'forward-char)
(global-set-key (kbd "M-i") 'previous-line)
(global-set-key (kbd "M-k") 'next-line)
(global-set-key (kbd "M-SPC") 'set-mark-command)
(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.
'(custom-safe-themes (quote ("d63e19a84fef5fa0341fa68814200749408ad4a321b6d9f30efc117aeaf68a2e" default)))
'(ecb-options-version "2.40"))
(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.
)
It consumes variables at load time, not when open.
The sr-speedbar-width-* should be set before the script loaded.
This does not work, because the variables sr-speedbar-width-x and sr-speedbar-width-console do not exist any more. They have been removed from the code on 03 Aug 2014. See here: https://www.emacswiki.org/emacs/download/sr-speedbar.el
The information that you probably found on https://www.emacswiki.org/emacs/SrSpeedbar is outdated.
There is only one variable sr-speedbar-width
The variable is "consumed" when sr-speedbar-open is called. The user can define the variable before or after loading the library. This is due to the behaviour of the Emacs Lisp special form defvar (line 364), which does not overwrite the variable. For more information on this see here: https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Variables.html
I am new to StackOverflow and I have a question about an issue that has been virtually the only thing to irk me in my quest to master Emacs.
I configured my .emacs file to set the default frame size for Emacs to 70 rows and 80 columns like so:
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))
This works fine when starting Emacs, the problem is that when I launch a new frame using new-frame or emacsclient -c these settings are not respected. Is there a way to force emacsclient and new-frame to read the settings in the .emacs file when they are executed?
Edit:
Here is my .emacs file:
;;This setting is meant to force emacs to read size settings before make-frame.
(add-hook 'before-make-frame-hook
#'(lambda ()
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))))
;;disable annoying welcome screen.
(setq inhibit-splash-screen t)
(setq inferior-lisp-program "/usr/bin/abcl")
(add-to-list 'load-path "/usr/share/emacs/site-lisp/slime/")
(require 'slime)
(slime-setup)
(add-to-list 'auto-mode-alist '("\\.lisp$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.cl$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.asd$" . lisp-mode))
(require 'slime)
(slime-setup)
(eval-after-load "slime"
'(progn
(setq slime-complete-symbol*-fancy t
slime-complete-symbol-function 'slime-fuzzy-complete-symbol
slime-when-complete-filename-expand t
slime-truncate-lines nil
slime-autodoc-use-multiline-p t)
(slime-setup '(slime-fancy slime-asdf))
(define-key slime-repl-mode-map (kbd "C-c ;")
'slime-insert-balanced-comments)
(define-key slime-repl-mode-map (kbd "C-c M-;")
'slime-remove-balanced-comments)
(define-key slime-mode-map (kbd "C-c ;")
'slime-insert-balanced-comments)
(define-key slime-mode-map (kbd "C-c M-;")
'slime-remove-balanced-comments)
(define-key slime-mode-map (kbd "RET") 'newline-and-indent)
(define-key slime-mode-map (kbd "C-j") 'newline)))
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))
(normal-erase-is-backspace-mode 0)
(tool-bar-mode -1) ;;method for disabling changed in 24. can not nil, most negative
(scroll-bar-mode -1)
;;for loading cedet.
(load-file "/usr/share/emacs/site-lisp/cedet/common/cedet.el")
You might try adding the following hook to your Emacs start-up file (normally, ~/.emacs), i.e.:
(add-hook 'before-make-frame-hook
#'(lambda ()
(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))))
Or, if you want to reload the whole .emacs file:
(add-hook 'before-make-frame-hook #'(lambda () (load-file "~/.emacs")))
The former hook is almost certainly preferable, as reloading the entire .emacs file is probably not only unnecessary and wasteful of resources, but also has the potential to cause errors or weird behavior (depending on the file's contents).
To elaborate, hooks are variables which define lists of commands that are executed when specific events happen within your Emacs session, e.g., the loading of a major editing mode, or, as in this case, the creation of a new frame. See the relevant Emacs manual page on hooks for more information. Generally speaking, if you want some function to be executed every time a particular event occurs within Emacs, adding said function to the right pre-existing hook is probably the best way to go about it.
Recently I tried Emacs and found Evil helpful to keep my vim custom. I'm used to typing "jj" to return normal mode from insert mode like many Vimers do but don't know how to make it in Evil mode.
I map it like this but seems not correct:
(define-key evil-insert-state-map (kbd "jj") 'evil-normal-state)
This works for me. It requires the KeyChord library:
;;Exit insert mode by pressing j and then j quickly
(setq key-chord-two-keys-delay 0.5)
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
(key-chord-mode 1)
It is inspired by #phils answer above and based on Simon's Coding Blog: Emacs and Unity Every Day.
I don't know whether it works with Evil, but for Emacs in general the KeyChord library is designed for this sort of thing.
Try it and see?
(key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
If you're using Spacemacs then I just found that this setting (added to the beginning of user-init) works very well,
(setq-default evil-escape-key-sequence "jj")
See this blog post: http://zuttobenkyou.wordpress.com/2011/02/15/some-thoughts-on-emacs-and-vim/ and search for "cofi". I use the "kj" version myself and it works just like Vim.
EDIT: Here is the actual code snippet from the blog post:
(define-key evil-insert-state-map "k" #'cofi/maybe-exit)
(evil-define-command cofi/maybe-exit ()
:repeat change
(interactive)
(let ((modified (buffer-modified-p)))
(insert "k")
(let ((evt (read-event (format "Insert %c to exit insert state" ?j)
nil 0.5)))
(cond
((null evt) (message ""))
((and (integerp evt) (char-equal evt ?j))
(delete-char -1)
(set-buffer-modified-p modified)
(push 'escape unread-command-events))
(t (setq unread-command-events (append unread-command-events
(list evt))))))))
For my windows install, adding as part of use-package evil configuration worked for me in init.el:
(use-package evil
:ensure t
:config
(evil-mode 1)
(define-key evil-insert-state-map "jj" 'evil-normal-state)
)
For Ubuntu, I followed E. Sambo's answer.
It's a bit more complicated - you have to watch for the previous character. This should do the trick. (the gist is for "jk", you can easily modify it for "jj" though you will note that "jk" is more efficient/faster).
This is my own solution i've been using for some time, although i use `jf' actually.
(defun xwl-jj-as-esc ()
(interactive)
(if (memq evil-state '(insert replace))
(let ((changed? (buffer-modified-p)))
(insert "j")
(let* ((tm (current-time))
(ch (read-key)))
(if (and (eq ch ?j)
(< (time-to-seconds (time-since tm)) 0.5))
(save-excursion
(delete-char -1)
(evil-force-normal-state)
(set-buffer-modified-p changed?))
(insert ch))))
(call-interactively 'evil-next-line)))
(define-key evil-insert-state-map "j" 'xwl-jj-as-esc)
(define-key evil-replace-state-map "j" 'xwl-jj-as-esc)
Initially I naively shared the answer below which not only it doesn't work (since it "disables" j and k) but also many emacs folks would laugh!
I found that evil-mode provides evil-define-key according to https://evil.readthedocs.io/en/latest/keymaps.html#evil-define-key.
So I tried below and it works for me.
;; you should be able to do "jj" instead but I prefer "jk"
(evil-define-key 'insert 'global (kbd "jk") 'evil-normal-state)
Now I found a real answer that is satisfying to me and working (using https://github.com/noctuid/general.el)
;; Enable installation of packages from MELPA.
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
;; Download Evil
(unless (package-installed-p 'evil)
(package-install 'evil))
;; Enable Evil
(require 'evil)
(evil-mode 1)
;; ------------------------------------------------------
;; you might have all the above already
;; but I still included for the completeness of the code
;; ------------------------------------------------------
;; Download general.el
(unless (package-installed-p 'general)
(package-install 'general))
;; Enable general.el
(require 'general)
(general-evil-setup)
;; now usable `jk` is possible!
(general-imap "j"
(general-key-dispatch 'self-insert-command
:timeout 0.25
"k" 'evil-normal-state))
since I literally just discovered general.el, this code might not be optimal