NERDTree with AG.vim after "e" cursor goes to NERDTree - vim - vim

I have NERDTree opened on the default left side, and some vim file on the right side. After I do search with Ag like :Ag! "echo" I get results in the quickfix window. I chose one of the results and by pressing "e" it opens it to the right side and close the quickfix window, but cursor goes to the left side where the NERDTree is located. Is there a way to jump to opened file right after I click "e" and not pressing "ctrl+w" and "l"?

I have the same setup (NERDTree and Ag) and hadn’t noticed this behavior. My workflow is to open a file and then open NERDTree (or vice versa), then Ag at some point. And pressing Enter or e just took me to the line in the file, as expected. The key is that I have some file already open.
I can reproduce the problem you’re seeing by firing up Vim with no file, then opening NERDTree, then running Ag. So a workaround is to have some file open first -- not just an empty Vim with NERDTree open.
The workaround is pretty, uh, workable. It’s already natural to open and close NERDTree frequently. A useful mapping for this is:
nmap <Leader>n :NERDTreeToggle<CR>
An alternative mnemonic would be <Leader>e for Explore. And while we’re at it:
nmap <Leader>g :Ag!<CR>
So the sequence to grep (if you have NERDTree open but no buffer open) is simply ,n,g (assuming your leader is ,).

Related

How to bring back NERDTree tab if I accidentally closed it?

I have a NERDTree setup nicely on my Rails project:
Sometimes, when I do fuzzy finding (Ctrl + P), I would accidentally still be on left tab; when I went to the file, it would replace the nerdtree's left tab. See screenshot below:
Super simple question, but how can I bring up the Nerdtree display back up like the first screnshot?
You should just be able to run :NERDTree again.
Additionally, I have this setting in my .vimrc, to make it less likely I'll delete the NERDTree buffer by accident with I'm compulsively typing :bd
autocmd FileType nerdtree cnoreabbrev <buffer> bd :echo "No you don't"<cr>
Deleting the buffer will permanently put NERDTree in hell (Vim plugins are usually brittle). If you do that, you have to restart Vim.
Also, if you use tabs in Vim, you should get NERDTreeTabs (dead but it works) which keeps NERDTree open / closed / in the same state across all tabs you have open, which is a standard design practice in all editors except Vim. I have a mapping set up to toggle NERDTree:
nnoremap <Leader>nt :NERDTreeTabsToggle<cr>
steal from my my vimrc!
nnoremap <F2> :NERDTreeToggle<CR>
Press F2 twice. The first one will close the NERDTree panel and second one will bring it up again.
F2 can be any key of your choice.

Open file in vertical split in Vim / netrw

If I open vim with vim . netrw provides me with a nice list of the files and directories in the current directory.
If I open a file using v the file opens in a very narrow split down the left hand side of the screen and the directory listing remains open in a wide split on the right hand side of the screen.
Ideally I'd like it to have the opposite effect. ie. Show the directory listing in a narrow split on the left hand side of the screen and show the file in a wide split on the right hand side of the screen.
Any help much appreciated.
Netrw v153 and later (May 28, 2014) gives you the :Lexplore command, which, by default, opens a directory listing on the left hand side and opens files to the right (by pressing <cr>).
Whilst Jonathan.Brink's answer works perfectly well, simply adding
let g:netrw_altv=1
to .vimrc also seems to do the trick...
See https://superuser.com/questions/1056929/open-file-in-vertical-split-in-vim-netrw/1062063#1062063 for more info.
I'm sure this could be improved upon you can write a custom mapping that target's the netrw filetype.
Stick this in your .vimrc:
" open file vertically to the right
augroup netrw_mappings
autocmd!
autocmd filetype netrw call Netrw_mappings()
augroup END
function! OpenToRight()
:rightbelow vnew
:wincmd p
:normal P
endfunction
function! Netrw_mappings()
noremap V :call OpenToRight()<cr>
endfunction
The only thing is that you need to use V rather than v. For some reason I was unable to override netrw's v command, but using the capital version seems better anyway since it's not overriding a default.

<C-6> like mapping for browse through *all* open buffers in vim

