I am trying to retrieve the output which-function-mode as a string in Emacs. I am going to create a keyboard binding shortcut that copies it to my clipboard.
It seems like this (which-func-mode ("" which-func-format " ")) is used to insert which-function into Emacs header line. However, I can't seem to get this output as a string or retrieve it in any way. There doesn't seem to be any documentation on the mode.
Any ideas for how to get the output of which-func-mode?
(which-function)
Return current function name based on point.
so to copy it to the clipboard, try
(defun my-copy-function-name ()
"Put name of function at point to kill-ring."
(interactive)
(kill-new (which-function)))
Related
When I run, M-x, <RET>, set-frame-title, I am able to change the title of an Emacs frame to whatever I want.
However, I am trying to do this programmatically from within the .emacs file. In particular, I have been trying to do something like this, but to no avail.
(setq frame-title-format '("new title here"))
How can I set the frame title from within the ELisp?
The doc for frame-title-format says:
It is used only on frames for which no explicit name has been set (see ‘modify-frame-parameters’).
There is no function set-frame-title in Emacs, but, if there were, it would have been probably using modify-frame-parameters ;-)
Here are a few copy and paste options with links to the actual string substitutions .
Option 1: is better for buffers like scratch that are temporary and don't exist on the filesystem.
Option 2: gives the absolute path to the file plus the major mode.
Option 3: is the most direct and contains both the string literal plus the buffer name by %-Construct.
(setq frame-title-format
(concat "%b - emacs#" (system-name)))
(setq-default frame-title-format '("%f [%m]"))
(setq frame-title-format "New title here - %b " )
Option 2 was pulled from here: emacs-stackexchange answer
String substitutions are know as %-Constructs , you have a lot of choices for what variables you'd like in the title. You can also find more information using the links #sds provided.
I tried to put this code in my Emacs init file:
(setq eshell-prompt-function (lambda ()
(concat "["
(user-real-login-name)
"#"
(system-name)
" "
(car (last (split-string (eshell/pwd) "/")))
"]$ ")))
It works nicely, but some errors appears:
When I push UP key (to get last command executed), I get this message in minibuffer: Not found
When I push TAB key (to autocomplete a path), I get this message in minibuffer: Invalid variable reference
I don't understand why this is happening. Could someone help me? Thanks!
Update
I entered debugger mode. When I press UP key (only UP key, not tab), debugger throws this error:
Debugger entered--Lisp error: (error "Not found")
signal(error ("Not found"))
error("Not found")
eshell-previous-matching-input("^\\[carlos#archlinux ~]\\$ " 1)
eshell-previous-matching-input-from-input(1)
funcall-interactively(eshell-previous-matching-input-from-input 1)
call-interactively(eshell-previous-matching-input-from-input nil nil)
command-execute(eshell-previous-matching-input-from-input)
Update 2
I set eshell-prompt-regexp to a regexp that matches my setup:
(setq eshell-prompt-regexp "\\[[[:alnum:]]+#[[:alnum:]]+[[:blank:]][[:print:]]+\\]\\$[[:blank:]].*")
Now, I can press UP and it works fine, but when I press TAB key to autocomplete a PATH, Emacs try to autocomplete with other 'thing'. For example, currently, I'm in a path that contains Programming directory. If I type cd Progr and I press TAB key, Emacs try to autocomplete showing this message:
Click on a completion to select it.
In this buffer, type RET to select the completion near point.
Possible completions are:
. 2to3
2to3-2.7 2to3-3.6
ControlPanel KateDJ
Magick++-config Magick-config
MagickCore-config MagickWand-config
NetworkManager Wand-config
WebKitWebDriver X
Xorg Xwayland
[ a2ping
and more. Why isn't emacs completing using directory? Lots of thanks to all comments and answers!
C-hv eshell-prompt-function states:
Make sure to update eshell-prompt-regexp so that it will match your prompt.
I believe that setting that regexp appropriately will resolve your issues.
You could doubtless come up with something even more specific than this, but the following regexp matches the prompt you're generating:
"^\\[.*?#.*? [^/]*?\\]\\$ "
This regexp is matching (at the beginning of a line) [x#y z]$ where x and y are sequences of non-newline characters (. does not match newlines, which isn't a problem here), and z is a sequence of non-/ characters (on account of the split-string call in your prompt function splitting on /).
*? is the non-greedy version of *, matching as few instances of the preceding pattern as possible/necessary; thus ensuring that we can't inadvertantly match text beyond the end of the prompt.
I want the following behavior:
Append the word under cursor into a file(~/vocabulary.txt, for example)
Better still to bind a key for it.
Could anyone show me how to do it?
Should I put those code into .emacs ?
Try the following function:
(defun my-write-to-file ()
"Save word at point to file"
(interactive)
(write-region (concat (thing-at-point 'word) "\n") nil "~/vocabulary.txt" 'append))
When called, this function will save the word at point (the word the cursor is on or the word right before the cursor) to ~/vocabulary.txt.
You can bind it to a key (C-c w in this case, but you can change it to whatever you like) like this:
(global-set-key (kbd "C-c w") 'my-write-to-file)
To use, simply put the function and the keybinding assignment in your .emacs.
#Elethan wrote you a command that does just what you ask for, and bound it to a key.
It might also help to mention some general commands that you can use for this kind of thing. M-x append-to-file appends the region contents to a file, and M-x write-region prepends.
The manual is your friend for things like this. See nodes Misc File Ops and Accumulating Text.
Be aware too that for the two commands just mentioned, as the manual says about append-to-file (it should say it about both):
You should use append-to-file only with files that are not being
visited in Emacs. Using it on a file that you are editing in Emacs
would change the file behind Emacs’s back, which can lead to losing some
of your editing.
Accumulating Text also tells you about commands for adding text to a buffer, including the case of adding to a buffer for a file that you are visiting (as opposed to what the above quote warns you about for append-to-file). These include commands append-to-buffer and prepend-to-buffer.
I am dabbling in Emacs Lisp and I am trying to write the following function:
(defun buffer-file-name-body ()
(last (split-string (buffer-file-name) "/")))
What I am trying to achieve is to extract just the file name and extension from the full path given by (buffer-file-name). However, this implementation returns a list of one item ("scratch.el") ... I tried several things such as passing the result of (last) through (string) but that raises an error... Google did not return anything useful when I searched for Emacs List convert list to string. How to I do this?
It sounds to me like what you want is (file-name-nondirectory (buffer-file-name)) which returns the simple file name, sans any directory information, as a string.
I want to copy a string to the clipboard (not a region of any particular buffer, just a plain string). It would be nice if it were also added to the kill-ring. Here's an example:
(copy-to-clipboard "Hello World")
Does this function exist? If so, what is it called and how did you find it? Is there also a paste-from-clipboard function?
I can't seem to find this stuff in the Lisp Reference Manual, so please tell me how you found it.
You're looking for kill-new.
kill-new is a compiled Lisp function in `simple.el'.
(kill-new string &optional replace yank-handler)
Make string the latest kill in the kill ring.
Set `kill-ring-yank-pointer' to point to it.
If `interprogram-cut-function' is non-nil, apply it to string.
Optional second argument replace non-nil means that string will replace
the front of the kill ring, rather than being added to the list.
Optional third arguments yank-handler controls how the string is later
inserted into a buffer; see `insert-for-yank' for details.
When a yank handler is specified, string must be non-empty (the yank
handler, if non-nil, is stored as a `yank-handler' text property on string).
When the yank handler has a non-nil PARAM element, the original string
argument is not used by `insert-for-yank'. However, since Lisp code
may access and use elements from the kill ring directly, the string
argument should still be a "useful" string for such uses.
I do this:
(with-temp-buffer
(insert "Hello World")
(clipboard-kill-region (point-min) (point-max)))
That gets it on the clipboard. If you want it on the kill-ring add a kill-region form also.
The command to put your selection on the window system clipboard is x-select-text. You can give it a block of text to remember. So a (buffer-substring (point) (mark)) or something should give you what you need to pass to it. In Joe's answer, you can see the interprogram-cut-function. Look that up for how to find this.
In my .emacs file, i use this
(global-set-key "\C-V" 'yank)
(global-set-key "\C-cc" 'kill-ring-save)
I could not use Ctrl-C (or System-copy), but this may be enough in case old habits kick in.