emacs, keybinding in iterm2 - emacs

I'd like to use the following key mapping
(global-set-key (kbd "C-M-;") 'comment-region) ;doesn't work in shell
(global-set-key (kbd "C-M-DEL") 'indent-region) ;doesn't work in shell
I have the following setup
iterm2 sends escape sequences ^[[1;39 and ^[[1;41 respectively
emacs has the following setup
(define-key input-decode-map "\e[1;39" (kbd "C-M-;"))
(define-key input-decode-map "\e[1;41" (kbd "C-M-DEL"))
Still C-M-; is only recognized as M-3, and C-M-DEL is recognized as 1
EDIT
Actually it works with
(defadvice terminal-init-xterm (after map-S-up-escape-sequence
activate)
(define-key input-decode-map "\e[1;39" (kbd "C-M-;"))
(define-key input-decode-map "\e[1;41" (kbd "C-M-DEL"))
)
(define-key input-decode-map "\e[1;39" (kbd "C-M-;"))
(define-key input-decode-map "\e[1;41" (kbd "C-M-DEL"))

Related

How to correctly set this local-key?

I wrote this code to bind both Alt+c and F5 to compile
(defun dg/cedet-hook()
(add-to-list 'ac-sources 'ac-source-semantic)
(local-set-key (kbd "C-c C-j") 'semantic-ia-fast-jump)
(local-set-key (kbd "M-c") 'compile)
(local-set-key (kbd "<f5>") 'compile)
(yas-global-mode 1)
)
(add-hook 'c-mode-common-hook 'dg/cedet-hook)
But, when I open a .cpp file Emacs returns the following error:
File mode specification error: (invalid-function (local-set-key (kbd <f5>) (quote compile)))
I don't understand what's wrong in the syntax above, and why the error triggers in only the statement which uses F5. Please note that my configuration doesn't contain any other key binding related to F5.
Any clue?
Thanks in advance.

Automatically load c++ mode for HPP and CPP files

I found this thread, but the accepted solution does not work for me. I still need to manually load c++-mode for indentation (!) (not syntax highlighting, however. But that worked before anyway) to be set to c++-mode. In my init I have this code:
(setq
c-default-style "stroustrup"
)
(add-to-list 'auto-mode-alist '("\\.cpp\\'" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.hpp\\'" . c++-mode))
(defun my-c++-mode-hook ()
(setq c-basic-offset 4)
(c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
and my entire init looks like this:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(setq gc-cons-threshold 100000000)
(setq inhibit-startup-message t)
(defalias 'yes-or-no-p 'y-or-n-p)
(defconst demo-packages
'(anzu
company
duplicate-thing
;; ggtags
;; helm
;; helm-gtags
;; helm-projectile
;; helm-swoop
;; function-args
clean-aindent-mode
comment-dwim-2
;; dtrt-indent
ws-butler
;; iedit
yasnippet
smartparens
;; projectile
;; volatile-highlights
;; undo-tree
zygospore))
(defun install-packages ()
"Install all required packages."
(interactive)
(unless package-archive-contents
(package-refresh-contents))
(dolist (package demo-packages)
(unless (package-installed-p package)
(package-install package))))
(install-packages)
;; this variables must be set before load helm-gtags
;; you can change to any prefix key of your choice
;; (setq helm-gtags-prefix-key "\C-cg")
(add-to-list 'load-path "~/.emacs.d/custom")
;; (require 'setup-helm)
;; (require 'setup-helm-gtags)
;; (require 'setup-ggtags)
(require 'setup-cedet)
(require 'setup-editing)
(windmove-default-keybindings)
;; function-args
;; (require 'function-args)
;; (fa-config-default)
;; (define-key c-mode-map [(tab)] 'company-complete)
;; (define-key c++-mode-map [(tab)] 'company-complete)
;; company
(require 'company)
(add-hook 'after-init-hook 'global-company-mode)
(delete 'company-semantic company-backends)
;; (define-key c-mode-map [(tab)] 'company-complete)
;; (define-key c++-mode-map [(tab)] 'company-complete)
;; (define-key c-mode-map [(control tab)] 'company-complete)
(define-key c++-mode-map [(control tab)] 'company-complete)
;; company-c-headers
(add-to-list 'company-backends 'company-c-headers)
;; hs-minor-mode for folding source code
(add-hook 'c-mode-common-hook 'hs-minor-mode)
;; Available C style:
;; “gnu”: The default style for GNU projects
;; “k&r”: What Kernighan and Ritchie, the authors of C used in their book
;; “bsd”: What BSD developers use, aka “Allman style” after Eric Allman.
;; “whitesmith”: Popularized by the examples that came with Whitesmiths C, an early commercial C compiler.
;; “stroustrup”: What Stroustrup, the author of C++ used in his book
;; “ellemtel”: Popular C++ coding standards as defined by “Programming in C++, Rules and Recommendations,” Erik Nyquist and Mats Henricson, Ellemtel
;; “linux”: What the Linux developers use for kernel development
;; “python”: What Python developers use for extension modules
;; “java”: The default style for java-mode (see below)
;; “user”: When you want to define your own style
(setq
c-default-style "stroustrup"
)
(add-to-list 'auto-mode-alist '("\\.cpp\\'" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.hpp\\'" . c++-mode))
(defun my-c++-mode-hook ()
(setq c-basic-offset 4)
(c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(global-set-key (kbd "RET") 'newline-and-indent) ; automatically indent when press RET
;; activate whitespace-mode to view all whitespace characters
(global-set-key (kbd "C-c w") 'whitespace-mode)
;; show unncessary whitespace that can mess up your diff
(add-hook 'prog-mode-hook (lambda () (interactive) (setq show-trailing-whitespace 1)))
;; use space to indent by default
(setq-default indent-tabs-mode nil)
;; set appearance of a tab that is represented by 4 spaces
(setq-default tab-width 4)
;; Compilation
(global-set-key (kbd "<f5>") (lambda ()
(interactive)
(setq-local compilation-read-command nil)
(call-interactively 'compile)))
;; setup GDB
(setq
;; use gdb-many-windows by default
gdb-many-windows t
;; Non-nil means display source file containing the main routine at startup
gdb-show-main t
)
;; Package: clean-aindent-mode
(require 'clean-aindent-mode)
(add-hook 'prog-mode-hook 'clean-aindent-mode)
;; Package: dtrt-indent
(require 'dtrt-indent)
(dtrt-indent-mode 1)
;; Package: ws-butler
(require 'ws-butler)
(add-hook 'prog-mode-hook 'ws-butler-mode)
;; Package: yasnippet
(require 'yasnippet)
(yas-global-mode 1)
;; Package: smartparens
(require 'smartparens-config)
(setq sp-base-key-bindings 'paredit)
(setq sp-autoskip-closing-pair 'always)
(setq sp-hybrid-kill-entire-symbol nil)
(sp-use-paredit-bindings)
(show-smartparens-global-mode +1)
(smartparens-global-mode 1)
;; Package: projejctile
(require 'projectile)
(projectile-global-mode)
(setq projectile-enable-caching t)
;; (require 'helm-projectile)
;; (helm-projectile-on)
;; (setq projectile-completion-system 'helm)
;; (setq projectile-indexing-method 'alien)
;; Package zygospore
(global-set-key (kbd "C-x 1") 'zygospore-toggle-delete-other-windows)
(require 'ggtags)
(add-hook 'c-mode-common-hook
(lambda ()
(when (derived-mode-p 'c-mode 'c++-mode 'java-mode 'asm-mode)
(ggtags-mode 1))))
(define-key ggtags-mode-map (kbd "C-c g s") 'ggtags-find-other-symbol)
(define-key ggtags-mode-map (kbd "C-c g h") 'ggtags-view-tag-history)
(define-key ggtags-mode-map (kbd "C-c g r") 'ggtags-find-reference)
(define-key ggtags-mode-map (kbd "C-c g f") 'ggtags-find-file)
(define-key ggtags-mode-map (kbd "C-c g c") 'ggtags-create-tags)
(define-key ggtags-mode-map (kbd "C-c g u") 'ggtags-update-tags)
(define-key ggtags-mode-map (kbd "M-,") 'pop-tag-mark)
I got it from here: http://tuhdo.github.io/c-ide.html
And currently I am altering it to fit my needs better.
Any idea what might be conflicting here, causing
(add-to-list 'auto-mode-alist '("\\.cpp\\'" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.hpp\\'" . c++-mode))
not to work?
No idea why but commenting out
(require 'dtrt-indent)
(dtrt-indent-mode 1)
and
(require 'setup-editing)
fixed it.

How to access to MELPA packages?

I've tried to install colour themes that can be found on MELPA, but when I use "M-x install-package", they are not found.
I restart emacs after each modification of the init.el file, and refresh the list of packages before each try.
Here is my init.el file :
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
(package-initialize)
(require 'ggtags)
(add-hook 'c-mode-common-hook
(lambda ()
(when (derived-mode-p 'c-mode 'c++-mode 'java-mode 'asm-mode)
(ggtags-mode 1))))
(define-key ggtags-mode-map (kbd "C-c g s") 'ggtags-find-other-symbol)
(define-key ggtags-mode-map (kbd "C-c g h") 'ggtags-view-tag-history)
(define-key ggtags-mode-map (kbd "C-c g r") 'ggtags-find-reference)
(define-key ggtags-mode-map (kbd "C-c g f") 'ggtags-find-file)
(define-key ggtags-mode-map (kbd "C-c g c") 'ggtags-create-tags)
(define-key ggtags-mode-map (kbd "C-c g u") 'ggtags-update-tags)
(define-key ggtags-mode-map (kbd "M-,") 'pop-tag-mark)
I've installed ggtags and ahundry, but not zenburn or solarized-theme for example.
So can someone please tell me what could be wrong, because I have no idea.

How to send <C-left> into Emacs term?

I use this function to send raw commands to terminal:
(defun raw (str)
(interactive "sKey: ")
(term-send-raw-string (read-kbd-macro str)))
But read-kbd-macro for <C-left> return [C-left] which is not a string.
I've also try:
(term-send-raw-string "\C-\eOD")
and
(define-key term-raw-map (kbd "<C-left>") 'term-send-raw)
But those also doesn't work.
How can I send C-left then?
I have the following snippet in my setup file, for the exact same purpose as you: move by words on the bash prompt using C-<arrows>
(defun term-send-Cright () (interactive) (term-send-raw-string "\e[1;5C"))
(defun term-send-Cleft () (interactive) (term-send-raw-string "\e[1;5D"))
(define-key term-raw-map (kbd "C-<right>") 'term-send-Cright)
(define-key term-raw-map (kbd "C-<left>") 'term-send-Cleft)
I found the \e[1;5C and \e[1;5D codes using the following trick:
run cat >/dev/null in a terminal
type C-<left> and C-<right> and see what is echoed back in the terminal
exit with C-d or C-c
Another way to find them would be to type in a terminal: C-vC-<left>

Enabling control-c and control-v copy and paste in Emacs

How do I copy & paste in Emacs with ctrl+c and ctrl+v?
What must I add to my .emacs file?
Check out the Cua Mode.
Old question, but nevertheless.
Unbind Ctrl+C because it's modifier in Emacs:
(global-set-key (kbd "C-c") 'undefined)
Bind Ctrl+C to Copy:
(global-set-key (kbd "C-c") 'kill-ring-save)
Bind Ctrl+V to Paste:
(global-set-key (kbd "C-v") 'yank)