2

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.

3
  • Open another? Doe's not help. Jan 15, 2012 at 1:43
  • 2
    How is running vims inside screen sessions editing multiple files? You are still editing one file at a time! You should really try to learn buffers and tabs before asking about plugins. Jan 15, 2012 at 1:44
  • I had tried my hands at buffers, but could not hold on to it like other concepts I have in vim. I would not go for tabs, they dont seem intuitive. Jan 16, 2012 at 2:21

7 Answers 7

7

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.

3
  • Thanks for this tip.I did not know about :e tab complete feature. Whenver I did :e, I had to look for file in a file system and it was troublesome to navigate the file system within vim. Sometimes i used :Explore or :Sexplore Jan 16, 2012 at 2:16
  • Look at the plugins NERDTree and FuFBuffer. Jan 17, 2012 at 23:51
  • 1
    @ThePosey, I have started using buffers more effectively now. Thanks a lot for the topics. Makes me a better vim user. Feb 10, 2012 at 1:24
5

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.

2
  • +1 for not using tabs. I just could not get it. I had followed it from the time it was release. It just a PITA compared to tabs in firefox or tabs in terminals. Thanks the servername and remote command, I think they would be immensely useful. I shall read the buffers concepts and so will be the ability to switch without saving buffers. I think you got my question right! :) Jan 16, 2012 at 2:20
  • yeah tabs are pretty worthless once you get the basics figured out
    – ThePosey
    Jan 16, 2012 at 23:44
3

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.

1

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
1

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

1

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>
0
0

I keep a single vim window open for days at a time. split windows work really nicely on large screens. I also like tabs; I cluster my splits for a single project in a tab, but keep other tabs around for my day plan, my vim wiki, scratch notes for when I'm interrupted. I find tabs easier to use than multiple windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.