vim autocmd on change line? - vim

I'm trying to run a function whenever a line is altered, but there doesn't seem to be a specific autocommand for this. I could run the function on CursorMoved, but it would slow down editing. I could also map the function for all the major editing movements, but it could get very messy as I'm trying to keep each line independent of the others. If there's no solution, I could possibly do a diff every few seconds to see what's changed and run the function on the changed lines, but again it's a messy solution.
Any ideas?

If you can get Vim 7.4, have a look at the TextChanged (and TextChangedI, for insert mode) events. (Note that this will monitor changes throughout the entire buffer.)

You could do something like this with a BufWritePre event. Have a master file that defines your lines and tokens, then in the slave files, a reference to their master, like:
master-Foo.vim:
let b:Dom_slaves = ['/foo/bar/slave.cpp', '...', ...]
let b:Dom_map = {
\ 10 : "do your laundry",
\ 20 : "prepare your lunch"
\ }
slave.cpp
/* Dom_master = master-Foo.vim */
...
cout << "I will gladly /* Dom-id:10 */ and /* Dom-id:20 */.\n";
Dominate.vim
let MSMap = {}
autocmd BufWritePre * call s:Dominate()
function! s:Dominate()
" if current buffer's Dom_slaves and Dom_map defined
" read & update all slaves with Dom_map mappings
" else see if 'Dom_master = somefile' appears in the buffer
" update mapped values from cache or read master file, cache and update
" endif
endfunction
By the way, this is a horrible thing to do to your vim. :P

Related

Vim highlight a set of words in different colors each in all files

I need vim/Gvim to highlight a set of keywords each with mentioned color in all files (it may be a text file, c source file, or anything else).
For example TODO, FIXME are highlighted in C files. Like that, I want to highlight TODO and FIXME in all files each with different colors specified somewhere. This should happen as I open a vim file and do not require me to give a command for this to happen.
How can I achieve this?
Thanks in advance for the help.
Note this is not my answer. Points go to user Ralf, answer copied over from this stackexchange, adding it here for convenience. This is the best solution I could test (if what you want is add custom highlighted words for ALL syntaxes)
function! UpdateTodoKeywords(...)
let newKeywords = join(a:000, " ")
let synTodo = map(filter(split(execute("syntax list"), '\n') , { i,v -> match(v, '^\w*Todo\>') == 0}), {i,v -> substitute(v, ' .*$', '', '')})
for synGrp in synTodo
execute "syntax keyword " . synGrp . " contained " . newKeywords
endfor
endfunction
augroup now
autocmd!
autocmd Syntax * call UpdateTodoKeywords("NOTE", "NOTES")
augroup END

Different VimResized autocmds for Shrinking and Growing

autocmd VimResized * <foo> will run the command <foo> whenever the vim application's window is resized.
Is there a way to run different commands depending on whether the resize is a shrink or a grow?
And, if so, are there any caveats for console vim?
A window is 2-dimensionnal, so the concept of shrinking or growing is quite imprecise : are you talking about the height, or about the width, or about the area?
Let's assume you're talking about the area.
A simple way to do it is to save the last size of the window, on startup and on each time the win is resized; then you just have to compare the last size and the new one, each time the win is resized:
" Defines a command to save the current dims:
command! SaveVimDims let g:last_lines=&lines | let g:last_columns=&columns
" Saves the dims on startup:
au VimEnter * SaveVimDims
" Calls the func below, each the win is resized:
au VimResized * call VimResized_Func()
function! VimResized_Func()
" Gets the area of the last dims:
let last_area = g:last_lines * g:last_columns
" Saves the new dims:
SaveVimDims
" Gets the area of the new dims:
let cur_area = g:last_lines * g:last_columns
" Compares the areas:
if cur_area < last_area
" do something when shrinking
else
" do something when growing
endif
endf
This was only tested with Gvim; I never use Vim in console. Hope it'll work as well

Make VIM function return text without indent

