how to keybind shortcuts with vim autocmd FileType? - vim

I have these lines in my vimrc file:
autocmd FileType js nnoremap <F5> :! node % <CR>
autocmd FileType cpp nnoremap <F5> :! g++ % -o %.out && ./%.out <CR>
autocmd FileType py nnoremap <F5> :! python % <CR>
Problem: compiling and running cpp files with F5 works normally, but js and py files can't run, I think the shortcut is not being executed at all.

The name of vim's filetype for python is "python", not "py". See also this answer. Similarily, it's "javascript", not "js". However, "cpp" is fine.
So the first and third lines should be:
autocmd FileType javascript nnoremap :! node %
autocmd FileType python nnoremap :! python %

Related

Vim: How to use local directory .vimrc to override key mapping defined by file type <buffer> map?

I have a key mapping in
~/.vim/after/ftplugin/c.vim
nnoremap <buffer> <C-D> : ! cc % && ./a.out <CR>
I want create a local configuration project/.vimrc to override the old mapping
nnoremap <C-D> : ! cc % -lncurses && ./a.out <CR>
But it not work.
I try
in previous example, the local map is global, it is override by the <buffer> mapping.
in local .vimrc , map <buffer> <C-D> ... ,
but it only override the key mapping of first opened file.
How to do this ?
Explicit autocommands should be executed after the content of after/ftplugin/c.vim:
augroup c_overrides
autocmd!
autocmd! BufNewFile,BufRead *.c nnoremap ...
augroup END
or:
augroup c_overrides
autocmd!
autocmd! FileType c nnoremap ...
augroup END
But what you should really do is put your project-specific commands in Makefile and use :make % in every project.
See :help :make.

i did mapping with if else condition in init.vim for float term plugin but got an error

for plugin in neovim called float-term actually you don't need to know the plugin at all , it's just error i got from mapping with conditional statements , i want to do conditional mapping like below , if i am do something wrong in function then write right function please.
if you think that my whole method is wrong , that also can be happen then give me right path
for javascript
nnoremap <F5> :FloatermNew node %<CR>
for c
nnoremap <F5> :FloatermNew gcc % && ./a.out<CR>
for c++
nnoremap <F5> :FloatermNew g++ % && ./a.out<CR>
for python
nnoremap <F5> :Floaterm python3 %<CR>
Now i want that these all working with one shortcut F5.
i done like this but get an error
nnoremap <expr> <F5> My_mapping()
function! My_mapping
if &filetype ==# 'c'
nnoremap <F5>:FloatermNew gcc % && ./a.out <CR>
elseif &filetype ==# 'c++'
nnoremap <F5>:FloatermNew g++ % && ./a.out <CR>
elseif &filetype ==# 'js'
nnoremap <F5>:FloatermNew node % <CR>
elseif &filetype ==# 'py'
nnoremap <F5>:FloatermNew python3 % <CR>
else
nnoremap <F5><CR>
endif
endfunction
error
Error detected while processing /home/visrut/.config/nvim/init.vim:
line 208:
E123: Undefined function: My_mapping
n <F5> * My_mapping()
line 220:
E193: :endfunction not inside a function
can anyone fix error? or if i am totally wrong with this method then can anyone define new way to do this
autocmd FileType javascript nnoremap <buffer> <F5> :w<esc>:FloatermNew node %<CR>
autocmd FileType c nnoremap <buffer> <F5> :w<esc>:FloatermNew gcc % && ./a.out && rm a.out<CR>
autocmd FileType cpp nnoremap <buffer> <F5> :w<esc>:FloatermNew g++ % && ./a.out && rm a.out<CR>
autocmd FileType python nnoremap <buffer> <F5> :w<esc>:FloatermNew python3 %<CR>
autocmd FileType typescript nnoremap <buffer> <F5> :w<esc>:FloatermNew ts-node %<CR>

Run code directly from Gvim with a keybinding

