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

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.

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.

how to disable some features in some specific window in vim?

here is my problem:
i'm using taglist and nerdtree.and set quickfix window displayed no matter if it has context.
and in my vimrc, i set them toggled in a fixed order, so i can get a layout i want.
but when i use C-o, C-i, C-], it will jump to a file, and if i want the features of taglist and nerdtree i should quit it and open it again.
but it will break the layout i want. so i have to quit all and open the file again.
so, is it possible to desable some features in some specific window?
thanks for any help.:)
You can disable certain commands for buffers via :map <buffer>; for sidebar windows like from NERDTree, that's good enough, as they always display the same (scratch) buffer. For example, to disable <C-O> in NERDTree:
:autocmd FileType nerdtree nnoremap <buffer> <C-o> <Nop>
I don't fully understand your question, but another approach (as it is hard to fully control where Vim places new buffer contents) would be to extend your "build your window layout" function from your .vimrc to first clean up any existing NERDTree / TagBar windows, so that you can call it later on (e.g. via a mapping) to "fix up" your layout again.

How to focus a particular window?

I use NERDTree plugin, and I want to create a mapping that focuses NERDTree window and enters search mode (to easily select files, of course). The difficult part here is focusing NERDTree window. I want the mapping to work from any window - even from the NERDTree window itself. So how can I focus that window using vimscript?
I found out that NERDTree's buffer has name "NERD_tree_1" (if only one NERDTree buffer exists, but that's enough for me). Can I somehow use it to focus a window containing that buffer?
use the :NERDTreeFocus command. You can bind it to a key, for example:
noremap <F2> :NERDTreeFocus <BAR> call feedkeys('/') <CR>

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>

How to set the default to unfolded when you open a file?

In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
set foldlevel=99
should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.
You can put this in your .vimrc:
au BufRead * normal zR
It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.
set nofoldenable
Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc
In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:
autocmd BufWinEnter * silent! :%foldopen!
That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)
Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.
But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.
You could map it to keys to enable it.
For example,
nmap ,f :set foldmethod=syntax<CR>
Then while in normal mode hit the ",f" key combination
You can open unfolded file when you put set nofoldenable into your .vimrc file.
autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.

Resources