search

Home  >  Q&A  >  body text

How to configure a "unified" shortcut key for running "Python, Ruby and other mainstream shell languages" in Vim?

[Description]
There is often this demand: In the Linux command line, I hope to run the currently edited script file under the test without leaving Vim (such as sometimes printing something)

I know a single language such as Python,<leader>r :!python %<cr>

I know that the shell can be called in Vim, but how to map multiple languages ​​to one shortcut key (preliminarily planned to be <Leader>r)?

淡淡烟草味淡淡烟草味2743 days ago708

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 16:39:32

    The basic idea is to use the autocmd根据当前缓冲的文件的类型或者语法的类型添加不同的map command.

    For example, if you only need to compile/execute the current file, you can add the following in your vimrcfile for each different file type:

    autocmd FileType sometype nnoremap <buffer> <F5> :w<CR>:!somecompiler % <CR>

    For python, that’s

    autocmd FileType python nnoremap <buffer> <F5> :w<CR>:!python % <CR>

    Among them<F5>可以改成其他你想映射的键位,如<leader>r.


    If you need cross-platform, you can refer to here.

    If you need more advanced functions, such as multi-file compilation, running part of the code in the file, etc., you need to write your own VIM script. For python, please refer to here.

    reply
    0
  • PHPz

    PHPz2017-05-16 16:39:32

    Create a file in vim's fplugin directory [语言名].vim
    Then write the corresponding language configuration into this file, vim will automatically load these configurations based on the file suffix

    reply
    0
  • 迷茫

    迷茫2017-05-16 16:39:32

    Add execution permissions to the file and add
    #!/bin/bash #!/usr/bin/python #!/usr/bin/ruby to the file header.
    Every time you want to run:./ this file will be fine.

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:39:32

    Recommend you to use
    vim-quickrun

    map <leader>r :QuickRun<CR>

    reply
    0
  • Cancelreply