vim: jumping with CTRL-O doesn't get me back to netrw - vim

Suppose I'm editing file a.txt on a buffer, then run :E. and from the netrw open another file, say b.txt.
Now if I hit CTRL+O, I'm back on a.txt and not into the netrw explorer.
:jumps has not registered the Netrw location. Is there any way to make netrw dir locations to be registered as jumps, and hence make CTRL+I, CTRL+O work as I expect?

I tried your scenario, and it worked for me. Nonetheless, perhaps you'll find
:Rex
will do what you want.

I think the canonical solution is to let g:netrw_keepj=""
*g:netrw_keepj* ="keepj" (default) netrw attempts to keep the
|:jumps| table unaffected.
="" netrw will not use |:keepjumps| with
exceptions only for the
saving/restoration of position.

Related

How to back to editing file after :Ex

I'm editing a new file in NeoVim and I don't remember the path of the directory I want to save my file to, so I want to look at the directory tree before saving. I do this by typing :Ex, which takes me to Netrw.
I'm done looking at the directory tree and I want to go back to my file and and finish my work. However I don't know the command to get back from Netrw to the file, and none of the numerous solutions I looked up online worked. The help file didn't help either. The intuitive command would be :q, which works fine for help but doesn't work for this situation. I have also tried q, Q, gq, gQ, :visual and :vi as suggested by people online.
Right, after making this post I finally stumbled upon a working solution in a comment by another.anon.coward on How to go back when I run :Ex command in Vim
You could try :bunload to unload current buffer & go back to previous
I think you can just press Ctrl-6 to go back to the previous buffer. Also check the nvim help: :help CTRL-6.
You can use those default vim mappings for that:
CTRL-O - Go to older cursor position in jump list
CTRL-I - Go to newer cursor position in jump list
So when you'll be at netrw press <C-o> couple of times and it will bring you back to the place where you started.
To read more about jump commands: :help jump-motions

How to change drive in Netrw and how to stay in givm window but quit Netrw?

As a newbie in vim I am full of silly questions. :D
Running gvim on Windows 7 64bit, I am careful to avoid complex plugins but try to start from whatever existing to be familiar with the tool.
So now I cannot find a way to lookup files on D drive, how to switch in Netrw? Do I have to mount it like in Linux?
Also, how to quit Netrw but still stay in gvim?
Last question, is nerdtree really much better than netrw?
A way to lookup files on D drive
The plugin is triggered by :editing a directory, so this is what you'll do:
:edit D:\
You could also :cd D:\ first, and then :edit ., or use the dedicated :Ex, :Sex, etc. command(s) that the plugin provides.
How to quit Netrw but still stay in gvim
In Vim, everything is a buffer displayed in a window. Unlike in IDEs, plugins have to "fake" their user interface as special buffers. So, you leave the netrw buffer like any other: Start :edit file, or :enew for a new one, or :bdelete, or :bnext, or ...
Is nerdtree really much better than netrw
The main differentiator is that NERDTree defaults to a hierarchical file listing in a left-hand sidebar (similar to Windows Explorer). You can have that in netrw, too (let g:netrw_liststyle = 3). There's feature parity in other areas, too (e.g. bookmarks), while some things are unique to NERDTree (fancier tree visualization, customizable action menu). netrw provides transparent access to remote file systems, which NERDTree doesn't. You can even use both in parallel!
I'd recommend to read NERDTree's help, and once you run into something that netrw doesn't do well, remember NERDTree and put it to the test.
The command :Ex D: should show you a listing of drive D:. Just press :, type Ex D:, Enter.
:q or :close should close any window or tab, including netrw.
You may wish to review the netrw documentation.

Vim netrw - Explore when modified without split

