I am using VIM and I would like to pass the current line number to an external program.
something like this:
map <F3> :!mycmd <linenumber><CR>
I tried to substitute <linenumber> for line('.'), line("."), . and others, but nothing seems to work.
Thanks.
You must use :execute to use a variable or expression in your mapping:
nnoremap <F3> :execute ":!echo " . line('.')<CR>
Related
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 got the following hotkey mapping:
nnoremap <leader>f Unite file -path=~/Workspace
This works great, however, I want to make it so that path equals the current folder I'm in (which will be seperate from working directory).
Anyone know how I can make this happen? :S
You can use the expand() function to use %:p:h in places where a file name is not expected (these expansions are for file-name arguments, not others like what it appears to happen with your command)
:echo expand('%:p:h')
But you can't map that directly. It is a command that needs to be built "on-the-fly", so you can use :execute to build and execute an evaluated expression:
nnoremap <leader>f :exec "Unite file -path=" . expand('%:p:h')
How about using:
:UniteWithBufferDir file
or
:UniteWithCurrentDir file
(depending on what you want)
Unite allows dynamic argument by using backtick, as documented in the Unite help doc:
You don't have to use |:execute| for dynamic arguments.
You can use evaluation cmdline by ``.
Note: In the evaluation, The special characters(spaces, "\" and ":")
are escaped automatically.
>
:Unite -buffer-name=search%`bufnr('%')` line:forward:wrap<CR>
So in your case, the mapping will be:
:nnoremap <leader>f :Unite file -path=`expand('%:p:h')`
The naive approach
nnoremap <leader>s :!echo "foo" <bar> vim -c="nnoremap x :q<cr>" -<cr>
displays errors. Is there a way to achieve this?
Figured it out. There are two problems with my original code. The first being an equals sign after -c. For the other, the solution is to use <lt>cr> instead of <cr>. So the entire command would look like this:
nnoremap <leader>s :!echo "foo" <bar> vim -c "nnoremap x :q<lt>cr>" -<cr>
:help <lt>
This question already has an answer here:
Editable vimgrep string in command line
(1 answer)
Closed 9 years ago.
I started with a mapping from this vim tip to use lvimgrep to search across files for the current word under cursor:
map <F4> :execute "lvimgrep /" . expand("<cword>") . "/j **" <Bar> lw<CR>
I realized that I'd like to edit the file glob path or search pattern before the command's actually executed.
for ex: when cursor is on a javascript variable in html file, pressing F4 just searches for the variables in the html files. It'd be better if it formed command and let me edit it before its executed.
Dropping the trailing <CR> prints out the code as is - command window shows:
:execute "lvimgrep /" . expand("<cword>") . "/j **" <Bar> lw
but that's hard to edit. I'd actually like the mapping to show:
:lvimgrep /theword/j *.js //---- cursor waiting here
I've tried all that I could but not sure how to go about it - and vimscript's not exactly easy.
PS: possible dupe of Editable vimgrep string in command line
PPS: final mapping (I've used <leader>*):
nnoremap <expr> <leader>* ":lvimgrep /" . expand("<cword>") . "/j **/*." . expand("%:e") . "\|lopen"
You can do that with a :help map-expr instead of :execute. This evaluates the expression and then uses the result as the right-hand side of the mapping:
:nnoremap <expr> <F4> ":lvimgrep /" . expand("<cword>") . "/j **"
Alternatively, you could insert the current word with <C-r><C-w>:
:nnoremap <F4> :lvimgrep /<C-r><C-w>/j **
Additional tip: You should use :noremap; it makes the mapping immune to remapping and recursion.
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