1 Star 0 Fork 0

quick-source/vim-floaterm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
5年前
Loading...
README
MIT

Use (neo)vim terminal in the floating/popup window.

CI GitHub license Maintenance Join the chat at https://gitter.im/skywind3000/asynctasks.vim

Features

  • NeoVim floatwin and Vim8 popupwin support
  • Open and toggle terminal window quickly
  • Multiple terminal instances
  • Customizable floating terminal style
  • Switch/Preview floating terminal buffers using vim-clap, denite.nvim or coc.nvim
  • Integrate with other external command-line tools(ranger, lf, fzf, etc.)
  • Use as a custom task runner for asynctasks.vim

Requirements

  • Vim or NeoVim with terminal feature

Run :checkhealth to check the environment.

Installation

  • vim-plug
Plug 'voldikss/vim-floaterm'
  • dein.nvim
call dein#add('voldikss/vim-floaterm')

Basic Usage

Use :FloatermNew command to open a terminal window, use :FloatermToggle to hide/reopen that. The filetype of the terminal buffer is set to floaterm.

If you've opened multiple floaterm instances, they will be attached to a double-circular-linkedlist. Then you can use :FloatermNext or :FloatermPrev to switch between them.

Commands

:FloatermNew[!] [options] [cmd] Open a floaterm window.

  • If ! exists, run program in $SHELL. Try :FloatermNew python and :FloatermNew! python to learn about the difference.
  • If cmd doesn't exist, open $SHELL.
  • The options is formed as --key[=value], it is used to specify some attributes of the floaterm instance, including height, width, wintype, position, name and autoclose.
    • height see g:floaterm_height
    • width see g:floaterm_width
    • wintype see g:floaterm_wintype
    • position see g:floaterm_position
    • name name of the floaterm
    • autoclose close the window after finishing job, see g:floaterm_autoclose
  • Use <TAB> to get completion.

For example, command

:FloatermNew --height=0.6 --width=0.4 --wintype=floating --name=floaterm1 --position=topleft --autoclose=2 ranger --cmd="cd ~"

will open a new floating floaterm instance named floaterm1 running ranger --cmd="cd ~" in the topleft corner of the main window.

:FloatermPrev Switch to the previous floaterm instance

:FloatermNext Switch to the next floaterm instance

:FloatermFirst Switch to the first floaterm instance

:FloatermLast Switch to the last floaterm instance

:FloatermUpdate [options] Update floaterm window attributes(height, width, etc.).

  • The options is the same as in :FloatermNew.
  • Use <TAB> to get completion.

:FloatermToggle[!] [floaterm_name] Open or hide the floaterm window.

  • If floaterm_name exists, toggle the floaterm instance whose name attribute is floaterm_name. Otherwise create a new floaterm named floaterm_name.
  • Use <TAB> to get completion.
  • If ! exists, toggle all floaterms

:FloatermShow[!] [floaterm_name] Show the current floaterm window.

  • If floaterm_name exists, show the floaterm named floaterm_name.
  • If ! exists, show all floaterms

:FloatermHide[!] [floaterm_name] Hide the current floaterms window.

  • If floaterm_name exists, show the floaterm named floaterm_name.
  • If ! exists, hide all floaterms

:FloatermKill[!] [floaterm_name] Kill the current floaterm instance

  • If floaterm_name exists, kill the floaterm instance named floaterm_name.
  • If ! exists, kill all floaterms

:FloatermSend [--name=floaterm_name] [cmd] Send command to a job in floaterm.

  • If --name=floaterm_name exists, send lines to the floaterm instance whose name is floaterm_name. Otherwise use the current floaterm.
  • If cmd exists, it will be sent to floaterm and selected lines will be ignored.
  • This command can also be used with a range, i.e., '<,'>:FloatermSend [--name=floaterm_name] to send selected lines to a floaterm.
    • If cmd exists, the selected lines will be ignored.
    • If use this command with a !, i.e., '<,'>:FloatermSend! [--name=floaterm_name] the common white spaces in the beginning of lines will be trimmed while the relative indent between lines will still be kept.
  • If there are no floaterms opened, :FloatermSend will open a floaterm and execute lines for you.
  • Use <TAB> to get completion.
  • Examples
    :FloatermSend                        " Send current line to the current floaterm (execute the line in the terminal)
    :FloatermSend --name=ft1             " Send current line to the floaterm named ft1
    :FloatermSend ls -la                 " Send `ls -la` to the current floaterm
    :FloatermSend --name=ft1 ls -la      " Send `ls -la` to the floaterm named ft1
    :23FloatermSend ...                  " Send the line 23 to floaterm
    :1,23FloatermSend ...                " Send lines between line 1 and line 23 to floaterm
    :'<,'>FloatermSend ...               " Send lines selected to floaterm(visual block selection are supported)
    :%FloatermSend ...                   " Send the whole buffer to floaterm
    