I am looking for a way to run the most used programming languages (python, c, c++, etc)
With a keybinding. A plugin would be preferred.
Thank you
Scripting languages (e.g. python):
To run the current python script with a shortcut (for example F1):
For Windows, add the following line to your _vimrc:
autocmd FileType python nmap <silent> <buffer> <F1> :silent up<CR>:silent <C-r>=!cmd /k python <C-r>=expand("%:p")<CR><CR>
For this to run correctly, python directory should be added to your %Path%.
If python scripts (*.py) are associated with python.exe, no need to call python then the script as an argument, as the script can be called directly.
For Linux, add the following line to your .vimrc:
autocmd FileType python nmap <silent> <buffer> <F1> :silent up<CR>:silent <C-r>=!python <C-r>=expand("%:p")<CR> &<CR>
Compiled languages (e.g. C):
Map a shortcut to compile the file:
autocmd FileType c nmap <buffer> <F1> :up<CR>:!gcc <C-r>=expand("%:p")<CR> -o <C-r>=expand("%:p:r")<CR><CR>
And map another shortcut to run the resulting executable:
autocmd FileType c nmap <silent> <buffer> <S-F1> :silent <C-r>=!<C-r>=expand("%:p:r")<CR> &<CR>

Mappings for compiling and viewing LaTeX documents in Vim

I used to use LaTeX-Box plugin to compile and view LaTeX documents with these mappings.
But I don't use it anymore because \begin and \end highlighting (and to a lower degree parentheses matching), which can't be turned off, make Vim a lot slower in big .tex files.
I know how to create an alternative mappings for compiling .tex documents:
autocmd FileType tex nnoremap <buffer> <F2> :!latexmk -xelatex %<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:!latexmk -xelatex %<cr>a
But this will create output files in directory I am currently in, not in the directory where .tex file is (for example, if I am in ~/Desktop and I open in Vim a file which is in ~/Documents, if I press F2, .pdf and all other files are created in ~/Desktop instead of in ~/Documents).
I don't know how to create mappings for viewing the compiled .pdf document.
So, I am asking you to help me create mappings that will compile .tex file, and view created .pdf file.
You can easily modify your mappings to generate files in the same directory as your source file with :help filename-modifiers:
augroup LaTeX
autocmd!
autocmd FileType tex nnoremap <buffer> <F2> :!cd %:p:h && latexmk -xelatex %<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:!cd %:p:h && latexmk -xelatex %<cr>a
augroup END
To open the generated file with an imaginary pdfviewer program (command shortened for legibility):
!cd %:p:h && latexmk -xelatex % && pdfviewer %:p:r.pdf
augroup LaTeX
autocmd!
autocmd FileType tex nnoremap <buffer> <F2> :!cd '%:p:h' && latexmk -xelatex '%:t'<CR>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:!cd '%:p:h' && latexmk -xelatex '%:t'<CR>a
autocmd FileType tex nnoremap <buffer> <F3> :silent !mupdf '%:p:r.pdf'&<CR>
autocmd FileType tex inoremap <buffer> <F3> <Esc>:silent !mupdf '%:p:r.pdf'&<CR>a
augroup END
This is what worked for me. Thanks to romainl for helping me to figure it out.

Vim - Only 1 'bind' in .vimrc file

I've got problem with .vimrc file. I've installed NerdTree and I added this line into vimrc file:
map <C-n> :NERDTreeToggle<CR>
It works perfectly, but I want to use python in vim. I added this line:
nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr>
And It doesn't work. When I've only had "python bind" it was working, but when I added NerdTree link "python bind" stopped working.
The <buffer> in nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr> means that the mapping is local to the current buffer.
Since you have that mapping in your vimrc, it is defined not for the vimrc but for the first buffer you edit and only the first buffer.
As soon as you open another buffer, no matter what kind of buffer (NERDTree included), your mapping won't work anymore for any other buffer than the first one.
Here is a revised version of your mapping that will only work in Python buffers, all of them:
augroup PythonThings
autocmd!
autocmd FileType python nnoremap <buffer> <F5> :exec '!python' shellescape(#%, 1)<cr>
augroup END
See:
:help <buffer>
:help autocommand
Never add anything to your config that you don't fully understand.

Resources