I guess this question could be taken in two ways...
(Generic) - is there a way to specify settings 'local' to a function (setlocal changes seem to persist after the function call)...
(Specific) - I have a function which gets called from an imap mapping (which takes a user input to pass into the function. The function works perfectly if I run set paste or set noai | set nosi either just before running my shortcut, or added into the function itself. The problem is, whichever way I do it, those setting changes persist after my function call.
Essentially, my workflow is:
In insert mode, type //// at which point I get prompted for input text, which I enter and press enter.
The function is called with my input. I need the function to disable indenting, return my string and then re-enable the previous settings. The string would just be a PHP-block comment like this:
/**
* Blah {INPUT TEXT}
*/
Any suggestions appreciated. My script currently looks like this:
function CommentInjector(txt)
return "\/**" ."\<CR>"
\ . " * foo " . a:txt . " bar " . "\<CR>"
\ . " */"
endfunction
imap <silent> //// <C-R>=CommentInjector(input("Enter some text:"))<CR>
UPDATE
Managed to figure it out at least how to dump a comment in... Would appreciate knowing how to get/restore settings though...
function! CommentInjector(txt)
set paste
exe "normal! i/**\<CR>"
\ . " * fooo " . a:txt . " bar\<CR>"
\ . " */\<Esc>"
set nopaste
endfunction
map <C-C><C-C><C-C> :call CommentInjector(input("Enter some text:"))<CR>
Using this you can just pres Ctrl+C 3 time, enter text when prompted and you get a nice comment written in. It assumes you had "set paste" disabled before running though...
Since you've posted an update and are really just looking at how to save/restore settings, I'll give a general solution.
At the start of your function save the initial value of the setting: let save_paste = &paste
Make any changes to paste that you'd like to make
Restore it at the end: let &paste = save_paste
An example of this can be found in the documentation with :help use-cpo-save where they talk about saving and restoring the value of cpoptions.

Is there a way to diff two registers in vim? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(Vim)diff two subroutines in same file
Sometimes I see a block of code I suspect to be identical to another block in the same file, but it's a bit too long for visual inspection and I may just be missing something. I've tried to visually select the block and yank to the the default register, put that register into / (find), but it didn't match even the original block.
Is there a way to select a section, yank it in a register, select another section then diff the two, without creating a bunch of new files? I imagine the diff results opening in a new buffer in a tab or split.
EDIT: My question is basically a duplicate of This one. I found this answer to be the most helpful & closest to what I was looking for. The only thing I'd change is to make it output in Unified format so it looks like the diff output I'm used to (it has more info as well). I suppose this means using a different diff utility.
Inspired from my lh#path#strip_common() function:
echo matchstr(#a.'##'.#b, '^\zs\(.*\)\ze.\{-}##\1.*$')
will show what is common between registers #a and #b.
You can show more information with:
function ShowDiff(a,b)
" I expect neither string to contain '##'
let start = matchstr(a:a.'##'.a:b, '^\zs\(.*\)\ze.\{-}##\1.*$')
let end= matchstr(a:a.'##'.a:b, '^.\{-}\zs\(.*\)\ze##.\{-}\1$')
let a = a:a[len(start): -len(end)-1]
let b = a:b[len(start): -len(end)-1]
echo "identical beginning: ".strlen(start )." chars -> ".start
echo "identical ending : ".strlen(end)." chars -> ".end
echo "typical to a : ".strlen(a)." chars -> ".a
echo "typical to b : ".strlen(b)." chars -> ".b
endfunction
Used with:
:call ShowDiff(#a, #b)
You could use the following sequence assuming that the two segments are already in registers, 'a and 'b. Could probably be put into a macro or function.
new
only
put a
diffthis
vnew
put b
diffthis
This creates a new buffer, makes it the only visible buffer, puts 'a into it, sets it up to be diff'd, then opens a new buffer in a vertical split, puts 'b into this split empty buffer and also sets it up to diff. Immediately vim (or gvim) will show the differences.
When done, type :ls to get the list of buffers, use :buffer *N* to return back to the original file and use :bdel! *N* to delete the created buffers (named "[No Name]").
Here's a function to open two new windows side by side, each containing the specified register contents (called as DiffRegs(#a, #1), for instance) and diff them. The new buffers will not be written or modifiable:
" A list for bookkeeping..
let g:diffreg_buffers = []
function! DiffRegs(reg1, reg2)
" Preserve the unnamed register
let s:nonamereg = ##
let ## = a:reg1
" new window
:new
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = a:reg2
:vsp +enew
normal P
setlocal nomodifiable
setlocal buftype=nofile
diffthis
call add(g:diffreg_buffers, bufnr('%'))
let ## = s:nonamereg
endfunction " DiffRegs(reg1, reg2)
" Function to wipe all buffers we're diffing with the function above
function! EndDiffs()
for buffer in g:diffreg_buffers
exe ':buffer ' . buffer
diffoff
quit
endfor
let g:diffreg_buffers = []
endfunction " EndDiffs()
You can bind those to key combinations of your choice, but if you don't call EndDiffs() after each call to DiffRegs(), you'll run into issues.
To compare quickly two different parts of a file, you can split the view in two by using:
:sp horizontal split
or
:vsp vertical split
Once you have splitted the screen, you must use :diffthis in each window to hightlight the differences. (Then :diffoff to leave diff mode)
Then to go back to a single window you can quit one of them with :q or use CTRLwo

Vim script: Buffer/CheatSheet Toggle

I want to make a vim cheat sheet plugin. It's real simple:
I want to toggle my cheatsheets. A vertsplit toggle, like Taglist or NERDTree.
I want the cheatsheet to be filetype specific. So I toggle my c++ cheatsheet when I have opened a .cpp file.
I want the cheatsheet to be horizontally split. So it shows two files, my syntax cheat sheet and my snippet trigger cheat sheet.
I already have a collection of these cheatsheets, in vimhelp format, but now I have to manually open them.
I haven't really done any vim scripting, but I imagine this would be really simple to put together. I'm sorta sick of googling unrelated codesnippets, so what I'm asking here is:
Could anyone give me a short sum-up of what I need to learn in regards to vim scripting to piece this together. What I have a hard time finding is how to toggle the buffer window.
If you know any intro tutorials that covers the material I need to get this up and running, please provide a link.
tx,
aktivb
The function below may not do exactly what you want, and I haven't tested it, but it should give you some ideas.
The main idea is that the function reads the filetype of the current buffer (you can test this by typing :echo &ft) and then sets the path of the appropriate cheat sheat. If it exists, this path is then opened (read-only and non-modifiable) in a split window. You can then call this function any way you wish, for example by mapping it to the {F5} key as shown.
I'm not sure about the toggling possibilities (is this really easier than just closing the split window?) but you could look at the bufloaded() function, which returns whether or not a given file is currently being accessed.
function! Load_Cheat_Sheet()
let l:ft = &ft
if l:ft == 'html'
let l:path = 'path/to/html/cheat/sheet'
elseif l:ft == 'c'
let l:path = 'path/to/c/cheat/sheet'
elseif l:ft == 'tex'
let l:path = 'path/to/tex/cheat/sheet'
endif
if l:path != '' && filereadable(l:path)
execute ':split +setlocal\ noma\ ro ' l:path
endif
endfunction
map <F5> :call Load_Cheat_Sheet()<CR>
Hope this helps. Just shout if anything is unclear, or you want to know more.
I had forgotten about this until I got a notice about Eduan's answer. Since I posted this question I've done quite a bit of vim scripting, including getting this to work:
let g:cheatsheet_dir = "~/.vim/bundle/cheatsheet/doc/"
let g:cheatsheet_ext = ".cs.txt"
command! -nargs=? -complete=customlist,CheatSheetComplete CS call ToggleCheatSheet(<f-args>)
nmap <F5> :CS<CR>
" strip extension from complete list
function! CheatSheetComplete(A,L,P)
return map(split(globpath(g:cheatsheet_dir, a:A.'*'.g:cheatsheet_ext)),
\ "v:val[".strlen(expand(g:cheatsheet_dir)).
\ ":-".(strlen(g:cheatsheet_ext) + 1)."]")
endfun
" specify cheatsheet or use filetype of open buffer as default
" instead of saving window status in a boolean variable,
" test if the file is open (by name). If a boolean is used,
" you'll run into trouble if you close the window manually with :wq etc
function! ToggleCheatSheet(...)
if a:0
let s:file = g:cheatsheet_dir.a:1.g:cheatsheet_ext
else
if !exists("s:file") || bufwinnr(s:file) == -1
let s:file = g:cheatsheet_dir.&ft.g:cheatsheet_ext
endif
endif
if bufwinnr(s:file) != -1
call ToggleWindowClose(s:file)
else
call ToggleWindowOpen(s:file)
endif
endfun
" stateless open and close so it can be used with other plugins
function! ToggleWindowOpen(file)
let splitr = &splitright
set splitright
exe ":vsp ".a:file
exe ":vertical resize 84"
if !splitr
set splitright
endif
endfun
function! ToggleWindowClose(file)
let w_orig = bufwinnr('%')
let w = bufwinnr(a:file)
exe w.'wincmd w'
exe ':silent wq!'
if w != w_orig
exe w_orig.'wincmd w'
endif
endfun
Thought I would add to Goulash's answer.
I think in order to implement the toggle you would simply use some if statements and a global variable.
let g:cheatsheet_toggle_on=0
if (g:cheatsheet_toggle_on == 0)
" Turn the cheatsheet on
" Also make sure to know that the toggle is on:
let g:cheatsheet_toggle_on=1
elseif (g:cheatsheet_toggle_on=1
" Do whatever you need to turn it off, here
endif
Hope this figures out that logic. :)

Resources