Options

g:floaterm_shell

Type string. Default: &shell

g:floaterm_title

Type string. Show floaterm info(e.g., 'floaterm: 1/3' implies there are 3 floaterms in total and the current is the first one) at the top left corner of floaterm window.

Default: 'floaterm: %s/%s'

Example: 'floaterm(%s|%s)'

g:floaterm_wintype

Type string. 'floating'(neovim) or 'popup'(vim) by default. Set it to 'normal' if your vim/nvim doesn't support floatwin or popup.

g:floaterm_width

Type int (number of columns) or float (between 0 and 1). If float, the width is relative to &columns. Default: 0.6

g:floaterm_height

Type int (number of lines) or float (between 0 and 1). If float, the height is relative to &lines. Default: 0.6

g:floaterm_winblend

Type int. The transparency of the floating terminal. Only works in neovim. Default: 0

g:floaterm_position

Type string. The position of the floating window. Available values:

  • If wintype is normal: 'top', 'right', 'bottom', 'left'. Default: 'bottom'
  • If wintype is floating or popup: 'top', 'right', 'bottom', 'left', 'center', 'topleft', 'topright', 'bottomleft', 'bottomright', 'auto'(at the cursor place). Default: 'center'

g:floaterm_borderchars

Type array of string. Characters of the floating window border.

Default: ['─', '│', '─', '│', '┌', '┐', '┘', '└']

g:floaterm_rootmarkers

Type array of string. If not empty, floaterm will be opened in the project root directory.

Example: ['.project', '.git', '.hg', '.svn', '.root', '.gitignore'], Default: []

g:floaterm_open_command

Type string. Command used for opening a file from within :terminal.

Available: 'edit', 'split', 'vsplit', 'tabe', 'drop'. Default: 'edit'

g:floaterm_gitcommit

Type string. Opening strategy for running git commit in floaterm window. Only works in neovim.

Available: 'floaterm'(open gitcommit file in the floaterm window), 'split', 'vsplit', 'tabe'.

Recommended: 'split' or 'vsplit'.

Default: '' which means this is disabled by default(use your own $GIT_EDITOR).

g:floaterm_autoclose

Type number. Decide whether to close floaterm window once job gets finished.

  • 0: Always do NOT close floaterm window
  • 1: Close window if the job exits normally, otherwise stay it with messages like [Process exited 101]
  • 2: Always close floaterm window

Default: 0.

g:floaterm_autoinsert

Type bool. Enter terminal mode after opening a floaterm. Default: v:true

g:floaterm_autohide

Type boolean. Decide whether to hide previous floaterms before switching to or opening a new one.

Default: v:true.

Keymaps

This plugin doesn't supply any default mappings. To use a recommended mappings, put the following code in your vimrc.

" Configuration example
let g:floaterm_keymap_new    = '<F7>'
let g:floaterm_keymap_prev   = '<F8>'
let g:floaterm_keymap_next   = '<F9>'
let g:floaterm_keymap_toggle = '<F12>'

You can also use other keys as shown below:

let g:floaterm_keymap_new = '<Leader>fn'

All options for the mappings are listed below:

  • g:floaterm_keymap_new
  • g:floaterm_keymap_prev
  • g:floaterm_keymap_next
  • g:floaterm_keymap_first
  • g:floaterm_keymap_last
  • g:floaterm_keymap_hide
  • g:floaterm_keymap_show
  • g:floaterm_keymap_kill
  • g:floaterm_keymap_toggle

Note that this key mapping is installed from the plugin directory, so if you use on-demand loading provided by some plugin-managers, the keymap above won't take effect(:help load-plugins). Then you have to define the key bindings yourself by putting the code used to define the key bindings in your vimrc. For example,

For example,

nnoremap   <silent>   <F7>    :FloatermNew<CR>
tnoremap   <silent>   <F7>    <C-\><C-n>:FloatermNew<CR>
nnoremap   <silent>   <F8>    :FloatermPrev<CR>
tnoremap   <silent>   <F8>    <C-\><C-n>:FloatermPrev<CR>
nnoremap   <silent>   <F9>    :FloatermNext<CR>
tnoremap   <silent>   <F9>    <C-\><C-n>:FloatermNext<CR>
nnoremap   <silent>   <F12>   :FloatermToggle<CR>
tnoremap   <silent>   <F12>   <C-\><C-n>:FloatermToggle<CR>

Highlights