I am using project and minibufexpl(mbx) with my vim.
The problem is using <C-6>, the buffer only toggles between last two open buffer, and not the all buffers open.
I checked vim's wiki but it says about listing the buffer and then selecting them manually i.e. map for :ls and :b. Not much helpful.
Though, I can move through all the open buffers using mbx's way, (go to mbx window and keep pressing arrow), a like alternative would have been helpful if it can span through all open buffers.
Any help please?
The simplest solution to your problem seems to be to use Vim's built-in :bn and :bN.
See :help buffers.
I've tested this in vim 7.3 on a Linux installation.
To have Vim insert a character sequence instead of doing the action, prefix it with Ctrl+v or Ctrl+q (in Windows). So to get the map sequence for Ctrl+right arrow press Crtl+v, then Ctrl+right arrow to get Vim to insert ^[[1;5C, similarly ^[[1;5D for Ctrl+left arrow.
You can then add the following to your .vimrc file to cycle through all buffers by pressing Ctrl+right arrow or Ctrl+left arrow:
nmap ^[[1;5C :bn^M
nmap ^[[1;5D :bN^M
nmap does the mapping only for normal mode. The ^M means Ctrl+v, then <return>.

how to navigate the opened files in vim?

I opened a files in vim using
gf
command.which opened a file that the current cursor position.
I used
ctrl + ^
is used to toggle between the last two opened files.
But,
How can i go forward and backward opened the files?
:bn & :bp to cycle buffers
To cycle windows I use ctrl+w+w
To cycle opened file :next & :prev
To list opened buffers: :ls
from good old vi :e# reopens previous file ^^ (not using it since ages... well since VIM comes to my systems ^^)
EDIT: a quick test on gvim in windows (set nocp)
map <C-tab> ^[:bn^M
in order to enter escape (^[) and enter (^M) you have to press ctrl+v and then the special char
EDIT 2: a cleaner way to do this is putting this in .vimrc
map <C-tab> <esc>:bn<cr>
but just remember that using mnemonics for the keys depends on some other options (I cannot remember which one).
The best way is IMHO to write a VIM function saving the actual mode, performing switch and then restoring it, then mapping it to in every mode (:[xxx]map commands)
:next to next file
:prev to previous file
Alternatively, you might want to check out any one of the plugins trying to make buffer navigation easier
I myself have reduced it to <leader>bx, x being the number of the buffer.
Selecting the buffer is done via a nice popup list at the bottom.
To get this behavior, you would first install LustyJuggler ( https://github.com/vim-scripts/LustyJuggler ), and then set up the shortcut like so in your vimrc file:
nmap <leader>b :LustyJuggler<CR>
Note that this mapping only works when you are in normal mode.
You can also map the TAB key to quickly cycle through open buffers.
Add the following to your vimrc file:
:nnoremap <Tab> :bnext<CR>
:nnoremap <S-Tab> :bprevious<CR>

vim / NERDtree / folding - can it remember the state of folds?

Is there a way to make NERDtree remember the state of folds when switching from buffer to buffer?
Here the my complete .vimrc:
set ignorecase
set scs
let perl_fold=1
hi Folded cterm=bold ctermfg=yellow ctermbg=lightblue
set modeline
cabbr N NERDTree
Here is what I am observing:
start NERDTree
select a file and use spacebar to open it in a new buffer (all folds are closed)
open some folds in the buffer
C-w w back to NERDTree
select a different file, use spacebar to open it
C-w w back to NERDTree
select the first file, hit spacebar
The folds I had opened originally are now closed.
I am editing perl files,so the perl_fold=1 is in force.
I'd like the state of the folds to be remembered as I bounce around from file to file.
Are you sure about the <Space> mapping? I don't see it listed in NERDTree's help.
Anyway, NERDTree has nothing to do with your buffers content or state, it's only a file explorer.
Without some mechanism to keep state of your buffers your folds are lost when you open a new file. Luckily you can add set hidden to your .vimrc.
With it buffers are kept around until you explicitly delete them with :bd. This means that you still have your folds when going back to your previous file, either by using NERDTree or by using :b <Tab>.
The Vim wiki has nice pages about buffers.

Resources