For example, I use the command local-set-key to set the key C-c C-n for flymake-goto-next-error in python-mode.
Instead of writing an expression and wrapping it into python-mode-hook, is there a convenient way to store this keybinding directly? Does anyone have ideas about this?
Command
(local-set-key (kbd "C-c C-n") 'flymake-goto-next-error)
works in the current buffer's local map. It is correct when local map is python-mode-map.
Convenient minimum lenght command (without using hook)
(define-key python-mode-map (kbd "C-c C-n") 'flymake-goto-next-error)
works when variable python-mode-map has been created.
Variable python-mode-map is created dynamically (in file python-mode.el) after call
(require 'python-mode)
If python-mode hasn't been loaded, command define-key can't directly use python-mode-map. Using hooks is for safety. Your emacs config should be reliable (it shouldn't depend on command's execution order if possible), and such wrapped (with hooks) commands prevent wrong situation: setting keybind in mode-map without setup mode.
Related
In Sublime Text (Mac OS X), you can do CMD + R shortcut to list function definitions in a popup. When you type function names, it does fuzzy matching to show matches. Press enter and go to the definition. I find it very handy.
In Emacs, is there something like that?
I'm using elpy for writing Python and I found elpy-occur-definitions (C-c C-o) which can show function definitions in a different buffer. Close. But not exactly what I want.
Thanks!
Thanks to #jenesaisquoi's comment, I found helm-imenu-anywhere
which does exactly what I want.
I override this key-binding M-r (since I don't use the navigation function that often) which is also very close to Cmd-R.
(global-set-key (kbd "M-r") 'helm-imenu-anywhere)
UPDATE:
If you are on Mac OS X and want to use the same CMD + R to do this, you ca bind the key to s-r instead:
(global-set-key (kbd "s-r") 'helm-imenu-anywhere)
UPDATE 2:
I found that, to list functions in current buffer (versus out of all files), the best trick is helm-semantic-or-imenu. So my ultimate setup is
(global-set-key (kbd "s-r") 'helm-semantic-or-imenu)
(global-set-key (kbd "s-o") 'helm-imenu-anywhere)
Is there an equivalent function to eval-print-last-sexp for comint derived modes in emacs?
Specifically, I'm using python-mode (with elpy) and am looking for a way to send the contents of a region to the Python process and then print the results on the next line of the python script I'm working in.
Printing the results to the Messages buffer would be acceptable as well, however the behavior of eval-print-last-sexp would be preferred.
I'm running Emacs 25.1.1 and elpy 1.13.0.
It would depend on the comint-derived mode, since you would need to redirect the process output. Different modes have different methods of interacting with inferior processes. Python mode already has a function to do this, python-shell-send-string-no-output (other modes have similar functions, but you would need to search for them).
I'm not sure how exactly you want to define a sexp for python, but here is an example of sending the current line, with output like eval-print-last-sexp.
(defun python-eval-print-last-sexp ()
"Print result of evaluating current line into current buffer."
(interactive)
(let ((res (python-shell-send-string-no-output
;; modify to get a different sexp
(buffer-substring (line-beginning-position)
(line-end-position))))
(standard-output (current-buffer)))
(when res
(terpri)
(princ res)
(terpri))))
elpy-shell-send-statement from the Emacs Python Development Environment.
When I am debugging code in Emacs, I like to have two buffers open, the first one with the source code I am debugging, and the second one with the debugger (pdb for Python).
I have the following keyboard shortcuts defined on my .emacs file:
(require 'gud)
(define-key gud-mode-map '[C-f10] 'gud-next)
(define-key gud-mode-map '[C-f11] 'gud-step)
(define-key gud-mode-map '[C-f5] 'gud-cont)
(define-key gud-mode-map '[C-f12] 'gud-break)
With the above, I can trigger the GUD shortcuts for gud-next, gud-step, etc. from the buffer where pdb is running, but I can't trigger them from the buffer that has the python code.
I would like to use keyboard shortcuts on the buffer with the source code to trigger GUD commands for the debugger. Is there any way to do this?
I am using the most recent version of python-mode (6.0.4) and Emacs 23.3.1.
Try using global-set-key instead:
(global-set-key [C-f10] 'gud-next)
(global-set-key [C-f11] 'gud-step)
(global-set-key [C-f5] 'gud-cont)
(global-set-key [C-f12] 'gud-break)
IIRC, this worked for me.
I am a fairly proficient vim user, but friends of mine told me so much good stuff about emacs that I decided to give it a try -- especially after finding about the aptly-named evil mode...
Anyways, I am currently working on a python script that requires user input (a subclass of cmd.Cmd). In vim, if I wanted to try it, I could simply do :!python % and then could interact with my script, until it quits. In emacs, I tried M-! python script.py, which would indeed run the script in a separate buffer, but then RETURNs seems not to be sent back to the script, but are caught by the emacs buffer instead. I also tried to have a look at python-mode's C-c C-c, but this runs the script in some temporary directory, whereas I just want to run it in (pwd).
So, is there any canonical way of doing that?
I don't know about canonical, but if I needed to interact with a script I'd do M-xshellRET and run the script from there.
There's also M-xterminal-emulator for more serious terminal emulation, not just shell stuff.
I like to use the Emacs "compile" command to test/run my python scripts. M-XcompileRET will pull up the default "make -k" but if you delete that and put in the command line for your script (including options), subsequent "compiles" will provide the new "compile" command automatically. All the output from your script will appear in the compile buffer. (As opposed to the shell, this provides a nice clean buffer each time it is invoked. Good for searching and such. If you forget to save your script before your run, compile will ask you if you would like to save the file.)
You will lose your the command line when you restart Emacs. But you can get Emacs to set the compile-command for the buffer holding your script by putting at the bottom of the python script this sort of code (actually a python comment):
# Trigger emacs to run this script using the "compile" command
# ;;; Local Variables: ***
# ;;; compile-command: "my_cool_script.py --complicated_option some_filename.txt" ***
# ;;; end: ***
This is handy for scripts with complicated invocations.
Note: The python comment character '#' protects this from the python interpreter while Emacs knows to set these variables because it looks at the bottom of every file when it opens them.
I'd love to be able to jump to 'compile errors' in my python script the way the compile command does when you use it for compiling C code but I'm too lazy to create the Emacs regular expression to make this work. Perhaps that would make another great question for stack overflow!
I currently use these hook to define my compilation commands:
(defun convert-filename-to-executable (file)
(if (eq system-type 'windows-nt)
(concat (file-name-sans-extension file) ".exe")
;; linux
(concat "./" (file-name-sans-extension file))))
(add-hook 'c++-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer- file-name))
(executable (convert-filename-to-executable file)))
(concat "g++ -g -Wall -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'c-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer-file-name))
(executable (convert-filename-to-executable file)))
(concat "gcc -g -ansi -Wall -Wpedantic -Wextra -Wc++-compat -Wconversion -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
(add-hook 'perl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
Together with this lambda set to call the compile function interactively:
(global-set-key (kbd "<f4>") (lambda () (interactive) (setq current-prefix-arg '(4)) (call-interactively 'compile)))
One button to rule them all!
If you press F4 (in my case, you can set the key yourself in the lambda for global-set-key), then a file opened in C++ or C mode will be compiled and a file in python or perl mode will be run (interactively)
The I think ansi-term has the most faithful emulation of a terminal. But I don't see a way to pass arguments to the process. You can of course just launch it from a shell inside the ansi-term buffer.
But I think the best thing to do is to not use python-send-buffer, but instead to use a new function which does it "right", that is by sending the path to the current file instead of making a temp file. There are some slight differences of course in that you have to save the current file first, but the following should at least get you on the right track.
(defun python-send-file ()
(interactive)
(save-buffer)
(python-send-string (concat "execfile('" (buffer-file-name) "')")))
;; This overwrites the `python-send-buffer' binding so you may want to pick another key
(eval-after-load "python"
(define-key python-mode-map "\C-c\C-c" 'python-send-file))
I checked and this allows you to interact. To get tabs you have a few options.
C-qTAB will always give you a literal tab
You can rebind tab to be a literal tab in inferior-python-mode-map:
(define-key inferior-python-mode-map "\C-i" 'self-insert-command)
I'm sure there are others that I can't think of
If you use C-c C-c a buffer is created (look for inferior-python). Try changing to that buffer*, every time you hit C-c C-c the result is shown there, you need to see that buffer to get the results. Use C-x 2 so you can see both buffers at the same time.
Also try C-c C-z (switch to shell).
*I use Ibuffer to manage buffers, is very good.
(btw, this http://tuhdo.github.io/index.html is an excelent place to learn some emacs)
EDIT: Have you tried C-c C-p ?
Yet another option:
Using C-c C-c works nicely with fgallina's python.el -- pwd will be the location of the buffer's file.
recently I started using Emacs as a Python IDE, and it not quite intuitive... The problem I am struggling with right now is how to pass command line arguments to the inferior python shell when the buffer is evaluated with C-c C-c. Thanks for help.
This doesn't appear to be easily possible; the inferior process managed by the python.el module is designed to persist across many invocations of python-send-buffer (and friends). One solution I've found is to write your own function that sets sys.argv programmatically from within the inferior process:
(defun python-send-buffer-with-my-args (args)
(interactive "sPython arguments: ")
(let ((source-buffer (current-buffer)))
(with-temp-buffer
(insert "import sys; sys.argv = '''" args "'''.split()\n")
(insert-buffer-substring source-buffer)
(python-send-buffer))))
Execute this function in your *scratch* buffer and/or save it in your .emacs file, then, if you want, bind it to a convenient key sequence. C-c C-a doesn't seem to be used by python-mode, so perhaps:
(global-set-key "\C-c\C-a" 'python-send-buffer-with-my-args)
The command will prompt you for arguments to use, then copy your source buffer into a temporary buffer, prepending it with a code snippet that sets sys.argv to the list of arguments you supplied, and finally will call python-send-buffer.
The above code will just naively split the string you type on whitespace, so if you need to supply arguments that have whitespace in them, you'll need a more sophisticated algorithm.