Vimscript: one command is not calling the other - vim

I have the following code in my vimrc file:
" move line to end of file and add a timestamp
noremap ,d ddGp,t
" append a timestamp to the end of the line
nnoremap ,t A <Esc>"=strftime("%H:%M")<CR>p
In the above code, ,t works when called by itself. However, when called by ,d the ,t command does nothing. The rest of the ,d command functions as expected. Why is this? How do I fix it?

,d is marked as nore which turns recursive mappings off, and thus fails to recognize ,t as a command. Thank you FDinoff for the answer.
More info:
What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?

Related

Vim keyboard shortcut to replace ^M

I manually replace ^M with
:%s/<ctrl-v><ctrl-m>//g after I open a log file. I have to do this several times a day so I tried to assign F11 key to perform this action.
I tried the following in .vimrc but it did not work.
:nnoremap <silent> <F11> :let _s=#/ <Bar> :%s/^M//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
Note: I pressed ctrl+vctrl+m to get ^M in the above statement.
I have a similar assignment for F10 to remove EOL whitespace and that works.
:nnoremap <silent> <F10> :let _s=#/ <Bar> :%s/\s\+$//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
What am I doing wrong here?
Check that your terminal actually forwards F11 to the commands it’s running instead of listening to the key itself. For instance, the default configuration of Windows Terminal intercepts F11 and does not forward it, so you can’t rebind the key inside Vim.
Furthermore, your command looks quite convoluted; my Vimscript knowledge is limited but if the purpose of your command is to change Windows file endings into UNIX file endings, the following, simpler command works:
:nnoremap <silent> <F11> :set fileformat=unix<CR>
There’s also a widely installed utility program, dos2unix, that does the same, and which might be even simpler for your use-case.

Hitting ENTER on quickfix error jumps to an empty buffer

I'm using GVim 8.1 on Windows 10 with no external plugins.
I have the following set up in my .gvimrc file:
let g:build_file_abs_path = fnamemodify(findfile("windows-build.bat", ";."), ":p:h")
" This build script is a basic wrapper for 'clang.exe file.c -o file.exe' style invocation
let &makeprg=g:build_file_abs_path . "\\windows-build.bat"
nnoremap <silent> <C-B> :cd <C-R>=g:build_file_abs_path<CR> <bar> make! <bar> copen <bar> redraw <bar> cd -<CR>
Now, this automatically opens a quickfix window with the correct compiler output. However, when I press ENTER over the error, the cursor jumps to the buffer for the affected file, yet it is completely blank with a single line. Furthermore, this occurs as I use :cn and :cp commands inside the quickfix window. e.g:
Images showing these two states:
before
after
Please note that:
:verbose nmap <CR> returns no mappings, so there is not conflict there.
I would appreciate it if someone could provide some insight as to how to avoid the buffer becoming empty and actually jump to the error in the appropriate file. Many thanks.
Thanks to Christian Brabandt's comment, I was able to solve the issue. I was misunderstanding the distinction between the working directories of vim and the build script. I made the following changes:
let &makeprg="cd " . g:build_file_abs_path . " && windows-build.bat"
nnoremap <silent> <C-B> :make! <bar> copen <bar> redraw <CR>

What is wrong with this line? nnoremap <C-[> :execute "normal! A{{{\<Esc>"<CR>

I am trying to add this to my vimrc but I am getting problems one it inserts {{{ at the beginning of my vimrc whenever I open it and also it apparently has 'c' in the regiser as the last pressed key so it deletes the first 2 lines when I press j
And when I run the command it complains that
"A{{{\ is missing a quotation mark and is not a command.
This gave me tip. map execute command vim
You don't need use :execute "normal! ..." if you are using nnoremap. This will work.
nnoremap <C-[> A{{{<Esc><CR>

Vim Send a Word under Cursor to External command

I am trying to send a Word under to an external command. I did some digging, yet cannot have a working solution. I learned about using <cword> but I can't seem to pass the current word to the external command.
Here is my command:
nmap <silent> <F8> :!start test.exe s/\(<c-r>=expand("<cword>")<cr>\)/
All it does is gets the current word under the cursor and pass it to test.exe. Can some one please help.
Update
Here is what I am trying to achieve. I have function name in the code.
a = 0
b = 1
c = add_function(a,b)
I would like to use word under cursor to pass add_function to the custom executable I have. So that it will launch test.exe, and pass the following:
open, 'add_function'
I tried the above vim command, but it does work.
I'm not sure what the problem with your mapping is as such, if I remove the <silent> I get in my cmdline:
:!start test.exe s/\(WORD\)/
Which may be what you want? It doesn't send because you didn't add a <Cr> to the end (and doesn't show in the commandline because of the <silent>)...
nnoremap <silent> <F8> :!start test.exe s/\(<c-r>=expand("<cword>")<cr>\)/<CR>
If this isn't what you want, and if adding s/\(..\)/ isn't intentional, then lets start over by trying to get the simplest working mapping by just just echo-ing the current word in Vim:
nnoremap <F8> :echo shellescape(expand('<cword>'))<Cr>
Then expand that to run the external echo command:
nnoremap <F8> :execute ':!echo ' . shellescape(expand('<cword>'))<Cr>
Then expand that to run your start text.exe:
nnoremap <F8> :execute ':!start test.exe' . shellescape(expand('<cword>'))<Cr>
And finally add the <silent>:
nnoremap <silent> <F8> :execute ':!start test.exe' . shellescape(expand('<cword>'))<Cr>
Add one more "cr" to run external program
nmap <silent><F8> :!start test.exe s/\(<c-r>=expand("<cword>")<cr><cr>\)/
End it would be better to rewrite it into
nnoremap <silent><F8> :!start test.exe %s/\(<c-r>=expand("<cword>")<cr><cr>\)/

I want to update the file (if necessary) and run the system command on current file

I realize that I can :nmap <leader>rc :!cat %<CR> to provide an easy set of triggers, but I would like to do this instead.
nmap <leader>rc :up :!cat %<CR> but it complains about needing only one filename. How do I get vim to recognize both commands, in series?
You are missing a <CR> after :up. <CR> tells vim you want a carriage return here.
nmap <leader>rc :up<CR> :!cat %<CR>
The reason up is complaining about multiple file names is that it sees :!cat and %<CR> as two arguments to up.
So the new macro executes
:up
:!cat %
instead of
:up :!cat %
(Side Note: you should probably use nnoremap instead of nmap)
ZyX recommends using the following mapping instead.
nnoremap ,rc :up\|execute "!cat" shellescape(#%, 1)<CR>
This uses | to separate commands and escapes the %. Escaping the % leads to a more robust mapping just incase the filename contains special characters.
Help for :h execute and :h shellescape

Resources