This plugin provides two highlight-groups to specify the background/foreground color of floaterm (also the border color if g:floaterm_wintype is 'floating' or 'popup') window.

By default, they are both linked to Normal(see detail). To customize, use hi command together with the colors you prefer.

" Configuration example

" Set floaterm window's background to black
hi Floaterm guibg=black
" Set floating window border line color to cyan, and background to orange
hi FloatermBorder guibg=orange guifg=cyan

Besides, there is a neovim only hi-group which can be used to configure no-current window(:help NormalNC). It's also linked to Normal by default.

" Configuration example

" Set floaterm window background to skyblue once the cursor moves out from it
hi FloatermNC guibg=skyblue

Integrate with command line tools

Before you start, if you are using neovim, you had better make sure that nvr executable is installed(why do we need that?), please install it via pip using pip3 install neovim-remote.

The following cases should work both in Vim and NeoVim unless otherwise specifically noted.

floaterm

Normally if you run vim/nvim somefile.txt within a builtin terminal, you will get another nvim/vim instance running in the subprocess. This plugin allows you to open files from within :terminal without starting a nested nvim process. To archive that, just replace vim/nvim with floaterm, i.e., floaterm somefile.txt

git

See g:floaterm_gitcommit option.

Execute git commit in the terminal window without starting a nested nvim.

❗️Note: neovim only

fzf

This plugin has implemented a wrapper for fzf command. So it can be used as a tiny fzf plugin.

Try :FloatermNew fzf or even wrap this to a new command like this:

command! FZF FloatermNew fzf

fff

There is also an fff wrapper

Try :FloatermNew fff or define a new command:

command! FFF FloatermNew fff

nnn

There is also an nnn wrapper

Try :FloatermNew nnn or define a new command:

command! NNN FloatermNew nnn

lf

There is also an lf wrapper

Try :FloatermNew lf or define a new command:

command! LF FloatermNew lf

ranger

This plugin can also be a handy ranger plugin since it also has a ranger wrapper

Try :FloatermNew ranger or define a new command:

command! Ranger FloatermNew ranger

vifm

There is also a vifm wrapper

Try :FloatermNew vifm or define a new command:

command! Vifm FloatermNew vifm

lazygit

Furthermore, you can also use other command-line programs, such as lazygit, htop, ncdu, etc.

Use lazygit for instance:

python

Use :FloatermNew python to open a python shell. After that you can use :FloatermSend to send lines to the Python interactive shell.

This can also work for other languages which have interactive shells, such as lua, node, etc.

Integrate with other plugins

vim-clap

Use vim-clap to switch/preview floating terminal buffers.

Try :Clap floaterm

denite.nvim

Use denite to switch/preview/open floating terminal buffers.

Try :Denite floaterm

coc.nvim

Use CocList to switch/preview/open floating terminal buffers.

Install coc-floaterm and try :CocList floaterm

other fuzzy finders

Please refer to the Wiki

asynctasks.vim

This plugin can be a runner for asynctasks.vim. To use it, copy the following code to your vimrc set g:asynctasks_term_pos to "floaterm" or add a "pos=floaterm" filed in your asynctasks configuration files.

function! s:runner_proc(opts)
  let curr_bufnr = floaterm#curr()
  if has_key(a:opts, 'silent') && a:opts.silent == 1
    FloatermHide!
  endif
  let cmd = 'cd ' . shellescape(getcwd())
  call floaterm#terminal#send(curr_bufnr, [cmd])
  call floaterm#terminal#send(curr_bufnr, [a:opts.cmd])
  stopinsert
  if &filetype == 'floaterm' && g:floaterm_autoinsert
    call floaterm#util#startinsert()
  endif
endfunction

let g:asyncrun_runner = get(g:, 'asyncrun_runner', {})
let g:asyncrun_runner.floaterm = function('s:runner_proc')

Then your task will be ran in the floaterm instance. See asynctasks.vim Wiki for more information.

How to define more wrappers

Once you've find a nice command line program which can be used as a wrapper of this plugin, you can either send me a PR or define a personal wrapper for yourself.

The wrapper script must be located in autoload/floaterm/wrapper/ directory, e.g., autoload/floaterm/wrapper/fzf.vim.

