【问题标题】:Changing the practice of quiting and opening vi (vim) editor?改变退出和打开vi(vim)编辑器的做法?
【发布时间】:2012-02-10 14:54:09
【问题描述】:

vi (vim) 和 emacs 之间的主要区别之一是 emacs 被设计并应该在不退出的情况下运行,鉴于 vim 的快速加载时间,退出和开始编辑任务更容易.我读到这已经成为这两个编辑器用户之间的文化差异。

我倾向于认为让编辑器始终运行,这有助于提高工作效率,正如您所知,有些事情正在进行中,您无需重新开始。您发现运行单个 vim 会话并从那里完成所有任务的最佳技巧和插件是什么?

例如,我编辑 Python 程序,然后退出以运行 appengine appcfg 并返回。有时退出当前文件以打开另一个文件。我还没有习惯缓冲区和选项卡的概念,如果我想编辑多个文件,我宁愿在我的屏幕会话中运行 vim。

【问题讨论】:

  • 打开另一个?没有帮助。
  • 如何在屏幕会话中运行 vim 来编辑多个文件?您仍在一次编辑一个文件!在询问插件之前,您真的应该尝试学习缓冲区和选项卡。
  • 我曾尝试过缓冲区,但无法像我在 vim 中的其他概念那样坚持下去。我不会选择标签,它们看起来不直观。

标签: vim


【解决方案1】:

只需使用 :! 命令在 shell 中运行东西。它与:cd 和% 扩展完美结合

bash> vim path/to/ex.c
...
:cd %:h. " move to path/ex/
:!gcc -o %:r % && %:r " compile ex.c into ex and run it

如果要将命令的输出放入当前缓冲区,也可以将其与:read 混合使用:

:read !ls " read in the names of all the files in the current directory

