How to save folds in Vim after changing the foldmethod to diff? - vim

I make heavy uses of folds in my VIMRC file (init.vim since I'm on neovim), and I also make use of the tool fugitive.vim.
When I want to do a diff on it, it changes to the fold method to diff. Pretty useful in the moment. But when I want to go back to my previous folds I find them all gone (set foldmethod=manual) and then have to refold everything. How can I revert back to my manual folds after fugitive does a diff on them?
I've tried going through the help files and to look online but I cannot find out how to do it.

:h fold-manual mentions :mkview and :loadview, which can save the folds and then restore them.

Related

Write this in VimScript : "If Lexplore opened a file(anyfile) then close Lexplore"

I want to:
Automaticaly close Lexpore after opening a file for better experience but I don't know vim script.
It shouldn't be hard, I suppose.
I will appreciate any comment!
I'm now learning vim script and trying to do it.
here is where I've got by now:
:autocmd <Lexplore> * :<Lexplore-exit>
or
if <command-mode> == "Lexplore *.*"
excute \\close last buffer. (because now I am in new buffer)
I just need to know how to say "RUN" / "RUNED" in script
then i use regex for anyfile and i need to say "CLOSE".
The truth is
I'm actually Hopeless! :)))))))))))))
There are other avenues to try before going full-on with VimScript. In this case, a simple mapping would probably be enough.
Basically, the right-hand side of a mapping is just a macro, a sequence of commands that you would type yourself, which makes mappings a very good and approchable way to discover Vim automation.
Assuming a simple layout, with only one window, how do you do what you describe manually?
You do :Lexplore and browse around.
In the Netrw window, you press <CR> to open the file.
Then you go back to the previous window (the Netrw window) with <C-w>p.
You close the window with <C-w>q.
Steps 2-4 above are easy to turn into a mapping (<key> is a placeholder, use what you want):
nmap <key> <CR><C-w>p<C-w>q
But that mapping will be available everywhere, which may not be a good idea. Enters the notion of filetype plugins:
Create these directories if they don't already exist:
~/.vim/after/ftplugin/ (Unix-like systems)
%userprofile%\vimfiles\after\ftplugin\ (Windows)
Create a netrw.vim file in the directory above:
~/.vim/after/ftplugin/netrw.vim (Unix-like systems)
%userprofile%\vimfiles\after\ftplugin\netrw.vim (Windows)
Put the mapping above in that file:
nmap <buffer> <key> <CR><C-w>p<C-w>q
The <buffer> tells Vim to only define that mapping in Netrw.
This is the simplest approach, which may or may not work for you.
A few notes regarding your comments…
Buffer numbers are implementation detail. They don't matter at all in your day-to-day work so you shouldn't think too much about them. Buffer names are far more useful and much easier to think about.
The "sidebar file explorer" UX pattern is a familiar one that newcomers like to transfer from their previous editor but it necessitates a lot of fighting against Vim itself to "work". Netrw's take on the pattern is different, yes, and it might take time to adjust to, but it fits much better within the overall experience. Hint: give up on :Lexplore. :Explore, :Sexplore, :Vexplore, and :Texplore are much more useful.
As you learn more, you will soon discover that file explorers, whatever UX pattern they follow, are not that useful to begin with.
Netrw already has a mapping for opening a file in a new tab page: :help netrw-t.

Managing split and multiple files with Vim

I'm brand new in the Vim game, and I'm looking for the best tips and shorcuts to manage multiple files projects.
I saw people on the internet having a window with all the directory they have in there projects, and I'm really interested to find how they do that.
So feel free to put all your tips here.
Thanks
One tip I can give you on making changes in a bunch of files is (based on vimcasts):
Let' say you have many markdown files and want to substitute the word ISSUE for SOLVED...
vim *.md
At this point you have all markdown files as arguments...
:args
So you have the argdo command, but in order to use it you have to set the hidden option (it allows you to go to the next file without saving the current one)
:set hidden
Now
:argdo %s/ISSUE/SOLVED/ge
The g flag makes the substitution in all occurrences at each line
the e flag makes vim ignore files where the pattern does not appear
Another good thing is avoiding messeges during substitution of each file, we can add silent at the beggining
:silent argdo %s/ISSUE/SOLVED/ge
If you realize you made a mistake
:silent argdo edit!
Because the command edit! with exclamation makes the file get back to its original state
If you are sure you made it all correct
:argdo update
There are tons of good tips about dealing with many files on vim, you can visit the vimcasts original tip here.
More about the arglist here.
Another great tool to combine with vim is FZF, you can see a good video about it here.
When you have a couple of files opened you can also use the buffer list easily with this mapping (on your ~/.vimrc or ~/.cofig/nvim/init.vim)
" list buffers and jump to a chosen one
nnoremap <Leader>b :buffers<CR>:b<Space>
A shortcut you can use to get back to the last edited file is Ctrl-6.
In order to open you vim on the last edited file add this alias to your ~/.bashrc or ~/.zshrc
alias lvim='vim -c "normal '\''0"'
Open a new terminal and run
lvim

Jump through git diff

I use and love Tim Pope's excellent Fugitive plugin for VIM, I've always wanted the ability to load up a diff of all of the files in the :Glog quickfix and cycles through them to the file in HEAD (or maybe any revision!) individually. Using :diffthis doesn't persist the diff when I switch to previous versions and macros seem like a kindof off way to solve it.
Is there a canonical way to do this?
Unfortunately, there is currently no way to do this; There are a lot of ways you could make the process relatively painless, and I could definitely script it for you, except for one thing; when you unload the buffer of a fugitive-revision in diff mode, fugitive bends over backwards (tpope's own words) to stop the diff in the original file for you.
I really like the behaviour you're proposing, so I went ahead and submitted an issue to the project on GitHub. Tpope willing, this will be addressed soon, and I'll be more than happy to post a script that will completely automate this behaviour then.

How to browse SASS/CSS files with VIM effectively?

Smartest minds in this sector of internet!
Please advise me how to effectively browse codebase in SASS with VIM.
Tagbar not showing SASS tree, neither I can fold style declarations hierarchically.
So, the question is - how to use tagbar and/or folding with SASS/SCSS/CSS files?
Maybe you can suggest me other way of effectively browsing SASS?
Thank you!
Folding
With set foldenable and set foldmethod=indent in your ~/.vimrc, you are able to fold away all the definitions with zM and go back to normal with zR. Use zj and zk to jump from fold to fold. See this answer for a very cool tip that I'm going to use right now and :help fold for more info about folding.
Tags
See this q/a for a limited ctags-based solution. And this Gist for an approach using TagBar. That's what you need to try first if you want a hierarchical tree with your classes, ids and tagnames.
Cscope
You won't get a hierarchical tree or an outline with it but cscope is a great code indexer that may be useful for jumping directly to a known selector. Do $ cscope -R *.scss and search for classes, ids and tagnames. Hit <CR> on a match to open your $EDITOR at the correct line.
I personnaly use the "limited ctags-based solution" above with CtrlP's :CtrlPBufTag and :CtrlpTag with great success. But I only do basic CSS.

How can I share my folds in VIM?

I am in a project with 3 people. We need to have the same folds in Vim for each member. How can I share my folds?
[Feedback]
I understood one important thing: Google ignores signs, such as {{{, so please google "VIM three braces" to find help about the marker-method. It becomes much easier to practise, as you can quickly find relevant information.
In order to use the the marker-method (suggested by Adam Bellaire), please note that you have to set the method:
:set foldmethod=marker
Thanks for your answers!
Probably the easiest way is to just use fold markers (e.g. {{{1), making sure to include the vim:fdm=marker setting in the file itself. For example, here's a shell script which contains both the setting to use fold markers and two levels of fold:
#/bin/sh
# vim:fdm=marker
echo This file contains fold markers.
#Top Level Fold {{{1
echo This is a top-level fold.
#Second Level Fold {{{2
echo This is a second-level fold.
Opening this file in vim will show the first four lines and then a fold, which if expanded will reveal the second fold. Just make sure to put a space between your comment syntax and the vim:fdm=marker line or vim won't see it. For example, in C you could use:
// vim:fdm=marker
Folds in files ?
Well, same settings should result in same folds.
Vim can fold in several ways: manually, by indent, by expression, by syntax, and by markers (by default, I believe are curved brackets, 3 of them).
So if you have the same vim version, and they haven't changed their syntax and indent files, let them check out your vimrc for foldmethod and foldmarker options, and copy them to their vimrc files. That should do it.
I haven't shared VIM folds with someone before, but if you're working on the same machine perhaps you can use VIM sessions, which will save your current state (including folds). Run the following command in VIM:
mks! /path/to/session_file
Then your friend can load up the session file:
vim -s /path/to/session_file
Ancient history, I know, but this may have appeared since the version of vim present in '09, and since I don't have an adequate reputation to comment yet, here we go.
The good news is that saving a view for the file should save manual folds as well, even nested folds.
The bad news is that I found it didn't produce consistent results under vim 7.0 (RHEL 5.5). This may have been fixed in a subsequent update that, sad to say, we aren't allowed to install.

Resources