LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   create command in vim (https://www.linuxquestions.org/questions/linux-newbie-8/create-command-in-vim-865334/)

xeon123 02-27-2011 02:15 PM

create command in vim
 
Hi,

I would like to create a command in the vim that saves a tex document and also compile it. Is it possible?

Thanks,

druuna 02-27-2011 02:25 PM

Hi,

I have these 2 in place (~/.vimrc):
Code:

" -----------------------------------------------------------------------------
"  f5 -> Compile (gcc) written code
map <F5> : call CompileGcc()<CR>
func! CompileGcc()
  exec "w"
  exec "!gcc % -o %<"
endfunc

" -----------------------------------------------------------------------------
"  shift f5 ->Compile (gcc) and run written code
map <S-F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
  exec "w"
  exec "!gcc % -o %<"
  exec "! ./%<"
endfunc

F5 -> Write file + compile using gcc.
Shift-F5 -> Write file + compile file (gcc) + run file.

Tailor the above to your liking.

Hope this helps.

xeon123 02-28-2011 04:23 AM

And if I want a general compile method?

For example, if the file extension of my file is .c, I press F5 and I compile it with gcc, if the file is .java, I compile it with javac, if the file is .tex, I compile it with latex, etc...

Thanks,

xeon123 02-28-2011 04:32 AM

I've found the answer:

Code:

autocmd FileType java      nnoremap <buffer> <silent> <f5> :!javac %:p<cr>
autocmd FileType cpp        nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>
autocmd FileType c        nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>
autocmd FileType tex        nnoremap <buffer> <silent> <f5> :!latex %:p<cr>


druuna 02-28-2011 06:49 AM

You're welcome :)

Nice to see you got the answer to your second question, and posted the solution!


All times are GMT -5. The time now is 08:37 AM.