【讨论】:

    【解决方案2】:

    所以你在每个屏幕会话运行一个文件 Vim?这听起来很糟糕的人。你真的不需要任何特殊的插件就可以轻松地在 Vim 中使用多个文件。做吧

    :e /home/project/myfile.py
    

    我在.vimrc 中设置了autochdir,它会自动将当前工作目录更改为当前处于活动状态的任何缓冲区。所以一旦你打开了那个文件,你就可以这样做

    :e myfile2.py
    :e myfile3.py
    

    等等。顺便说一句,在 Vim 中打开任何文件都可以通过制表符完成来完成,因此请确保您正在这样做。一旦你打开了一堆缓冲区可以在我之间切换,我就这样做

    :b myfile1.py
    

    您也可以使用制表符完成,因为您只需键入:b 1 并点击制表符,它就会发现您想要打开myfile1.py,因此如果您能记住一般文件名并且如果还有更多文件名,它会非常快比一个类似的匹配,它会给你一个列表,你可以通过标签。为此,我还建议查看wildmode 和wildmenu 设置,看看您喜欢什么,它们将为您提供增强的选项卡完成菜单。如果您在任何时候开始迷失哪些缓冲区是打开的以及您想要查看的内容,您可以这样做

    :ls
    

    它会显示所有打开的东西。

    还请记住,您可以通过在命令前加上 ! 来运行外部命令

    :!ls
    

    例如。希望其中的一些帮助或至少让您找到正确的方向。

    【讨论】:

    • 感谢您的提示。我不知道 :e 选项卡完整功能。每当我做 :e 时,我必须在文件系统中查找文件,而且在 vim 中导航文件系统很麻烦。有时我使用 :Explore 或 :Sexplore
    • 看看插件NERDTree和FuFBuffer。
    • @ThePosey,我现在已经开始更有效地使用缓冲区了。非常感谢主题。让我成为更好的 vim 用户。
    【解决方案3】:

    这里有一个很好的视频教程,它有助于了解如何以及为什么使用单个 Vim 会话来管理所有编辑的工作流程:

    http://www.derekwyatt.org/vim/vim-tutorial-videos/vim-intermediate-tutorial-videos/#onevim

    【讨论】:

      【解决方案4】:

      您甚至可以使用 :sh 下拉到 shell,然后在 shell 中使用 exit 回到 Vim。要在同一个 Vim 中编辑多个文件,可以使用 :vsplit 文件名或 :split 文件名(用于垂直和水平分割),然后使用 Esc+Ctrl+w+箭头键在不同的分割之间导航。这样你就不需要标签了。如果您使用的是小段代码,则效果特别好。

      【讨论】:

        【解决方案5】:

        其他人说的都加三:

        • 使用set hidden,您可以打开一个新缓冲区来代替当前缓冲区,即使它没有保存。你可以像那样打开几十个缓冲区,无需关闭 Vim!请参阅:help windows 或Vim wiki。

        • 假设 Vim 使用正确的标志 (+clientserver) 编译,您可以让单个 Vim 作为“服务器”运行(在终端窗口 A 中)并在该单个 Vim 中打开所有文件(从终端窗口 B) .分两步完成:

          1. $ vim --servername WHATEVER 启动 Vim
          2. $ vim --remote file.js 打开文件

          您的文件已在终端窗口 A 中的 Vim 中打开,您仍然在终端窗口 B 中看到提示。

        • 不要触摸标签。它们的名称非常错误,根本不像标签。

        【讨论】:

        • +1 表示不使用标签。我只是无法得到它。从它发布的那一刻起,我就一直在关注它。与 Firefox 中的选项卡或终端中的选项卡相比,它只是一个 PITA。感谢 servername 和 remote 命令,我认为它们将非常有用。我将阅读缓冲区的概念,因此将能够在不保存缓冲区的情况下进行切换。我想你的问题是对的! :)
        • 是的,一旦你了解了基础知识,标签就毫无价值了
        【解决方案6】:

        如果我从控制台运行 vim(我在 linux 上这样做,因为我只使用 ssh),那么我经常使用 CTRL-z 来挂起 vim。然后做我的shell的东西和fg返回vim。

        在 vim 中使用 ctags 非常有用 -- help tags 了解更多信息。

        我使用了一个非常强大的 perforce 插件:http://www.vim.org/scripts/script.php?script_id=240。差异支持非常棒,因为您可以循环浏览所有打开的文件或查看文件历史记录和 2 个旧版本之间的差异。试试 :PVDiff、:PFilelog 和 :POpened。

        我为搜索和缓冲窗口操作等定义了很多宏。我在这里列出了一些有趣的宏/函数,它们可以帮助我在 vim 中生活。


        剪贴板:

        let mapleader=","
        " put from clipboard
        nmap ,p "*p
        " yank to clipboard
        nmap ,y "*y
        

        标签:

        " jump to tag in other window
        map t :call TagJumpOtherWindow()<cr>
        function! TagJumpOtherWindow()
          let cw = expand("<cword>")
          winc p
          exec "tjump " . cw
          let @/ = cw
          normal z.
          winc p
        endfunction
        

        从头开始:

        let mapleader=","
        nmap ,x0 :e e:/work/scratch0.txt<CR>
        nmap ,x1 :e e:/work/scratch1.txt<CR>
        nmap ,x2 :e e:/work/scratch2.txt<CR>
        nmap ,x3 :e e:/work/scratch3.txt<CR>
        nmap ,x4 :e e:/work/scratch4.txt<CR>
        nmap ,x5 :e e:/work/scratch5.txt<CR>
        nmap ,x6 :e e:/work/scratch6.txt<CR>
        nmap ,x7 :e e:/work/scratch7.txt<CR>
        nmap ,x8 :e e:/work/scratch8.txt<CR>
        nmap ,x9 :e e:/work/scratch9.txt<CR>
        

        IDE:

        function! GetMsdevFile(line)
          let mx = '^\s*\([a-zA-Z_/\.0-9:\- ]*\)'
          let line = matchstr( a:line, mx )
          let file = substitute( line, mx, '\1', '' )
          let file = substitute( line, '\\', '/', '' )
          return file
        endfunction
        
        function! GetMsdevLineNumber(line)
          let mx = '^\s*\([a-zA-Z_/\.0-9:\- ]*\)(\(\d\+\))'
          let line = matchstr( a:line, mx )
          let linenumber = substitute( line, mx, '\2', '' )
          return linenumber
        endfunction
        
        function! GetMsdevFile2(line)
          let file = expand("%:p:h") . "/" . GetMsdevFile(a:line)
          let file
          return file
        endfunction
        
        function! GetMsdevFile2(line)
          let file = expand("%:p:h") . "/../" . GetMsdevFile(a:line)
          let file
          return file
        endfunction
        
        function! GotoMsdevMake( thiswin, version )
          exec "cd ".$DIRECTORY."\\.."
          let l = getline(".")
          if a:version==0
              let file = GetMsdevFile(l)
              let linenumber = GetMsdevLineNumber(l)
          elseif a:version==1
              let file = GetMsdevFile2(l)
              let linenumber = GetMsdevLineNumber(l)
          else
              let file = GetMsdevFile3(l)
              let linenumber = GetMsdevLineNumber(l)
          endif
          if a:thiswin==1
            winc p
          endif
          exec "e +" . linenumber. " " . file
          exec "cd -"
        endfunction
        
        function! GetGCCFile(line)
          let mx = '^\([a-zA-Z_/\.0-9:\- ]*\):[0-9]\+: .*'
          let line = matchstr( a:line, mx )
          let file = substitute( line, mx, '\1', '' )
          let file = substitute( file, '\\', '/', '' )
          return file
        endfunction
        
        function! GetGCCLineNumber(line)
          let mx = '^\([a-zA-Z_/\.0-9:\- ]*\):\([0-9]\+\):.*'
          let line = matchstr( a:line, mx )
          let linenumber = substitute( line, mx, '\2', '' )
          return linenumber
        endfunction
        
        
        function! GotoGCCMake()
          exec "cd ".$DIRECTORY."\\.."
          let l = getline(".")
          let file = GetGCCFile(l)
          let linenumber = GetGCCLineNumber(l)
          winc p
          exec "e +" . linenumber. " " . file
          exec "cd -"
        endfunction
        
        function! MakeOut( filename )
          exec ":e " . a:filename
          call MakeBuffer()
          normal zz
        
        endfunction
        
        " use the current buffer into a Visual Studio build output buffer to jump to errors
        function! MakeBuffer()
          normal! gg
          exec "/).*error\\|failed"
          nnoremap <buffer> <cr> :call GotoMsdevMake(1, 0)<cr>
          nnoremap <buffer>  :call GotoMsdevMake(1, 1)<cr>
          nnoremap <buffer> o :call GotoMsdevMake(1, 1)<cr>
          " nnoremap <buffer>  :call GotoMsdevMake(0, 0)<cr>
        endfunction
        
        " use the current buffer into a GCC build output buffer to jump to errors
        function! MakeGCCErr()
          normal! gg
          exec "/: error:"
          nnoremap <buffer> <cr> :call GotoGCCMake()<cr>
          nnoremap <buffer>  :call GotoGCCMake()<cr>
          nnoremap <buffer> o :call GotoGCCMake()<cr>
        endfunction
        
        function! MakeGCCOut( filename )
          exec ":e " . a:filename
          call MakeGCCErr()
        endfunction
        
        nmap ,mr :call MakeOut( "e:/perforce/branch/obj/release/BuildLog.htm" )<cr>
        nmap ,md :call MakeOut( "e:/perforce/branch/obj/debug/BuildLog.htm" )<cr>
        nmap ,mm :call MakeBuffer()<CR>
        nmap ,mq :call MakeGCCErr()<cr>
        

        【讨论】:

          【解决方案7】:

          我一次打开一个 vim 窗口好几天。拆分窗口在大屏幕上工作得非常好。 我也喜欢标签;我将单个项目的拆分集中在一个选项卡中,但保留其他选项卡用于我的日常计划、我的 vim wiki、当我被打断时的草稿笔记。我发现标签比多个窗口更易于使用。

          【讨论】:

            猜你喜欢
            • 2015-05-23
            • 2021-11-29
            • 1970-01-01
            • 2017-04-04
            • 2013-06-04
            • 2010-10-02
            • 1970-01-01
            相关资源
            最近更新 更多