Execute current line in Bash from Vim

后端 未结 7 1867
误落风尘
误落风尘 2020-12-12 11:58

This question is similar to Vim: execute current file?, but instead of executing the current file I want to execute only the current line.

Is this possible?<

相关标签:
7条回答
  • 2020-12-12 12:35

    I do this sort of thing all the time with:

    :exec '!'.getline('.')
    

    You can even create a mapping in your .vimrc:

    nmap <F6> :exec '!'.getline('.')
    
    0 讨论(0)
  • 2020-12-12 12:35

    Move the cursor to that line, and in normal mode press:

    !!bash<cr>
    
    0 讨论(0)
  • 2020-12-12 12:36

    Consider the following command run on terminal,

    seq 0 10 | xargs printf "%02x "
    

    Expected output is,

    00 01 02 03 04 05 06 07 08 09 0a
    

    Consider you have this above command written in a file. To execute this command get this respective output back in that file, you can add this mapping in yours .vimrc,

    nmap <leader>E :exec 'r!'.getline('.')<CR>
    

    Note that, to execute above mentioned line, you need to write it with adding escape char for '%' as follows,

    seq 0 10 | xargs printf "\%02x "
    

    Go this line in your file and press <leader>E. In my case, <leader> is mapped to , hence I will press ,E

    0 讨论(0)
  • 2020-12-12 12:40

    This could be a comment if I can comment.

    Concerning redirect/pipe lines of current buffer in Vim to external command, inspired by Daan Bakker's great answer, I wrote I answer here (Save and run at the same time in Vim), on an question concerning running a Python script (current buffer).

    Beside running the whole buffer, how to run a range of line via an external command is demonstrated.

    To save time, I just copy it below.

    #####################
    

    In Vim, you could simply redirect any range of your current buffer to an external command (be it 'bash', 'python', or you own Python script).

    # Redirect the whole buffer to 'python'
    :%w !python
    

    Suppose your current buffer contains the two lines as below,

    import numpy as np
    print np.arange(12).reshape(3,4)
    

    then :%w !python will run it, be it saved or not. And print something like below on your terminal,

    [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    

    Of course, you could make something persistent, for example, some keymaps.

    nnoremap <F8> :.w !python<CR>
    vnoremap <F8> :w !python<CR>
    

    The first one runs the current line. The second one runs the visual selection, via the Python interpreter.

    #!! Be careful, in Vim ':w!python' and ':.w !python' are very different, the
    first writes (creates or overwrites) a file named 'python' with thew contents of
    the current buffer. The second redirects the selected cmdline range (here dot .,
    which mean current line) to an external command (here 'python').
    

    For cmdline range, see

    :h cmdline-ranges
    

    Not the below one, which concerning normal command, not cmdline one.

    :h command-range
    

    This was inspired by Execute current line in Bash from Vim.

    0 讨论(0)
  • 2020-12-12 12:40

    If I have a command on a line of text within vi/Vim like this"

    ls -la

    Position the cursor anywhere on the line and do ":.!!" and press return.

    That is: colon dot bang bang return

    That will execute the text in that line in the current shell from within vi/Vim and have the output inserted within the text file.

    I'm thinking that is what you were asking? It's like magic.

    0 讨论(0)
  • 2020-12-12 12:45

    Add this mapping to your .vimrc file,

    nmap <leader>E yyp:.!csh<CR>
    

    Explanation:

    1. yy Yank current line
    2. p Paste yanked line below (and the cursor goes to this next row)
    3. :.!csh<CR> Execute (using csh) this newly pasted line in place. The output of this line replaces this current line, thus before executing the line was yanked and pasted below.
    0 讨论(0)
提交回复
热议问题