Skip to main content
added 223 characters in body
Source Link
Martin Tournoij
  • 63.3k
  • 26
  • 199
  • 274

You can map FloatTermNew <cmd> in an autocmd for specific filetypes:

autocmdaugroup floatterm-mappings
    au!
    au FileType javascript nnoremap <buffer> <F5> :w<esc>:FloatermNew node %<CR>
autocmd    au FileType c          nnoremap <buffer> <F5> :w<esc>:FloatermNew gcc % && ./a.out && rm a.out<CR>
autocmd    au FileType cpp        nnoremap <buffer> <F5> :w<esc>:FloatermNew g++ % && ./a.out && rm a.out<CR>
autocmd    au FileType python     nnoremap <buffer> <F5> :w<esc>:FloatermNew python3 %<CR>
augroup end

The <buffer> in nnoremap makes sure the mapping only works for that specific buffer (rather than all buffers).

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>

You can map FloatTermNew <cmd> in an autocmd for specific filetypes:

augroup floatterm-mappings
    au!
    au FileType javascript nnoremap <buffer> <F5> :w<esc>:FloatermNew node %<CR>
    au FileType c          nnoremap <buffer> <F5> :w<esc>:FloatermNew gcc % && ./a.out && rm a.out<CR>
    au FileType cpp        nnoremap <buffer> <F5> :w<esc>:FloatermNew g++ % && ./a.out && rm a.out<CR>
    au FileType python     nnoremap <buffer> <F5> :w<esc>:FloatermNew python3 %<CR>
augroup end

The <buffer> in nnoremap makes sure the mapping only works for that specific buffer (rather than all buffers).

edited body
Source Link
Visrut
  • 125
  • 6
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 c++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 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 c++ 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 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>
deleted 675 characters in body
Source Link
Visrut
  • 125
  • 6

for javascript

nnoremap <F5>autocmd :FloatermNewFileType nodejavascript %<CR>

for c

nnoremap <F5> :FloatermNew gcc % && ./a.out<CR>

for c++

nnoremap<buffer> <F5> :w<esc>:FloatermNew g++ % &&node ./a.out<CR>%<CR>

for python

nnoremap <F5>autocmd :FloatermFileType python3c %<CR>

Now i want that these all working with one shortcut F5.

i done like this but get an error

nnoremap <expr><buffer> <F5> My_mapping()

function! My_mapping
    if &filetype ==# 'c'
        nnoremap <F5>:w<esc>:FloatermNew gcc % && ./a.out <CR>
    elseif &filetype&& ==#rm 'c++'a.out<CR>
     autocmd FileType c++ nnoremap <buffer> <F5> :w<esc>: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&& processingrm /home/visrut/.config/nvim/inita.vim:
line  208:out<CR>
E123:autocmd UndefinedFileType function:python My_mapping
nnnoremap <buffer> <F5>        * My_mapping()
line  220:
E193: w<esc>:endfunction not insideFloatermNew apython3 function%<CR>

can anyone fix error?

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?

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 c++ nnoremap <buffer> <F5> :w<esc>:FloatermNew g++ % && ./a.out && rm a.out<CR>
autocmd FileType python nnoremap <buffer> <F5> :w<esc>:FloatermNew python3 %<CR>
added 6 characters in body
Source Link
Visrut
  • 125
  • 6
Loading
added 491 characters in body
Source Link
Visrut
  • 125
  • 6
Loading
added 225 characters in body
Source Link
Visrut
  • 125
  • 6
Loading
Source Link
Visrut
  • 125
  • 6
Loading