Reputation: 5715
I want to write a vim
plugin that does certain text transformations to the text while in the editor, but I don't want these transformations visible inside the file.
As an example, consider the word Gnarly
in a text file I want to edit. Upon load I would want my vim script change that to G
, but when I save the buffer I want it to expanded back to Gnarly
.
My scenario is a little bit more complex because it will involve an external script, but I want to see exactly how that would be invoked.
Also I'd want to be able to apply this change only to some files based on their extension.
Upvotes: 1
Views: 353
Reputation: 172758
First of all, define your own filetype, e.g. gnarly
. Read :help new-filetype
for the details, but basically it's this autocmd:
:autocmd BufRead,BufNewFile *.gnarly set filetype=gnarly
Then, the conceal feature introduced in Vim 7.3 is the way to go. Write a syntax script ~/.vim/syntax/gnarly.vim
. For your example it would contain:
:syntax keyword gnarlyConceal Gnarly conceal cchar=G
But you can also use :syntax match
for more complex patterns.
Finally, concealment is off by default. To turn it on, put the following command into ~/.vim/ftplugin/gnarly.vim
(you could put it into the syntax file, too, but this separation is recommended and done by all full plugins that ship with Vim):
:setlocal conceallevel=1
You can customize the 'concealcursor'
behavior, too. If you still need help, have a look at the help pages, or existing plugins that use concealment.
Upvotes: 2
Reputation: 196886
See :h autocmd
. The events you need are BufRead
and BufWrite
.
Maybe you will be interested by :h conceal
.
Upvotes: 3