How to insert current time in the emacs org-mode - emacs

Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon 16:44]) in the org-mode? In the manual there is a lot of stuff (clocks, deadlines, schedules), but most of them require entering the time manually.

C-u C-c . will insert a timestamp at point, though Org will still prompt you for a time (with the default being the current time).
More: "Creating timestamps" in the Org Mode manual.

In my installation, which is org-mode version 9, the following enters the current date and time without prompting anything
C-u C-u C-c .

C-u C-c !
inserts an inactive timestamp with the current time such as:
[2018-05-08 Tue 00:30]

In emacs-lisp, you could use
(org-insert-time-stamp (current-time) t)
With default settings, it will generate a time stamp on the format
<2021-06-20 Sun 10:33>
If you want to access it from any emacs session, put
(defun insert-now-timestamp()
"Insert org mode timestamp at point with current date and time."
(interactive)
(org-insert-time-stamp (current-time) t))
in your .emacs file. You may then call the function with M-x insert-now-timestamp.
Emacs 27.2, org mode 9.4.4.
Edit: I now realise this does the same thing as #anachronic's solution. I however leave it here for reference.

On my installation
C-u C-c .
inserts a date-with-time stamp

Just give you some other options:
If you are using windows system, you can use AutoHotKey to achieve this.
If you can install YASnippet for emacs, it also have the shortcut to insert current date and time.
These two options are very powerful tools, not for just insert date and time.

Related

How to insert date only in org-mode

