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

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>

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.

Why is my term-mode-hook not selecting line mode?

I wrote this elisp function:
(defun run (command)
"Open a terminal running a command."
(interactive "sCommand: ")
(if (buffer-exists (concat "*" command "*" )) (kill-buffer (concat "*" command "*")))
(let ((term-mode-hook (cons (lambda () (term-line-mode)) term-mode-hook)))
(ansi-term (cons "sh" (cons "-i" (list "-c" command))) command)))
This works nicely except that the new ansi-term buffers remains in char mode (which is the default), so as far as I can tell the term-line-mode call is not doing anything. If I replace (term-line-mode) with (message "foo") I do see the message in the messages buffer.
The definition of term-line-mode in lisp/term.el is:
(defun term-line-mode ()
"Switch to line (\"cooked\") sub-mode of term mode.
This means that Emacs editing commands work as normally, until
you type \\[term-send-input] which sends the current line to the inferior."
(interactive)
(when (term-in-char-mode)
(use-local-map term-old-mode-map)
(term-update-mode-line)))
What am I doing wrong?
I wasn't able to get "term-line-mode" to work as you want in any of the term hooks; however, it does work if you advise the "ansi-term" function:
(defadvice ansi-term (after advice-term-line-mode activate)
(term-line-mode))

emacs equivalent of following vi command

I am looking for equivalent of following vi command
:! nl %
this runs nl command on currently open file
What is emacs way to detect name of open file ?
M-X shell-commnad nl
I am not able find determine value of current open/buffer and substitute.
Thx/Mahesh
EDIT: Misread your question as wanting to apply that change to the file you're working on. If you just want to run a shell command against a buffer, you can use shell-command-on-region, which is usually bound to M-|.
If you're just trying to get to a particular line number, M-x goto-line works. I bind that to C-x C-l by putting (define-key global-map "\C-x\C-l" 'goto-line) in my ~/.emacs.
Try this (in your ~/.emacs file):
;;; Run a shell command on all text between the mark and the point and
;;; replace with the output.
(defun shell-command-in-region (start end command &optional flag interactive)
"Execute shell-command-on-region and replace the region with the output
of the shell command."
(interactive (list (region-beginning) (region-end)
(read-from-minibuffer "Shell command in region: "
nil nil nil 'shell-command-history)
current-prefix-arg
(prefix-numeric-value current-prefix-arg)))
(shell-command-on-region (point) (mark) command t)
)
(define-key esc-map "#" 'shell-command-in-region)
Invoke it by selecting a region you want to operate on and then doing M-#.
If you always want the buffer's file name to be inserted for the shell command, you can use this advice:
(defadvice read-shell-command (before read-shell-command-with-filename activate)
"force the initial contents to contain the buffer's filename"
(if (and (null (ad-get-arg 1))
buffer-file-name)
(ad-set-arg 1 buffer-file-name)))
Once you've added the above code, M-x shell-command will always start with the buffer's file name, so you can use it in the command.
I use this:
(defun my-shell-command-on-current-file (command &optional output-buffer error-buffer)
"Run a shell command on the current file (or marked dired files).
In the shell command, the file(s) will be substituted wherever a '%' is."
(interactive (list (read-from-minibuffer "Shell command: "
nil nil nil 'shell-command-history)
current-prefix-arg
shell-command-default-error-buffer))
(cond ((buffer-file-name)
(setq command (replace-regexp-in-string "%" (buffer-file-name) command nil t)))
((and (equal major-mode 'dired-mode) (save-excursion (dired-move-to-filename)))
(setq command (replace-regexp-in-string "%" (mapconcat 'identity (dired-get-marked-files) " ") command nil t))))
(shell-command command output-buffer error-buffer))
(global-set-key (kbd "M-!") 'my-shell-command-on-current-file)
Then you can do M-! nl %

elisp: call command on current file

I want to set a key in emacs to perform a shell command on the file in the buffer, and revert the buffer without prompting. The shell command is: p4 edit 'currentfilename.ext'
(global-set-key [\C-E] (funcall 'revert-buffer 1 1 1))
;; my attempt above to call revert-buffer with a non-nil
;; argument (ignoring the shell command for now) -- get an init error:
;; Error in init file: error: "Buffer does not seem to be associated with any file"
Completely new to elisp. From the emacs manual, here is the definition of revert-buffer:
Command: revert-buffer &optional ignore-auto noconfirm preserve-modes
Thanks!
The actual error you're seeing is because you've specified the global-set-key incorrectly, namely the function call. What you want is:
(global-set-key (kbd "C-S-e") '(lambda () (revert-buffer t t t)))
You had the funcall actually evaluating when your .emacs was loading, which is what caused the error.
Then, to get the whole thing, you can create a command like:
(defun call-something-on-current-buffers-file ()
"run a command on the current file and revert the buffer"
(interactive)
(shell-command
(format "/home/tjackson/bin/dummy.sh %s"
(shell-quote-argument (buffer-file-name))))
(revert-buffer t t t))
(global-set-key (kbd "C-S-e") 'call-something-on-current-buffers-file)
Obviously customize the command, and add error checking if you want.
Maybe using the minor mode "auto-revert-mode" is an option.
Just enable it on the current buffer:
M-x "auto-revert-mode"
and always ensure the buffer is saved, before executing an external command.

run perlscript from emacs (cscript example //C:Perlscript)

i would like to create a shortcut key for emacs to execute this command:
cscript example //C:Perlscript
with example.pl being the perl script that i want to execute
i already got a shortcut key for executing perl: (global-set-key (kbd "") 'perl-eval)
how do i make this?
Try this
(defun run-command () "Run hard coded shell command" (interactive)
(shell-command (concat "cscript " buffer-file-name " //C:Perlscript")))
(global-set-key (kbd "<f2>") 'run-command)