Let gVim always run a single instance

后端 未结 12 666
时光说笑
时光说笑 2020-12-12 19:14

Is there a way to let gVim only run a single instance, so that when a new file is opened with it it\'s automatically opened in a new tab in the currently running instance?

相关标签:
12条回答
  • 2020-12-12 19:18

    I often find that I want to go to a specific place in a file. The only way I found to do this was:

    gvim --remote-send '^[:tabnew +$lineno $filename ^M'
    

    where ^[ is esc (typed ctrl-v esc) and ^M is enter (ctrl-v enter).

    Hope this helps. If anyone understands how to use --remote-expr, give me a shout.

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

    It depends on your operating system and shell. Using linux you can always set up an alias like:

    alias gvim='gvim --remote-tab-silent'
    

    in your .bashrc (if you use bash as your login shell).

    On windows see the Vim wiki for solution: http://vim.wikia.com/wiki/Launch_files_in_new_tabs_under_Windows .

    0 讨论(0)
  • 2020-12-12 19:21
    alias tvim="gvim --servername `gvim --serverlist | head -1` --remote-tab"
    

    This make vim open a new file in new tab on same instance of vim. Source: http://eustaquiorangel.com/posts/477

    0 讨论(0)
  • 2020-12-12 19:30

    I'm actually using

      gvim () { 
        if [ "$#" -gt 0 ]
        then
          gvim --remote-tab-silent "$@" &
        else
          gvim "$@" &
        fi
      }
    

    It kills errors and only used the flag when there is a parameter

    0 讨论(0)
  • 2020-12-12 19:31

    I've found the --remote-tab-silent option in an alias to work for the most part except when I've wanted to pass options to gvim (e.g. gvim --serverlist) - in which case gvim treats the option as a literal filename which is no good as firstly that's not what you wanted and secondly you have to clean up the buffers from your now tainted vim session.

    It's not practical to use another alias or resolve vim/gvim differently for some cases such as the following.

    • gvim
    • gvim /path/to/files
    • gvim --serverlist
    • gvim -p /path/to/file1 /path/to/file2
    • gvim -t tag filename

    My solution is the following wrapper script around gvim (~/.bin/gvim) as Tom Veiner suggests but this will only use an existing server if none of the arguments are gvim options - otherwise, a new server is created.

    #!/usr/bin/perl
    
    use v5.10;
    
    sub gvim { exec { '/usr/bin/gvim' } '/usr/bin/gvim', @_; }
    
    if (scalar @ARGV) {
      unshift @ARGV, '--remote-tab-silent' unless /^--?/ ~~ @ARGV;
      gvim @ARGV 
    }
    else {
      chomp(my $serverlist = `gvim --serverlist`);
      if (length $serverlist) {
        gvim '--remote-send', '<Esc>:tabnew<CR>'
      } else { gvim }
    }
    
    0 讨论(0)
  • 2020-12-12 19:33

    NOT For WINDOWS

    I use MacVim (snapshot 73).

    Add this to your .bash_profile.

    It won't generate "NO NAME" and error message.

    vi() {
    
        if [[ `mvim --serverlist` == 'VIM' ]]; then
            command mvim --remote-tab-silent "$@"
        else
            command mvim "$@"
        fi
    }
    
    0 讨论(0)
提交回复
热议问题