I can insert an inactive timestamp interactively with org-time-stamp-inactive (C-c C-!), then pressing Enter when presented with the calendar. This inserts a value like [2021-12-10 Fri].
I would like to know if this can be achieved programmatically with org-time-stamp-inactive. I tried org-time-stamp-inactive '(16) but this also inserts a time which I do not want. I can do insert (format-time-string "[%Y-%m-%d %a]") but this uses a different approach, and I would like to know if what I want can be achieved with org-time-stamp-inactive.
EDIT (in response to comment): No, it is not possible, but you got to look at the code to make sure. org-time-stamp-inactive is a thin wrapper around org-time-stamp. That in turn calls org-insert-time-stamp, which takes a time and a with-hm argument (among others - see the doc string of the function with C-h f org-insert-time-stamp for the details). If with-hm is nil, then no time is printed. But org-time-stamp (and therefore org-time-stamp-inactive) always calls org-insert-time-stamp with a non-nil with-hm, so the time is always inserted in that case. The best you can do is call the underlying function org-insert-time-stamp like so: (org-insert-time-stamp (current-time) nil 'inactive).
I still think your simple suggested solution (elaborated a bit in the original answer below) is the best way to go about the problem.
[ORIGINAL ANSWER]
Org mode is plain text: things may appear differently, but all that is smoke and mirrors: you can cat an Org mode file in the terminal and see precisely what it contains. In particular, inactive time data are just strings of the form [2021-12-11]. So there is no penalty if you do it manually, instead of going through some Org mode interfaces: the end result is the same, so your manually inserted date is just as good as the one inserted by org-time-stamp-inactive.
So assuming that the simple solution you suggested is acceptable, here's filling in the details:
(defun my/org-insert-current-time-as-inactive-time-stamp ()
(interactive)
(insert (format-time-string "[%Y-%m-%d]")))
(define-key org-mode-map (kbd "C-c _") #'my/org-insert-current-time-as-inactive-time-stamp)
C-c _ was undefined in the Org mode keymap in my case, but YMMV: choose something that is not used.

Elisp - Avoid prompt in interactive function

I'm trying to call the following org-mode function to insert the current timestamp in the buffer. The function is called by a script.
(org-time-stamp-inactive)
This, as expected, brings up a prompt asking for the date to use for the timestamp. But I want to skip the prompt and insert the timestamp directly. Is that possible at all? Haven't found anything that could help me.
This should insert the current inactive time stamp:
(org-insert-time-stamp nil nil t)
org-time-stamp unconditionally¹ calls org-read-date to prompt the user for a date. You can't pass a date. But you can locally bind org-read-date to a function that returns the date that you want to use.
(require 'cl)
(flet ((org-read-date (org-with-time &rest args)
(format-time-string (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
(current-time))))
(org-time-stamp-inactive with-time))
¹ Except sometimes when there is already a time stamp, but that's no help.
I ended up creating the timestamp from scratch with the following call:
(insert (format-time-string "[%Y-%m-%d %a]"))
You can find the answer in documentation for similar function (org-time-stamp)
[...] With two universal prefix arguments, insert an active timestamp
with the current time without prompting the user. [...]
So.. all you need to do is press C-u C-u before you invoke the function (for example C-u C-u M-x org-time-stamp-inactive RET)

How to force Org-mode to open a link in another frame?

In Org-mode it is possible to have links and top open links. As listed by http://orgmode.org/orgcard.txt in Org-mode C-u C-c C-o or mouse-3 forces links to open in another window. How can I do the corresponding for frames, that is, how can I force a link to open in another frame?
What I want is for C-c C-o to work as per default but C-u C-c C-o to force the link to be opened in another frame.
(For the distinction of windows and frames see http://www.gnu.org/software/emacs/manual/html_node/emacs/Frames.html.)
I am running Org-mode 7.6 in 23.3.1.
I just tested and you can get it to work by wrapping org-open-at-point in a (let ) as a custom function.
In this case I'm just prefixing the current org-link-frame-setup with your desired find-file-other-frame to ensure that if you use the command on another link type it will not fail.
(defun zin/org-open-other-frame ()
"Jump to bookmark in another frame. See `bookmark-jump' for more."
(interactive)
(let ((org-link-frame-setup (acons 'file 'find-file-other-frame org-link-frame-setup)))
(org-open-at-point)))
I suspect you will need to bind it to a key sequence other than C-u C-c C-o, unless Emacs will permit you to bind it to that sequence specifically.
Have a look at the variable org-link-frame-setup (M-x customize-variable RET org-link-frame-setup). The docstring should explain the approach.
I just tested this in a non-customized emacs:
emacs -q
GNU Emacs 24.0.92.1 (i386-mingw-nt5.1.2600) of 2011-11-30 on MARVIN
Org-mode version 7.7
When running C-c C-o and C-u C-c C-o on a link similar to the following:
file:~/Dropbox/org/test.org::*Test
I end up with a new frame being opened in both cases. C-u C-u C-c C-o opens the test.org file in my active emacsclient session. When changing the link to .../org/test.txt it still opens in a new frame, however it is unable to create the new file (I'm assuming I don't have .txt properly associated on my end, which is possible).
Testing with [mailto:test#test.org][test-mail]] also results in new frames by default.
Edit: The value for org-link-frame-setup is as follows (it is also the default since it is from emacs -q):
Its value is ((vm . vm-visit-folder-other-frame)
(gnus . org-gnus-no-new-news)
(file . find-file-other-window)
(wl . wl-other-frame))

How can I more easily switch between buffers in Emacs?

I've recently started using emacs and I'm enjoying using it for the most part. The only thing I'm not enjoying, is switching between buffers. I often have a few buffers open and I've grown tired of using C-x b and C-x C-b, are there any packages that make switching between buffers easier? I've looked into emacs wiki on switching buffers and I'd appreciate insight/feedback on what are are using/enjoying. Thanks.
UPDATE: iswitchb-mode is obsolete in Emacs >= 24.4, replaced by ido.
All of the features of iswitchdb are now provided by ido. Ross provided a link to the documentation in his answer. You can activate with the following in your .emacs (or use the customization interface as Ross suggests):
(require 'ido)
(ido-mode 'buffers) ;; only use this line to turn off ido for file names!
(setq ido-ignore-buffers '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
By default, ido provides completions for buffer names and file names. If you only want to replace the features of iswitchb, the second line turns off this feature for file names. ido will ignore any buffers that match the regexps listed in ido-ignore-buffers.
The behaviour described below for iswitchb-mode applies equally to ido for switching buffers.
iswitchb-mode (Emacs < 24.4)
iswitchb-mode replaces the default C-x b behaviour with a very intuitive buffer-switching-with-completion system. There are more sophisticated options, but I've never needed more than this.
After you hit C-x b, you are presented with a list of all buffers. Start typing the name of the buffer you want (or part of its name), and the list is narrowed until only one buffer matches. You don't need to complete the name, though, as soon as the buffer you want is highlighted hitting enter will move you to it. You can also use C-s and C-r to move through the list in order.
You can turn it on by default with this in your .emacs:
(iswitchb-mode 1)
You can also tell it to ignore certain buffers that you never (or very rarely) need to switch to:
(setq iswitchb-buffer-ignore '("^ " "*Completions*" "*Shell Command Output*"
"*Messages*" "Async Shell Command"))
You can use C-x <right> (next-buffer) and C-x <left> (previous-buffer) to cycle around in the buffer ring. You could bind S-<right> and S-<left> to these functions. (S is the "super-key" or windows-key). This way you can save some keystrokes.
Moreover, note that C-x b has a default entry, i.e. it displays a standard value (most of the time this is the previously viewed buffer), so that you don't always need to enter the buffer name explicitly.
Another nice trick is to open separate windows using C-x 2 and C-x 3. This displays several buffers simultaneously. Then you can bind C-<tab> to other-window and get something similar to tabbed browsing.
M-x customize-group ido then set Ido Mode to Turn on both buffer and file and set Ido Everywhere to on. Then click the Save for future sessions button at the top and enjoy ido magic for both files and buffers. Read the docs to get a sense of how to use ido.
Also, take a look at smex.
ido-mode provides an efficient way to switch buffers.
ibuffer is best for managing all opened buffers.
anything is good for selecting an interested thing from different
sources. (for eg: a single key can be used to switch to another
buffer or to open recently closed file or to open a file residing
in the same directory or ... anything you want ... )
If you've looked at the Emacs Wiki, you probably have all this information already, but here are a few other relevant Q&As:
Emacs: help me understand file/buffer management
Buffer switching in Emacs
How to invoke the buffer list in Emacs
My toolkit consists of ibuffer, windmove+framemove, winner-mode, and a custom binding to make C-xleft/right and C-cleft/right less of a hassle to use.
I have mapped the "§"-key to 'buffer-list and I find it to be very efficient.
I've started using anything for a couple of days and I'm really liking it: http://www.emacswiki.org/emacs/Anything .
Emacs-fu has an good intro to anything: http://emacs-fu.blogspot.com/2011/09/finding-just-about-anything.html
My favourite function for this is helm-mini which is part of helm.
As other helm functions, it allows incremental narrowing of the selection. It also searches your recently visited buffers, which is a really nice way to re-open a buffer. Helm can be a little surprising at first and as a new Emacs user, I found it visually overwhelming and I preferred ido or ibuffer which have been suggested in other replies. But now I absolutely love it and use it all the time for countless things.
Something that I realized by accident and that can be useful:
mouse-buffer-menu is by default bound to <C-mouse-1> (Control key + mouse left click) and opens a popup with a list of the current buffers.

How can I save my mini-buffer history in Emacs?

I'd like to save, for instance, my find-file and Meta-X history in Emacs' mini-buffer so I can recall commands later in a different session.
As Trey Jackson said, you want to put this:
(savehist-mode)
in your Emacs start up file, then restart Emacs. (Calling it interactively will stomp on your current mini-buffer history, so you may not want to do that.)
It's also worth pointing out that you can persist other variables across sessions by adding them to savehist-additional-variables, like so:
(setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring))
You may also want to customize savehist-file, to pick the location where Emacs saves all this stuff:
(setq savehist-file "~/.emacs.d/tmp/savehist")
M-x savehist-mode
or
(savehist-mode 1)
(available in Emacs as of 22.1)
If you have an Emacs version older than 22, you can use this instead:
http://www.emacswiki.org/emacs/download/savehist-20%2b.el