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

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)

Related

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>

how to set name for an interactive buffer in elisp

I have the following function defined in emacs -
(defun web2py-server ()
(interactive)
(shell-command "cd /opt/web2py; python /opt/web2py/web2py.py &"))
The above creates a new buffer in emacs, how do set a name "abc" for the window.
Thanks,
Murtaza
Use shell-command's second parameter, OUPTUT-BUFFER, for that:
(defun web2py-server ()
(interactive)
(shell-command "cd /opt/web2py; python /opt/web2py/web2py.py &"
(get-buffer-create "abc")))
You can do it interactively by calling M-x rename-buffer, or you can add the name to your function:
(defun web2py-server ()
(interactive)
(shell-command "cd /opt/web2py; python /opt/web2py/web2py.py &")
(rename-buffer "abc"))
Edit:
If you want an ongoing, interactive process, it might be better to use start-process instead of shell-command. This allows you to designate the output buffer when you start the process. Otherwise, Moritz' answer looks better than my original. If you're interested in start-process, you could start by replacing your shell-command line with the following:
(let ((default-directory "/opt/web2py"))
(start-process "my-server" "abc" "python" "/opt/web2py/web2py.py")

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.

How to (automatically) remove or prevent popping up *Async Shell Command* in emacs?

As is asked in here. I could run vi or mate within emacs.
The problem is that after running (async-shell-command "vi"), I always have the *Async Shell Command" popped up as a window.
Can I prevent popping up this windows? Or, can I modify the emacs code to remove the window as soon as it pops up?
(defun runvi ()
(interactive)
(let (filename (file-truename buffer-file-name))
(setq cmd (format "/Users/smcho/bin/mvim %s" (file-truename buffer-file-name)))
(async-shell-command cmd)))
This will work (assuming cmd is bound to the command you want, like you have above):
(save-window-excursion
(async-shell-command cmd))