There are two ways for a command to be spawned:

  • To be executed after spawning $SHELL. Here is the old implementation of fzf wrapper

    function! floaterm#wrapper#fzf#() abort
      return ['floaterm $(fzf)', {}, v:true]
    endfunction
    

    The code above returns a list. floaterm $(fzf) is the command to be executed. v:true means the command will be executed after the &shell startup. In this way, the second element of the list must be {}.

  • To be executed through termopen()/term_start() function, in that case, a callback option can be provided. See fzf wrapper

    function! floaterm#wrapper#fzf#(cmd) abort
      let s:fzf_tmpfile = tempname()
      let cmd = a:cmd . ' > ' . s:fzf_tmpfile
      return [cmd, {'on_exit': funcref('s:fzf_callback')}, v:false]
    endfunction
    
    function! s:fzf_callback(...) abort
      if filereadable(s:fzf_tmpfile)
        let filenames = readfile(s:fzf_tmpfile)
        if !empty(filenames)
          if has('nvim')
            call floaterm#window#hide_floaterm(bufnr('%'))
          endif
          for filename in filenames
            execute g:floaterm_open_command . ' ' . fnameescape(filename)
          endfor
        endif
      endif
    endfunction
    

    In the example above, after executing :FloatermNew fzf, function floaterm#wrapper#fzf# will return ['fzf > /tmp/atmpfilename', {'on_exit': funcref('s:fzf_callback')}, v:false].

    Here v:false means cmd(fzf > /tmp/atmpfilename) will be passed through termopen()(neovim) or term_start()(vim). As a result, an fzf interactive will be opened in a floaterm window. After choosing a file using <CR>, fzf exits and the filepath will be written in /tmp/atmpfilename. Then the function s:fzf_callback() will be invoked to open the file.

How to write sources for fuzzy finder plugins

Function floaterm#buflist#gather() returns a list contains all the floaterm buffers.

Function floaterm#terminal#open_existing({bufnr}) opens the floaterm whose buffer number is {bufnr}.

For reference, see floaterm source for vim-clap.

Wiki

Here you go

F.A.Q

  • This plugin leaves an empty buffer/window on startify window

    Put this code in your vimrc

    autocmd User Startified setlocal buflisted
    
  • How to customize the style of the floaterm window?

    Use autocmd. For example

    function s:floatermSettings()
        setlocal number
        " more settings
    endfunction
    
    autocmd FileType floaterm call s:floatermSettings()
    
  • I want to open normal(non-floating) floaterm in a vsplit window.

    Use :FloatermUpdate

    :FloatermUpdate --wintype=normal --position=right
    
  • Can not enter insert mode after creating a new floaterm...

    See option g:floaterm_autoinsert, also #52 might be helpful.

  • Why the plugin is named "vim-floaterm" instead of "vim-popterm" or others?

    Because this was firstly developed based on nvim's floating window. But now it supports both floaterm and popup, you can get similar experience in both.

  • How to execute command in the TERMINAL mode?

    Go back to the NORMAL mode using <C-\><C-N> and type : to do that.

  • How to go back to the former windows?

    First go back to the NORMAL mode using <C-\><C-N>, and then use <C-W>W to switch among windows. Also you can use :FloatermToggle or :FloatermHide.

  • Incorrect indent when using :FloatermSend with Ipython

    Turn off Ipython's autoindent, see #150.

Break Changes

  • Rename g:floaterm_wintitle to g:floaterm_title and change its value type to string
  • Change g:floaterm_autoclose default value to 0
  • Change in asynctasks.runner_proc: call floaterm#hide() => call floaterm#hide(1, '')
  • Current floaterm will be hidden by default before opening a new one or switching to prev/next one using :FloatermPrev or :FloatermNext. See g:floaterm_autohide to get more info.
  • Current floaterm won't be hidden before switching to prev/next one using :FloatermPrev or :FloatermNext
  • Command :FloatermSend [floaterm_name] => :FloatermSend [--name=floaterm_name] [cmd]
  • Use GNU style for cmdline arguments. e.g., wintype=normal => --wintype=normal
  • Floaterm window won't be closed automatically after finishing job by default, see g:floaterm_autoclose
  • Rename: g:floaterm_type => g:floaterm_wintype
  • Rename: FloatermNF => Floaterm
  • Rename: FloatermBorderNF => FloatermBorder

Feedback

If you like this plugin, please star it or sponsor the project.

It's a great way of getting feedback.

The same goes for reporting issues or feature requests.

Credits

License

MIT

MIT License Copyright (c) 2019 voldikss Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
README
MIT
取消

发行版

暂无发行版

近期动态

4年多前推送了新的提交到 master 分支,380206f...f92c4dc
4年多前推送了新的提交到 master 分支,4411484...380206f
接近5年前推送了新的提交到 master 分支,97b1b85...4411484
接近5年前推送了新的提交到 master 分支,e2d8e88...97b1b85
接近5年前推送了新的提交到 master 分支,f23cd3a...e2d8e88
加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quick-source/vim-floaterm.git
git@gitee.com:quick-source/vim-floaterm.git
quick-source
vim-floaterm
vim-floaterm
master

搜索帮助