I usually exit vim to open other files, I know it is not productive and I want to avoid it. I want to use netrw instead to open the files I want. Is there any way to prevent me from exiting vim, using :x, :wq and others ?

I've found out this script but it requires a graphical vim

I cannot recompile vim. Below I've included version information:

VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Feb  4 2012 09:50:29)
Included patches: 1-108
Compiled by 'http://www.opensuse.org/'
Huge version without GUI.  Features included (+) or not (-):
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent 
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments 
+cryptv +cscope +cursorshape +dialog_con +diff +digraphs -dnd -ebcdic 
+emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path +find_in_path 
+float +folding -footer +fork() +gettext -hangul_input +iconv +insert_expand 
+jumplist +keymap +langmap +libcall +linebreak +lispindent +listcmds +localmap 
+menu +mksession +modify_fname +mouse -mouseshape +mouse_dec -mouse_gpm 
-mouse_jsbterm +mouse_netterm -mouse_sysmouse +mouse_xterm +multi_byte 
+multi_lang -mzscheme -netbeans_intg -osfiletype +path_extra -perl +postscript 
+printer +profile -python +quickfix +reltime +rightleft -ruby +scrollbind 
+signs +smartindent +sniff +statusline -sun_workshop +syntax +tag_binary 
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
 -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo 
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
 -xterm_clipboard -xterm_save 
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/etc"
 f-b for $VIMRUNTIME: "/usr/share/vim/current"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -g -Wall -pipe -fno-strict-aliasing -fstack-protector-all        
Linking: gcc   -L/usr/local/lib -o vim       -lm -lncurses -lacl
up vote 7 down vote accepted

Update: I created a confirm_quit.vim plugin; it has some additional options, like only asking for confirmation when quiting the last buffer (rather than closing any buffer).

I will leave the below code as-is because it demonstrates how you can achieve something like this in a simple way, but I recommend you use the code from the link :-)


Update2: After re-reading your post and reading statox's answer, I had another idea; instead of asking for confirmation, why not run :Explore when using :q?

My plugin now supports this with the confirm_quit#command() function. It's not mapped by default (this would be too surprising), but you can map it like so:

let g:confirm_quit_nomap = 1
cnoremap <silent> x<CR>  :call confirm_quit#confirm(1, 'last')<CR>
nnoremap <silent> ZZ     :call confirm_quit#confirm(1, 'last')<CR>
cnoremap <silent> q<CR>  :call confirm_quit#command(0, 'last', 'Explore')<CR>
cnoremap <silent> wq<CR> :call confirm_quit#command(1, 'last', 'Explore')<CR>

See confirm_quit#command() for some more information.


Original: The script you link to also works without a GUI. The only reason it doesn't is because it's wrapped in "if has("gui_running")...
I'm not sure why the author thinks this only runs with gVim; perhaps he was confused/mistaken, or perhaps something changed in Vim in the 11 years since that plugin was published.

Here is a modified version which works in terminal Vim. It also fixes a bug where :wq didn't work, I added a mapping for ZZ, and made a few small improvements.

fun! ConfirmQuit(writeFile)
    if (a:writeFile)
        if expand('%') == ''
            echohl ErrorMsg | echomsg 'E32: No file name' | echohl None
            return
        endif
        write
    endif

    let l:confirmed = confirm('Do you really want to quit?', "&Yes\n&No", 2)
    if l:confirmed == 1
        quit
    endif
endfun

cnoremap <silent> q<CR>  :call ConfirmQuit(0)<CR>
cnoremap <silent> wq<CR> :call ConfirmQuit(1)<CR>
cnoremap <silent> x<CR>  :call ConfirmQuit(1)<CR>
nnoremap <silent> ZZ     :call ConfirmQuit(1)<CR>

You can also add more commands if you want (such as :quit, :q!, etc.)

From your post it sounds like you have a problem of exiting Vim. You mention a plugin that may help, but lets get to the root of the problem which is you have a created bad habit. As with all bad habits you need to break them. Maybe stick with using :w to write files and using :close/<c-w>c to close windows until you fully break your habit.

You also mention wanting to open files with netrw. This is the easy part:

  • Use :Explore or :E for short to open up netrw
  • Use :edit/:split/:vsplit/... with a directory to open up a netrw buffer for that directory. e.g. :e. to open the current working directory
  • Make a mapping to make this easier. e.g. nnoremap - :e %:h<cr>
  • Nice Vimcast episode on this: The file explorer
  • Protip on netrw usage: :Rex will re-open/resume your last netrw session.

Personally I rarely use any file explorers like netrw or NerdTree as I feel that they are just too slow and convoluted for my workflow. Instead I get by with using :e with tab completion and <c-d> to list completions for simple file exploring purposes. As I often am editing well structured projects I use a combination of projectionist.vim and ctags/GNU global/cscope. For a more generalized file switching I use :find or a fuzzy finder like CtrlP.

For more help see:

:h :close
:h netrw
:h :Explore
:h :e
:h cmdline-completion
:h ctags
:h cscope
:h :find
:h 'path'
  • 4
    I can't recommend this enough. Always look for solutions that vim has by default before looking for plugins. – EvergreenTree Jun 26 '15 at 17:09

Edit: Maybe I didn't understood your question correctly in the first place: I thought you didn't know how to access new files an open them but actually the script you're linking in your question is about exiting vim when you don't want to so my answer might be totally off topic. If it's the case let me know and I'll just delete it :-)

A plugin which is pretty convenient for what you want to do is nerdTree Explorer. You can install it just like any other plugin

On the readme the author advise to use pathogen, I used Vundle which works really good, you simply need to had the following line to your vimrc:

Plugin 'scrooloose/nerdtree'

This plugin allows you to open a split windows into vim which contains a file explorer similar to a GUI explorer. Once it is installed you can open the explorer with the command

:NERDTree

Personnaly I added the following line in my vimrc to open the explorer with leadero:

noremap <Leader>o :NERDTree <CR>

Once in the window you can navigate in the files with j and k. The parents directories are accessibles via the line ...

To open a file simply use Enter to open it in a new buffer or s to open it in a vertical split windows.

Note: Ones might say that NerdTree doesn't follow the workflow of vim and might prefer ctrlp but NerdTree is easier to use for a debutant in my opnion.

Note 2: Using netrw as you mentionned is also a good option since it is a built-in vim feature but I'm not familiar enough with it to help you using it.

  • No need to be sorry for that. I'm not sure making it a gist would be really useful since I think a lot of people wrote better stuff than is answer. :-) – statox Jun 26 '15 at 14:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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