Adding timestamp in vim, in config file - vim

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?

Related

How to correct spelling in entire file?

Spelling mistakes can be fixed by pointing the cursor on the misspelled word and pressing z= (a list appears and you can select correct word). But this will fix the mistake only once (on current word). This is not that much of us will want, most will be happy to do replacement in entire file. How can I do so?
Unsuccessful Attempt:
I got an idea of substitute that mistake with correct one (i.e. :%s/badspell/goodspell/g). But the bad part of this is, it is painful. First I need to (atleast) correct a word, then store bad spelling and corrected spelling in my buffer (brain), then write the command for substitution (chances are I will do mistake in writing bad spelling and good one.). Though its time taking too.
There's a similar question on StackOverflow which says I can solve my problem with doing :spellrepall after the correction. The only problem is I don't want to all this typing. I want to automate this everytime there is a spelling correction.
And don't suggest me about plugins.
you can set the spell on in .vimrc
set spell
or if u execute this on vim directly do in vim command mode.
:set spell
Once the spell is on, you need to know following command:
]s — move to the next mispelled word
[s — move to the previous mispelled word
zg — add a word to the dictionary
zug — undo the addition of a word to the dictionary
z= — view spelling suggestions for a mispelled word
If most of the time you're selecting the first fix from the query you can use a remap for 1z= as such:
nnoremap 1z= 1z=:spellrepall<cr>
Although I would recommend binding :spellrepall to a seperate key as many people already suggested. I highly doubt you have run out of keybindings with all those <leader> and <localleader> combos. (something like <leader>z would be a perfect candidate for this)
I don't think this can go any easier without you loosening your expectations.

What does "1 line >ed 1 time" mean in VIM?

Lately I have been encountering a problem in VIM.
I use the shortcut >> (hold shift, press the period key twice) to indent a line. Sometimes I must be hitting something wrong because this shortcut stops working until I restart VIM.
Every time I try to indent with the shortcut it says "1 line >ed 1 time" instead of indenting. Or, if I have 3 lines selected and try to indent them all it will say: "3 lines >ed 1 time".
How do I fix this and restore the shortcut?
Thanks!
I have no idea, why your shortcut is not working, or what's the wrong key that you have pressed.
Regarding your 1 line >ed 1 time message: What you're doing is to shift a number of lines to the right. Vim is just notifying you about what was done, which is: One line is right-shifted one time. The > here is the right shift operator and >ed is just short for "shifted". If you do the opposite it says <ed, which is shifted to the left.
You can find more details in vim's help with: :help >
When a section is marked (visually marked) one > is sufficiant. The 2nd > should start another indentation, but since now nothing is marked it waits for another >.
More than this, I couldn't reproduce any error. (As it was mentioned by frosch03, the msg is just the normal response)
ed was an (well, probably still is) old editor, but I doubt that is what Vim is telling you. I cannot reproduce the message you're getting, nor find anything in the documentation regarding it.
Does this happen also when you're using a clean Vim (when you start it without vimrc with vim/gvim -u none)? If so, could you paste your vimrc if it's not too big somewhere (one of those paste sites should do nicely), so we can take a look to see is there any weird combo inside causing that behaviour.
Apart from that, not much advice I can offer regarding the given data :/

How can I save a filename with UTC time in vim?

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.

Is there a way to paste the output of a vim command into the buffer?

Something that I want to do from time to time is paste the output of a vim command into the buffer. E.g. when I'm editing my vimrc, it'd be nice to be able to fiddle with statusline and then be able to just do something akin to
"=set statusline?<Enter>p
Problem is, that yields
E121: Undefined variable: set
E15: Invalid expression: set statusline?
Press ENTER or type command to continue
I figure that this is possible, and that I just don't know enough about the builtin functions and how to use them (I see expand used here and there, but have never successfully made it work for me in any context), even though I (think that I) have a pretty solid understanding of normal mode.
Note that this specific example is a little contrived, but I can't think of a better one right now. For the specific use case above, I could just ":p to get the whole set command that I used during experimentation and then edit to suit, but fairly regularly I run into other cases where I want vim to tell me something and then I want to paste that output somewhere so that I can continue to look at it while continuing with my work.
You can paste an option setting:
"=&statusline<Enter>p
I don't know of any way to put the output of an arbitrary command in the buffer, however.
The values of settings are stored in variables that are prepended with an & symbol. So the value that statusline is set to can be accessed by referencing &statusline. To insert into a document one way is to use the "expression" register, <ctrl-R>=. To use it enter insert mode and press <ctrl-R> and then =. You will see an equals sign in the command line, where you can enter: &statusline and then press enter. This will insert the value into the buffer.

Does Vim have an auto-comment feature based on the file's syntax?

I'm not sure this is possible, but I'm interesting in making this happen.
Ideally, I would like to map this feature to SHIFT+CTRL+3.
I'm looking for a way to have Vim enter a comment (single line) which corresponds to the syntax of the file I'm editing. If there are multiple single-line comment styles, Vim could either automatically pick one, or give me the choice. If the single-line comment has two parts (e.g. /* and */), then pressing SHIFT+CTRL+3 the first time will start the comment, and the second time will close the comment.
Examples:
Python: #
JavaScript: //
C, C++: /* with */ or //
I know there are scripts which will insert comments for you, but I haven't seen any that will do this based on the syntax of the file.
I highly recommend NERD Commenter.
Sort of! I don't believe vim will do this out of the box, but you can install plugins that will do fairly intelligent commenting (using movement keys, visual line highlighting, etc) that are specific to the filetype being edited. You can get these plugins off of vim.org, and you should be able to make your own key mappings in your .vimrc file if you don't like the ones they come with.
tComment is pretty well regarded, and has worked for me.
I've heard that EnhCommentify might be better, but I haven't used it myself.
Seems like a similar question to this:
How to comment in vim while respecting the indent?
Use the nerd commenter plugin:
http://www.vim.org/scripts/script.php?script_id=1218
See: this script which provides a function to commented a highlighted area in visual mode.
You want to start a comment in insert mode so your function would look more like:
fun CommentLines()
exe ":s#^#".g:Comment."#g"
endfun
Not quite what you're looking for, but efficient, and I suppose you know which comment to use.
(all this in command mode)
Put your cursor to the first line you want to comment. We willl then set a marker called a (valid names are a-z, single character) by typing
ma
put the cursor to the last line, then set a marker called b by typing
mb
Then comment the whole block (by searching for a newline and inserting the comment character (note the use of "#" as search delimiter because otherwise wee have to escape the "/")
:'a,'bs#^#//#
or for Python:
:'a,'bs/^/#/
To uncomment:
:'a,'bs#^//##
As we do line comments, it doesn't matter if we have other comments already in the file, they will be preserved.

Resources