I am trying to save a file name that includes the date and time. However, I would like the date and time to be in UTC. This is what I am doing:
In vimrc:
cmap <F3> <C-R>=strftime("%Y%m%d%H%M")<CR>
I type this when I save the file:
:w i<F3>.txt
and I get a file that is named:
i[localtime].txt
but I want:
i[UTCtime].txt
Is there a way to actually do this, or am I stuck with my local time forever? Vim is my only way to explore various time zones, please help me. :)
Oh and by the way, I will be using both Linux and Windows for this.
Well, I'm not sure whether this will work correctly on Linux or not, but I believe so (I'm on a Mac).
The idea, is that you can use the date utility to retrieve the time with more flexibility. It accepts an option -u which outputs the time in UTC. All you have to to is wrap that in a system() call.
cmap <F3> <C-R>=system('date -u "+%Y%m%d%H%M"')<CR>
And there you have your formatted UTC time.
If a null character appears at the end of the time inserted (it appears as ^#) then you may want to append a [:-2] to strip it from the returning string, right before the <CR> in the mapping above. That comes from the translated newline the system outputs. Check :h NL-used-for-Nul.
About windows, well I have no clue. You may want to try out the utility in Linux before mapping things, but I'm almost sure there will be no differences.
Related
I have added the following line to my vim config file:
inoremap <leader>dt r! date "+\%Y-\%m-\%d<CR>
In order to add a shortcut for printing the current date. But when I use the shortcut in normal mode it only prints the string: "r! date "+%Y-%m-%d".
Why is that? What am I doing wrong?
But when I use the shortcut in normal mode it only prints the string: "r! date "+%Y-%m-%d".
You are doing that in insert mode, not "normal mode".
Why is that?
Vim is doing exactly what you ask it to do, which is to insert the string r! date "+\%Y-\%m-\%d<CR> when you press <leader>dt in insert mode.
What am I doing wrong?
The most salient item in the list would be that you didn't explain what, exactly, you are trying to achieve, asking instead for a fix to your flawed solution. This is known as the XY problem and a very common trap to fall into. In fact, since you appear to be surprised by Vim doing exactly what you ask it to do, it is possible that you are not sure about it yourself. Can you clarify this, please?
I'm trying to emulate in vim a behaviour similar to that of TextEdit.app.
As I work I often open a lot of files to take notes, and keep them there without saving them. When I restart the laptop, the TextEdit files will still be there and open thanks to AutoSave. If I do the same thing with vim (specifically MacVim) the files are (as expected) not saved and I lose their content.
My recipe for solving this problem has two bits. The first one is to automatically save the files when I'm not using them, so using a command like:
autocmd BufLeave,FocusLost * silent! wall
This works fine with files that have already been saved, but it ignores ones that have not yet been saved. This is where the second bit comes into play, I would like vim to automatically give these files a default name if it tries to save them and they don't already have a name. Possibly I would also like there to be a default save directory.
The ideal solution would be that when an unnamed file/buffer loses focus it gets saved as ~/Documents/notes/note_1.txt, the second one note_2.txt, etc etc.
I did look around for any pointers that could help in either direction (default name and default directory - the latter is not fundamental though), but couldn't find anything.
Can anybody help?
I don't like your idea, but it is doable.
You need a function:
function! SaveIt()
if bufname("%")==''
exec 'w /path/note_'.localtime()
else
w
endif
endfunction
and in your autocommand, just call the function. Some points you need to note:
the filename would be /path/note_( ms since 1970). your 1,2,3.. index will make vim check filesystem to find out the index. It could be a better name, e.g note_2013-09-11_11:11:11.233 You just change the localtime()
this may throw exception when you try to save a readonly buffer. (help, qf ...) You could check for them though.
Note that I didn't add the ! in w cmd.
it may not work for your autocmd wall. if you want to do it, you have to loop through all buffers, and for each buffer call the function.
after all the function shows the direction how it could be done, it (the quality) is still very far away from "production" level.
This may be an odd question, but still. I use cat to display a file in bash (KDE Konsole),
cat foobar.rb
Now, I would like to use Vim to colourize that foobar.rb file according to what you would get when you start foobar.rb in Vim. Edit: But only for display purpose, on the terminal.
I am not sure this is possible, but I thought it would be neat if I could use Vim for that.
I really just want colourized keywords, and Vim has the perfect colour definitions.
So I thought combining this would be great.
Is this possible in Vim out of the box though?
One approach would be to use a library such as Pygments, which is a general purpose syntax highlighter. You could write a wrapper called ccat or something that would apply syntax highlighting to an input file and write to stdout.
If you want to page up and down in a highlighted file, you can use less with the -R switch, which passes control characters through to the terminal directly, preserving colours. So:
ccat file.rb | less -R
But at that point, you're pretty much at the capabilities of view.
I'm not sure if I understand your question correctly, but if you are only looking for a command that will give you a read-only view of the input file (like cat) but with coloured keywords, use view. view is an alternative way to start vim in read-only mode, so you have all syntax highlighting possibilities. From the vim man page:
view Start in read-only mode. You will be protected from writing
the files. Can also be done with the "-R" argument.
gvim gview
The GUI version. Starts a new window. Can also be done with
the "-g" argument.
evim eview
The GUI version in easy mode. Starts a new window. Can also
be done with the "-y" argument.
rvim rview rgvim rgview
Like the above, but with restrictions. It will not be possi-
ble to start shell commands, or suspend Vim. Can also be
done with the "-Z" argument.
I have always seen view on systems that have vim installed.
Closest is the less script that comes with vim:
cat myfile | vim -u /usr/share/vim/vim72/macros/less.vim -
Note the - argument to vim. You may need to change the vim72 to your version (and the whole path if you have it installed elsewhere)
Now, this isn't exactly what you want, because its behaviour is less-like, in that you have to press keys to make it scroll down or complete. However, they are briefer than usual vim. For example, space to scroll down; and q to quit (not :q).
You want a cat-like version; me too. But there doesn't seem to be one.
EDIT uh, there's also a vimpager project, that includes vimcat - exactly what you want. But it doesn't come with vim, and I haven't tried it yet.
vim.org: http://www.vim.org/scripts/script.php?script_id=1723
github: https://github.com/rkitover/vimpager
Does anyone know if there's a way to format the date generated by strftime in Vim (under MS Windows) such that Month, Day, and Hour are not padded to two digits with a leading zero?
For example, the following commands in vimrc:
nmap <F3> a<C-R>=strftime("%I:%M %p %m/%d/%Y ")<CR><Esc>
imap <F3> <C-R>=strftime("%I:%M %p %m/%d/%Y ")<CR>
will print
03:32 AM 07/09/2010
into the file, if it was actually July 9th.
I'd like to know if there's a format string that will print
3:32 AM 7/9/2010
for vim instead. I know strftime is platform-specific, so I'm looking for a Windows-specific solution, without the use of external tools. I'd also appreciate comments to the effect that this is impossible with those constraints.
Thanks.
Some operating systems support %k and %e for the single-digit hour and day, respectively. Neither of these seem to be supported on Windows (or not on Windows XP, at least, which is all I've got nearby to try it on).
But all is not lost. Windows does support %x, which gives the date in the 7/9/2010 format you want, and we can get rid of any leading zero with a call to substitute():
echo substitute(strftime("%I:%M %p %x"), "^0", "", "")
This prints
3:50 AM 7/19/2010
Keep in mind that all of this is specific not only to the OS, but also to the current locale. This really isn't portable at all.
I don't know if there are any ways to tell strftime() how to generate what you are looking for. However, you can still apply a substitute(strftime(...), '\<0\+\ze\d', '', 'g').
EditPad Lite has a nice feature (CTRL-E, CTRL-I) which inserts a time stamp e.g. "2008-09-11 10:34:53" into your code.
What is the best way to get this functionality in Vim?
(I am using Vim 6.1 on a Linux server via SSH. In the current situation a number of us share a login so I don't want to create abbreviations in the home directory if there is another built-in way to get a timestamp.)
To make it work cross-platform, just put the following in your vimrc:
nmap <F3> i<C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR><Esc>
imap <F3> <C-R>=strftime("%Y-%m-%d %a %I:%M %p")<CR>
Now you can just press F3 any time inside Vi/Vim and you'll get a timestamp like 2016-01-25 Mo 12:44 inserted at the cursor.
For a complete description of the available parameters check the documentation of the C function strftime().
http://kenno.wordpress.com/2006/08/03/vim-tip-insert-time-stamp/
Tried it out, it works on my mac:
:r! date
produces:
Thu Sep 11 10:47:30 CEST 2008
This:
:r! date "+\%Y-\%m-\%d \%H:\%M:\%S"
produces:
2008-09-11 10:50:56
Why is everybody using :r!? Find a blank line and type !!date from command-mode. Save a keystroke!
[n.b. This will pipe the current line into stdin, and then replace the line with the command output; hence the "find a blank line" part.]
As an extension to #Swaroop C H's answer,
^R=strftime("%FT%T%z")
is a more compact form that will also print the time zone (actually the difference from UTC, in an ISO-8601-compliant form).
If you prefer to use an external tool for some reason,
:r !date --rfc-3339=s
will give you a full RFC-3339 compliant timestamp; use ns instead of s for Spock-like precision, and pipe through tr ' ' T to use a capital T instead of a space between date and time.
Also you might find it useful to know that
:source somefile.vim
will read in commands from somefile.vim: this way you could set up a custom set of mappings, etc., and then load it when you're using vim on that account.
:r! date
You can then add format to the date command (man date) if you want the exact same format and add this as a vim alias as well
:r! date +"\%Y-\%m-\%d \%H:\%M:\%S"
That produces the format you showed in your example (date in the shell does not use \%, but just %, vim replaces % by the name of the current file, so you need to escape it).
You can add a map in your .vimrc for it to put the command automatically, for instance, each time you press F3:
:map <F3> :r! date +"\%Y-\%m-\%d \%H:\%M:\%S"<cr>
(Edited the from above :) )
(Edit: change text part to code, so that
<F3>
can be displayed)
From the Vim Wikia.
I use this instead of having to move my hand to hit an F key:
:iab <expr> tds strftime("%F %b %T")
Now in Insert mode it just type tds and as soon as I hit the space bar or return, I get the date and keep typing.
I put the %b in there, because I like seeing the month name. The %F gives me something to sort by date. I might change that to %Y%m%d so there are no characters between the units.
Unix,use:
!!date
Windows, use:
!!date /t
More details:see Insert_current_date_or_time
For a unix timestamp:
:r! date +\%s
You can also map this command to a key (for example F12) in VIM if you use it a lot:
Put this in your .vimrc:
map <F12> :r! date +\%s<cr>
I wanted a custom command :Date (not a key mapping) to insert the date at the current cursor position.
Unfortunately straightforward commands like r!date result in a new line. So finally I came up with the following:
command Date execute "normal i<C-R>=strftime('%F %T')<CR><ESC>"
which adds the date/time string at the cursor position without adding any new line (change to normal a add after the cursor position).
I'm using vi in an Eterm for reasons and it turns out that strftime() is not available in vi.
Fought long and hard and finally came up with this:
map T :r! date +"\%m/\%d/\%Y \%H:\%M" <CR>"kkddo<CR>
Result: 02/02/2021 16:45
For some reason, adding the date-time alone resulted in a blank line above the date-time and the cursor set on the date-time line.
date +"[etc]" <CR>
Enters the date-time
"kk
Moves up two lines
dd
Deletes the line above the date-time
o <CR>
Opens a line below the time and adds a carriage return (linefeed)
Bonus:
vi doesn't read ~/.vimrc, it reads ~/.exrc
Also, this is how it looks in vim/.vimrc:
map T "=strftime("%m/%d/%y %H:%M")<CR>po<CR>
Another quick way not included by previous answers: type-
!!date