How could I map a command to save the current file and then run it?
I am trying this:
:nnoremap <leader>r :w<CR>| !python %
This runs the python command instantly but not when I call the leader key. What am I missing here?
Just to follow up on what Conner was saying, you can do something like this:
nnoremap <leader>r :w \| !python %<cr>
Aha, I don't need the pipe character when writing a map, just a second colon:
:nnoremap <leader>r :w<CR> :!python % <CR>
Related
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?
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>
I would like to have the following behavior for a key in vim (for example the F8 key):
Find the most recent ex command starting with :! and repeat it.
For example, if my command history is
(...)
:!python abc.py
/hi
:%s/hi/ho/g
:w
then by pressing F8 I would like vim to run :!python abc.py
:!! repeats the last :!{cmd}. You can map it to F8 like so:
:nnoremap <F8> :!!<CR>
You could try the following mapping:
nnoremap <F8> q:?^!<CR><CR>
Explanation:
q: - open the command-line window
?^! - search for a line starting with !
<CR><CR> - hit enter twice to execute that command
nnoremap <F8> :!<UP><ENTER>
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
I finally decided to try out Vim, as I am getting increasingly frustrated by GUI editors. So far, I'm loving it, but I can't find any help for a issue I'm having...
I am trying to map the command :Pyrun to :!python % in Vim using cmap. The mapping shows up fine if I type :cmap. However, on typing :Pyrun, I get this error message:
Not an editor command: Pyrun.
Here is what I'm trying in .vimrc:
:autocmd FileType python :cmap Pyrun<cr> !python %<cr>
:autocmd FileType python :cmap Intpyrun<cr> !python -i %<cr>
What can I do to solve this problem?
I would try something like this in your .vimrc or your ftplugin/python_ft.vim
command Pyrun execute "!python %"
command Intpyrun execute "!python -i %"
Then :Pyrun and :Intpyrun should work
You could then map a function key to each
map <F5> :Pyrun<CR>
map <F6> :Intpyrun<CR>
I personally prefer another approach. First create a function receiving the command arguments and then create a command to call the function:
fun! DoSomething( arg ) "{{{
echo a:arg
" Do something with your arg here
endfunction "}}}
command! -nargs=* Meh call DoSomething( '<args>' )
So it would be like
fun! Pyrun( arg ) "{{{
execute '!python ' . expand( '%' )
endfunction "}}}
command! -nargs=* Pyrun call Pyrun( '<args>' )
But, there's a better way to do it in Vim. Use makeprg:
makeprg=python\ %
Just type :make to run your current Python file. Use :copen to show error list.
G'day,
Similar to karoberts answer, I prefer the more direct:
:map <F9> :!python %<CR>
If my script is creating some output I also like to capture it in a temp file and then autoread that files content into another buffer, e.g.
:map <F9> :!python % 2>&1 \| tee /tmp/results
I then set autoread by entering :set autoread and opening the results file in another buffer
:split /tmp/results<CR>
Then I can easily see the results of the run in the buffer that auto refreshes when the results file is updated by running the script under development.
HTH
cheers,
With new LUA api:
vim.api.nvim_create_user_command('Hello', 'echo "Hello World!"', {})
vim.api.nvim_create_user_command('HelloLua', function ()
print('Hello LUA!')
end, {})
NeoVIM API reference