Changing the practice of quiting and opening vi (v

2020-06-06 05:00发布

One of the primary distinction betweeen vi (vim) and emacs, is emacs is designed and supposed to be run at times without quitting, where as given the quick load time of vim, it is easier to quit and start for editing tasks. I read that it has become a cultural difference between these two editor users.

I tend to think that keeping the editor running at all times, kind of helps in productivity as you know that, something is in progress and you need not start again. What is best tricks and plugins that you have found to run a single vim session and do all your tasks from there?

For e.g, I edit Python programs, then quit to run the appengine appcfg and go back. Sometimes quit current file to open another file. I have not yet gotten used to concept of buffers and tabs, but rather run vim in my screen sessions, if I want to edit multiple files.

标签: vim
7条回答
仙女界的扛把子
2楼-- · 2020-06-06 05:45

Everything the others said plus three:

  • With set hidden you can open a new buffer in place of the current one, even if it's not saved. You can open dozens of buffers like that, no need to close Vim! See :help windows or the Vim wiki.

  • Supposing Vim is compiled with the correct flag (+clientserver) you can have a single Vim running as a "server" (in a terminal window A) and open all your files in that single Vim (from terminal window B). It's done in two steps:

    1. $ vim --servername WHATEVER to start Vim
    2. $ vim --remote file.js to open a file

    Your file is opened in Vim in terminal window A and you still have your prompt in terminal window B.

  • Don't touch tabs. They are terribly wrongly named and don't work like tabs at all.

查看更多
▲ chillily
3楼-- · 2020-06-06 05:47

Just use the :! command to run stuff in a shell. It mixes great with :cd and % expansion

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

You can also mix it with :read if you want to put the output of a command in the current buffer:

:read !ls " read in the names of all the files in the current directory
查看更多
时光不老,我们不散
4楼-- · 2020-06-06 05:54

Here's good video tutorial that helps with workflow of how and why to use a single Vim session to manage all your edits:

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

查看更多
别忘想泡老子
5楼-- · 2020-06-06 05:55

If I'm running vim from console (which I do on linux because I use ssh exclusively), then I often use CTRL-z to suspend vim. Then do my shell stuff and fg to return to vim.

Using ctags in vim is incredibly useful -- help tags for more info.

I use a perforce plugin that is quite powerful: http://www.vim.org/scripts/script.php?script_id=240. The diff support is amazing because you can cycle through all opened files or look at file history and diff between 2 older versions. Try :PVDiff, :PFilelog and :POpened.

I define a lot of macros for things like search and buffer windows manipulation. I have some interesting macros/functions listed here that help me live in vim.


Clipboard:

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

Tags:

" 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

Scratch:

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>
查看更多
Melony?
6楼-- · 2020-06-06 05:56

So you're running one file Vim per screen session? That sounds pretty bad man. You don't really need any special plugins to use multiple files in Vim easily. Just do

:e /home/project/myfile.py

I have set autochdir in my .vimrc which automatically changes current working directory to whatever buffer is currently active. So once you have that file open you can just do

:e myfile2.py
:e myfile3.py

etc. BTW opening any files in Vim can be completed with tab completion so make sure you are doing that. Once you have a bunch of buffers open to switch between I just do

:b myfile1.py

which you can also use tab completion for you can just type :b 1 and hit tab and it will figure out you want myfile1.py open so it is super quick if you can remember the general file name and if there is more than one similar match it will give you a list that you can tab through. For that I would also advise taking a look at the wildmode and wildmenu settings to see what you prefer they will give you enhanced tab completion menus. If at any time you start getting lost with what buffers are open and what you want to look at you can just do

:ls

and it will show you everything open.

Also remember you can run external commands by preceding a command with !

:!ls

for example. Hope some of this helps or at least gets you looking in the right direction.

查看更多
SAY GOODBYE
7楼-- · 2020-06-06 05:58

You can even drop down to a shell using :sh, and then get back to Vim using exit in the shell. For editing multiple files in the same Vim, you can use :vsplit filename or :split filename (for vertical and horizontal splits), and then use Esc+Ctrl+w+arrow keys to navigate between the different splits. This way you don't need tabs. Works especially well if you're working with small pieces of code.

查看更多
登录 后发表回答