I've opened a file and modified it - now I want to do :Explore without splitting.
It splits - because I have an unsaved buffer. By default this is nice, becasuse Vim generally doesn't allow to switch files, until you do something with the modified buffer (save or discard changes).
But I have set hidden option, so I can jump from buffer to buffer, switch files and everything - and save or not when I feel I want to. It seems Netrw doesn't follow this policy. How could I help it?
One way I know of is to map netrw explore to save & explore, but I'm not sure if autowriting is a good way of doing things... I am actually using other autowriting settings now, but I was just rethinking to maybe get rid of them.
So here is the function, that does just that:
function! ExploreWithHidden()
let s:cw = getcwd()
lcd %:p:h
enew
Explore
cd `=s:cw`
endfunction
Seems to work like expected.
You could use :Texplore instead. This is the same as explore except in a new tab (and will have no splits).
:h Texplore
Another thing you could do is use :lcd %:p:h to change the current working directory to the directory of the file. Then use :enew to create another buffer then open explore. (Make sure hidden is on if the buffer is modified)
:command! BExplore :lcd %:p:h | enew | Explore
To run the command use :BExplore.
The old buffer will be sitting in the background and the explore window is pointing at the directory the file was in.
You could just upgrade your netrw -- its up to v153s at http://www.drchip.org/astronaut/vim/index.html#NETRW, and it does avoid the split if the modified buffer is hidden.
tpope's vim-vinegar operates like this. It maps - to Explore the current file's directory. (That's the same key netrw uses to go up one directory.) It uses the current window instead of splitting.
When the current file is modified:
If you have 'hidden' set, it will not split and Explore from the current window.
If you do not have 'hidden' set, it will issue an error and do nothing.

Go to back directory browsing after opening file in vim

When I open a directory browsing in vim, I can open a file by moving the cursor to a file name and pressing Enter.
Now, is there any command to go back to the previous directory browsing?
If not, is there a way to open a file without closing the directory browsing mode?
You can go back to the last buffer using :b#.
If you just opened a file, then it will bring you just back to the directory browsing.
Update:
Since this answer happened to be accept as the correct answer and is thus on the top, I'd like to summarize a bit the answers, including the one by #romainl that imho is the correct one.
:Rex[plore]: Return to Explorer (by #romainl) vimdoc.sourceforge
:Explorer: opens the Explorer, same as :E (if no other command starting with E is defined (see stackoverflow), or as :Ex (see vim.wikia) (by #drug_user841417).
:b#: goes back to the "previously edited buffers". See vim.wikia
:e# or Ctrl-6 (or Ctrl-^): goes back to the "previously edited file" (by #aman-jain). See vim.wikia
Ctrl-O: jump back to the previous (older) location, not necessarily a buffer (by #Peyman). See vim.wikia
You can use Ctrl-O to go back to the previous buffer.
The proper command for returning to the netrw listing is :Rex[plore]. You can run it even after you have jumped to and from dozens of files.
:Ex will take you back (and show you what you edited and didn't save).
I rely on Ctrl ^ to go back and forth b/w two buffers.
I usually just use :E. It's easy to remember and gets you back to the directory listing of the file you had open.
I'd like to use the Jump-motions shortcut keys to do any 'go back' or 'go forward' actions in Vim. It makes vim like a web browser.
You can search for Vim's help for more details.
Vim :h jump-motions
So like as mentioned in other answers ctrl+O works cool, and hope vim's help-file let you learn more commands like ctrl+I .
Ctrl + I : Go Forward
Ctrl + O : Go Back

vi, vim buffers overrun

I'm losing all previous buffers when by mistake I'm trying to switch behind the last buffer [n:].
If for example I open couple of files in editor
:ls
1 # "/etc/moduli" line 1
2 %a "/etc/motd" line 1
:n
E163: There is only one file to edit
:p
E163: There is only one file to edit
now i can navigate between tabs just using :b [number]
Please advice how to fix this behavior. How can I prevent buffers from closing in this case?
I think you're confusing something there. A buffer is something like an open file. When you switch to the next file in the argument list using :n you close the current buffer and open the next one, so the changes must either be saved or discarded at this point.
Additionally the default behaviour of vim is to display an error message if you try to go beyond the last file in your argument list, so losing anything is not very easy in vim.
Maybe describing your actions (pressed keys) could help here, if this does not answer your question.
[edit]
Ok, now I know what the problem is: There is a difference between a buffer and the list of files to edit that you supply when starting vim. If you start vim with
vim a.txt b.txt
there are 2 files to edit. This does not mean, there are multiple buffers. You can navigate using :n and :p (meaning n(ext) file and p(revious) file). If you have the global flag :hidden set, this means that every buffer you close will become a hidden buffer. The file is still being edited, but it is not shown in any window. This value is possibly set upon startup of vim in your system. Try adding :se nohidden to your .vimrc and try the following:
:help buffer-hidden
[/edit]
:bn
will display the next file in your buffer (in your case "/etc/moduli")
:bp
will display the previous file in your buffer (also "/etc/moduli" because it does a permutation)
One thing that you'll notice is that the file you're editing is marked with
%a
whereas
#
means it's the last file you displayed.
Hope it helps you.
:n and :p doesn't switch between buffers :)
try :bufnext and :bufprev
maybe you'll like:
nmap <LEADER>k :bnext<CR>:redraw<CR>
nmap <LEADER>j :bprevious<CR>:redraw<CR>
nmap <LEADER>d :bd<CR>
nnoremap <LEADER>b :buffers<CR>:buffer<space>
Press ,j for the previous buffer, ,k for the next buffer, ,d to close the current buffer and ,b to list your buffers and select one with number keys.

Resources