Fold only comments when file is opened in Vim - vim

When I am viewing long code files with verbose comments in Vim, I would like to be able to load the files with comments folded but everything else unfolded. The current folding configuration I have in my .vimrc is:
set foldmethod=syntax
set nofoldenable
That way, when I want to start doing folds, I can just start executing z commands. But is there a way to only fold the (block) comments?

You could execute a global command to close all the block comment folds:
:g/^\/\*/foldc
This will execute :help foldclose on any line that starts with /* (a common block comment indicator). Notice the / and * need to be escaped in this instance. You don't need to escape the / if you use a different delimiter (e.g. :g#^/\*#foldc). If you want this to happen automatically you could add it in an autocommand. For example:
set fdm=syntax fen
augroup closeCommentFolds
au!
au FileType javascript %foldo | g/^\/\*/foldc
au FileType ruby %foldo | g/^=begin/foldc
augroup end
Note that in these examples the ^ character in regex specifies that the /* and =begin matches are at the beginning of the line. If you want to match if there's whitespace between the beginning of the line and the match then use ^\s* instead of ^. The %foldo will open all folds so that foldenable is set, but it won't fold everything (just comments in this case).

Related

Can two foldexpr coexist in my .vimrc?

1) I have in my .vimrc a fold expr (cf. the third example under the :help fold-expr section) which makes a fold out of paragraphs separated by blank lines :
set foldmethod=expr
set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
Most of my files are files with paragraphs (to begin with my .vimrc)
2) But I have two or three text files which are simply lists with no paragraphs and I would like to be able to fold everything except for matches to my search. I found in Vim Wikia (Tip 282) the following "Simple Folding" (at the bottom of the Tip's page) which I would like to implement :
:set foldexpr=getline(v:lnum)!~#/
:nnoremap <F8> :set foldmethod=expr<CR><Bar>zM
How can I have both of them peacefully coexist in my .vimrc ?
Is setlocal (for the second foldexpr) the solution ? Tried, failed…
Is it possible to have a fold expression (the second one) apply only to two files (file1.txt, file2.txt) ?
Is it possible to merge the 2 foldexpr in one ?
Thanks for your help
Option foldexpr is local to window, so:
:set will establish both global and local values. Global value will be used as a default for subsequent windows where local value is not specified.
:setlocal will determine local value only.
It's not clear to me what merging the two expressions would mean, but of course you can create a fold function containing all the complicated logic you want.
What is definitely easy is to set different values for foldexpr depending on the file (or file type). Use an autocommand for that.
So, the whole thing could be, in your .vimrc:
" Default: for all files
set foldmethod=expr
set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
nnoremap <F8> :setlocal foldmethod=expr<CR><Bar>zM
" Only for specific files
augroup NonDefaultFoldMethod
autocmd!
autocmd BufNewFile,BufRead file1.txt,file2.txt setlocal foldexpr=getline(v:lnum)!~#/
augroup end
The augroup/autocmd! idiom is there just to avoid duplicating autocommands if you source .vimrc repeatedly. It's best practice when establishing autocommands.

Run a command each time a file opens, or general syntax

I have a syntax rule that highlights trailing whitespace:
highlight Badspace ctermfg=red ctermbg=red
match Badspace /\s\+$/
This is in my .vimrc. It works fine, but the problem is I use splits a lot, and it seems that the match is only run on the first file you open, as well it should because the .vimrc should only run once.
Anyway, how can I get the above syntax to match any file that is opened? Is there a "general" syntax file? Is there any other way to run match each time a file opens rather than just once? I'd like to know both because I could end up using either one in the future.
The :match command applies the highlighting to a window, so you can use the WinEnter event to define an :autocmd.
:autocmd WinEnter * match Badspace /\s\+$/
Note that there are already a number of plugins for this purpose, most based on this VimTip: http://vim.wikia.com/wiki/Highlight_unwanted_spaces
They handle all that for you, and turn off the highlighting in insert mode; some can also automatically delete the whitespace. In fact, I have written a set of plugins for that, too: ShowTrailingWhitespace plugin.
You could accomplish this by using an autocmd:
highlight Badspace ctermfg=red ctermbg=red
autocmd BufEnter * match Badspace /\s\+$/
However, there's another way to accomplish your specific goal of marking trailing whitespace. Vim has a built-in feature for highlighting "special" whitespace, which includes tabs (to differentiate from spaces), trailing whitespace, and non-breaking spaces (character 160, which looks like a normal space but isn't).
See :help list and :help listchars. Here's what I use:
set list listchars=tab:>·,trail:·,nbsp:·,extends:>
listchars has the benefit of working with any file type, and marking up multiple whitespace types that are of interest. It is also a lot faster (match will be noticeably slow on giant files) and built-in already.
(Note that those are funky non-ASCII dot characters, which should work fine for you if you cut-and-paste into a UTF8-capable Vim. If they don't work for you, you can use any characters you like there, such as periods or underscores).
Here's what it looks like for me:
The correct approach to this problem is actually to use :syntax to define a custom syn-match.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au Syntax * syn match customBadWhitespace /\s\+$/ containedin=ALL | hi link customBadWhitespace Error
augroup END
Edit: It should also be noted that there is built-in support for highlighting trailing whitespace with the 'list' option; see :help 'listchars' and :h hl-SpecialKey (SpecialKey is the highlight group used to highlight trailing whitespace characters when 'list' is on).
This is accomplished using autocmd. The events you're looking for are BufWinEnter and VimEnter. From the Vim manual:
BufWinEnter
After a buffer is displayed in a window. This
can be when the buffer is loaded (after
processing the modelines) or when a hidden
buffer is displayed in a window (and is no
longer hidden).
Does not happen for |:split| without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.
VimEnter
After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au VimEnter,BufWinEnter * match Badspace /\s\+$/
augroup END
Do :help autocmd for more info.
This is completely wrong because :match is window-local, not buffer-local. Ingo Karkat has the right idea. Unfortunately, there is no good way to avoid triggering the autocmd every time you enter the window.
More to the point, though, this is a job for a custom syntax, not match.

No more messing up whitespace in VIM

I have an BufWritePre hook added to my .vimrc that trims trailing whitespace before a buffer is saved. This is very convenient for me when editing my own code or that of others who also have a policy to always remove trailing whitespace. However, this makes me sometimes mess up the whitespace of others, which doesn't look very nice in version control.
I have two ideas how this could be solved in general, both of which I have specific problems with:
Option 1
After opening a file (maybe using a BufReadPost hook), detect whether there is trailing whitespace in the file. If yes, set a buffer-local flag to signal this. If the flag is set, disable the trimming before a save.
The problem I have with this approach is that I don't seem to figure out how I can detect whether there is trailing whitespace in the buffer. I know about =~, but how do I get the buffer contents as a string? Or even better, I can do a search using /\s+$<cr>, but how can I check if the search was successful (if there are hits)?
Option 2 (more intelligent)
It would be even better if the whitespace trimming would only happen on the lines that were actually modified. This way I could have the benefit of not having to care about trailing whitespace in my code but still not messing up the rest of the file. This raises the question: can I somehow get the line numbers of the lines I added or modified?
I'm new to Vimscript, so I'd appreciate any hints or tips :)
UPDATE: I settled with option 1 now:
" configure list facility
highlight SpecialKey term=standout ctermbg=yellow guibg=yellow
set listchars=tab:>-,trail:~
" determine whether the current file has trailing whitespace
function! SetWhitespaceMode()
let b:has_trailing_whitespace=!!search('\v\s+$', 'cwn')
if b:has_trailing_whitespace
" if yes, we want to enable list for this file
set list
else
set nolist
endif
endfunction
" trim trailing whitespace in the current file
function! RTrim()
%s/\v\s+$//e
noh
endfunction
" trim trailing whitespace in the given range
function! RTrimRange() range
exec a:firstline.",".a:lastline."substitute /\\v\\s+$//e"
endfunction
" after opening and saving files, check the whitespace mode
autocmd BufReadPost * call SetWhitespaceMode()
autocmd BufWritePost * call SetWhitespaceMode()
" on save, remove trailing whitespace if there was already trailing whitespace
" in the file before
autocmd BufWritePre * if !b:has_trailing_whitespace | call RTrim() | endif
" strip whitespace manually
nmap <silent> <leader>W :call RTrim()<cr>
vmap <silent> <leader>W :call RTrimRange()<cr>
Option 1 can benefit from search() function, like so:
let b:has_trailing_spaces=!!search('\v\s+$', 'cwn')
search() function returns a number of matched line (they start from 1) or 0 if nothing was found, !! turns it to either 1 or 0, dropping information about on which line search() found trailing whitespace. Without n flag search() moves the cursor which is, I guess, undesired. Without w it may search only in the part of buffer that is after the cursor (really depends on 'wrapscan' option).
Proposed option 2 implementation is a hack that uses InsertLeave and '[, '] markers:
augroup CleanInsertedTrailingSpaces
autocmd!
autocmd InsertLeave * let wv=winsaveview() | keepjumps lockmarks '[,']s/\s\+$//e | call winrestview(wv)
augroup END
It assumes that you only add trailing whitespaces after typing. It will break if you move your cursor across the lines in insert mode. You can also try adding
autocmd CursorHold * if getpos("'.")[1]!=0 | let wv=winsaveview() | keepjumps lockmarks '.s/\s\+$//e | call winrestview(wv) | endif
, this should remove trailing spaces at the line of last change (only one line, '[ and '] can’t be used here because they point to first and last lines to often to be useful). Both autocommands should add information to undo tree.
There is a second option for the option 2: git annotate is able to annotate current state of the file thus you can use grep to filter out lines that have both trailing spaces and uncommitted changes and use a hook to purge unwanted spaces from them before commit. Sad, but hg annotate is not able to do so and thus you will have to write something more complex, possibly in python. I can’t say anything about other VC systems.
I guess it would be better if you used set list listchars+=trail:- to see such spaces and thus be able to remove them manually if they accidentally appear (personally I can’t remember myself constantly adding trailing spaces by accident, though in comments and documentation they are used by me intentionally to indicate that paragraph continues). What do you do so that this problem appears?
I tend not to let vim automatically trim anything. As you say, this can be a nightmare if dealing with other peoples code, and can lead to unnecessary conflicts. The approach I take, to keep my own code tidy is to make whitespace visible. With vim this can be achieved by adding the following to your ~/.vimrc file:
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
The result is to show whitespace like this:
This allows me to keep files clean whilst I write them. Most other (GUI) editors have the ability to show whitespace too.
" Show trailing whitepace and spaces before a tab:
:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$\| \+\ze\t/
:autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red
This way any bad whitespace will glow in red. It's quite hard to miss.

How do I define an exception for line breaks in Vim?

I edit files in Vim were I log terminal command lines along with descriptions of what I did.
All my command lines start with $, so my files look like this:
This is a description of what this
command does, it can be quite long and
should have line breaks.
$ ./the_command.sh
These are actually Viki files, but I guess this question should apply to any file type. I have filetype detection on and the files are correctly identified.
The question now is:
I want (hard) line breaks to be inserted into all the text except for the actual copies of command lines, which could be easily identified by the leading $.
Is it possible in Vim to define an exception for applying the line break rule based on a pattern? Would I do that in the syntax file for viki files?
UPDATE
Using a combination of what Herbert and Jefromi suggested, I now have this in my .vimrc:
au CursorMovedI *.viki call SetTextWidth()
function! SetTextWidth()
if getline(".")=~'^\$'
set textwidth=1000
else
set textwidth=80
endif
endfunction
It does exactly what I want. Thanks guys!
I gather when you say you want "hard line breaks" you mean you want Vim to break a line automatically, as when it reaches a textwidth column. The best way to do this, I think, is to define an 'au' command that sets textwidth to a high number (higher than longest possible line) when it's on a line that begins with a "$".
So something like this would change textwidth whenever you enter or exit insert mode on a line:
au InsertEnter call SetTextWidth()
au InsertLeave call SetTextWidth()
function! SetTextWidth()
if getline(line('.')) =~ '^\$'
" [edit: 'set textwidth = 0' is preferable to line below]
set textwidth =1000
else
set textwidth=78
endif
endfunction
You might want to use the CursorMoved/CursorMovedI groups instead of InsertEnter/Leave since they're more fine-grained. They get triggered whenever you move the cursor, so the function ends up getting called lots more times, but function is simple enough that it's probably not going to introduce any noticeable degradation in performance.
For doing without a function at all you could probably use something like this:
au InsertEnter exec "set tw=" . getline(line('.'))=~'^\$' ? 1000 : 78
au InsertLeave exec "set tw=" . getline(line('.'))=~'^\$' ? 1000 : 78
I believe this would meet your criteria:
set textwidth=78
v/^$/normal gq/^$\|\%$^M
^M is ctrl-v followed by enter
Lets break this down into smaller peices
/^$\|\%$ is a pattern that matches every line not starting with a $. The \%$ will include the lines between the last $ started line and the end of file.
gq/^$\|\%$ formats from the current line up to the pattern /^$\|\%$
:normal {cmd} executes a normal mode commands on current line.
:v/pattern/ is equivalent to :g!/pattern/ which executes a command on every line lot matching /pattern/
This solution does not format as you type as #Herbert Sitz solution does. Instead formats the text in one fell swoop at the end.
You could of course apply this before each write with
au BufWritePre filename-pattern-here set textwidth=78 | v/^$/normal gq/^$\|\%$^M
au BufWritePost filename-pattern-here set textwidth=0
Although I'm not sure what you want (you should be a bit more specific on where (why, how) you want to insert hard line breaks), but you can issue a command like
:v:^\s*$: <Your command to insert line break, e.g. s/something/\r/ >
This above command searches for any line which does not start with any whitespace followed by a $, then executes the command you specify.
HTH

Insert vim variables into text for comment shortcut

I have a simple goal: Map Ctrl-C, a command I don't think I've ever used to kill vim, to automatically insert at the beginning of a line the correct character(s) to comment out that line according to the file's filetype.
I figured I could use an autocommand the recognize the file type and set a vim variable to the correct comment character when the file is open. So I tried something like:
" Control C, which is NEVER used. Now comments out lines!
autocmd BufNewFile,BufRead *.c let CommentChar = "//"
autocmd BufNewFile,BufRead *.py let CommentChar = "#"
map <C-C> mwI:echo &CommentChar<Esc>`wll
That map marks my current location, goes to the beginning of the line in insert mode, echoes the Comment Character(s) at that point, enters command mode, goes back to the set mark, and goes two characters right to make up for the inserted comment characters (assuming C style comment).
The italicized portion is the part I'm having trouble with; it is only there as a place holder to represent what I want to do. Can you help me figure out how to achieve this? Bonus points if you use strlen(CommentChar) to step the correct number of spaces to the right! Extra bonus points for the vim-master that includes how to do block-style comments if you are in visual mode!!
I'm still fairly new at vim scripting; my .vimrc is a measly 98 lines long, so if you could please help me by explaining any answers you provide! Thanks.
You can use <C-r> here:
noremap <C-c> mwI<C-r>=g:CommentChar<CR><Esc>`wll
see :h i_CTRL-R.
Also look at NERDCommenter plugin, with it mapping will look like this:
" By default, NERDCommenter uses /* ... */ comments for c code.
" Make it use // instead
let NERD_c_alt_style=1
noremap <C-c> :call NERDComment(0, "norm")<CR>
And you will not have to define comment characters by yourself.
I pulled this off the vim tips wiki at some point and use it myself. The only downside is it adds a space to the end of the line(s) for some reason, probably something small I overlooked.
" Set comment characters for common languages
autocmd FileType python,sh,bash,zsh,ruby,perl,muttrc let StartComment="#" | let EndComment=""
autocmd FileType html let StartComment="<!--" | let EndComment="-->"
autocmd FileType php,cpp,javascript let StartComment="//" | let EndComment=""
autocmd FileType c,css let StartComment="/*" | let EndComment="*/"
autocmd FileType vim let StartComment="\"" | let EndComment=""
autocmd FileType ini let StartComment=";" | let EndComment=""
" Toggle comments on a visual block
function! CommentLines()
try
execute ":s#^".g:StartComment." #\#g"
execute ":s# ".g:EndComment."$##g"
catch
execute ":s#^#".g:StartComment." #g"
execute ":s#$# ".g:EndComment."#g"
endtry
endfunction
" Comment conveniently
vmap <Leader>c :call CommentLines()<CR>

Resources