Highlight copied area in Vim - vim

I would like to mimic a nice effect found in the game Vim Adventures: When a yank command is done, I would like the yanked area to be highlighted (let's say in red) for a second to show me that my selection was correct.
For example, yy would highlight the current line in red one second, then I would know what was selected.

For Neovim and Vim
There is a plugin named vim-highlightedyank for this, which works both for Vim and Neovim. Install it and it just works. You can also set the highlight duration by adding the following config:
" set highlight to 1000 ms
let g:highlightedyank_highlight_duration = 1000
Neovim only
If you are using nvim 0.5+, they have made this little feature builtin in this pull request.
No plugin is needed in this case. Just install nvim 0.5+ and add the following config to your init.vim:
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END
See Neovim's documentation on this feature for more.

For Neovim >= 0.7
Neovim includes a function for highlighting a selection on yank, see Neovim Lua documentation.
With Neovim version >= 0.7, you can also Neovim API to define autocommand to use it with TextYankPost event. Add this configuration in your init.lua file :
vim.api.nvim_create_autocmd('TextYankPost', {
group = vim.api.nvim_create_augroup('highlight_yank'),
desc = 'Hightlight selection on yank',
pattern = '*',
callback = function()
vim.highlight.on_yank { higroup = 'IncSearch', timeout = 500 }
end,
})

One vimmer (not me) saw this question and just wrote a plugin for that. Note that this is not a simple solution and probably not for those who prefer to keep vim minimum, but it works like a charm. Also note that this was not meant to be a serious work, thus you can't necessarily seek for a steady support.
plugins to install
kana/vim-operator-user : Lets user define their own operators.
thinca/vim-operator-sequence : "Operator to do two or more operators."
osyo-manga/vim-operator-highlight : the plugin for this.
Use your preferred way to install these plugins.
Settings
Let's say you want to use Meta+y to "yank and highlight". Write the following settings in your .vimrc.
noremap <expr> <Plug>(yank-highlight) operator#sequence#map("y", "\<Plug>(operator-highlight)")
nmap <A-y> <Plug>(yank-highlight)
vmap <A-y> <Plug>(yank-highlight)
Change <A-y> to whatever keymap (just y might not work) you'd like to use : <Leader>y can be nice, for example.
The highlight can automatically be cleared. Write the following:
let g:operator#highlight#clear_time=2.0
This will clear that highlight in about 2 secs. The interval also depends on :set updatetime?, which defaults to 4000(ms), so if this doesn't seem to clear the highlighting, try setting updatetime to a smaller value.

I use the plugin by markonm called hlyank.vim.
I opened the plugin and changed the time as it only blinked the color on the screen so changed it from 200 to 800. In the autoload folder, I opened hlyank.vim and edited the following lines:
call timer_start(800, {-> matchdelete(id, winid)})
else
call timer_start(800, {-> matchdelete(id)})

Related

What is the correct way in Vim to autocommand ":highlight" after a buffer is loaded?

Due to various reasons, I run Vim at sixteen-colors, synced w/ my terminal's colors. In a recent Vim update, I've had to rework my "~/.vimrc" completely to get it back into running order on Linux.
Initially I was shocked to find that this simple line did not work (even w/ "syntax on" preceding it):
:highlight Comment ctermfg=White
I'm also using a "LineNr" ctermfg. No matter where I placed/stacked the "Comment" ctermfg, it didn't work, or interfered w/ everything else sourcing correctly (ie, placed in the same line w/ "LineNr"). However, I found that calling "Comment" after a buffer had loaded would make the comments appear as intended.
I am new to autocmd in Vim (and want to know how it works, anyways). Is there an "autocmd" call that I can have in my "~/.vimrc" that will run the aforementioned line of code immediately after a buffer has loaded?
I have tried several iterations (BufWritePre, BufWritePost, etc.) and been unsuccessful. This was my previous attempt:
autocmd BufWinPost * :highlight Comment ctermfg=white
Don't resort to :autocmd without reason; search harder for the root cause!
Your description lacks specifics; I guess your chosen colorscheme (or a plugin, but no sane plugin should interfere with the default highlightings) overrides your custom one for Comment. You can check who defined this via
:verbose highlight Comment
If this points to your colorscheme, you simply need to execute your :highlight command after it. For this, you need to understand :help initialization, and maybe check the output of :scriptnames. If you have a :colorscheme foo command in your ~/.vimrc, it should be as simple as putting the :highlight command after it.
You do need an :autocmd if you switch colorschemes on the fly, as most colorschemes override the basic Comment definition. The correct event and pattern for that would be ColorScheme *
If I do a quick :h autocmd-events, I find the the event BufWinPost does not exist. I think, you want BufWinEnter instead. The autocmd you wrote should work, except for the :. HTH

Vim: How to compose search with another command?

I just installed sweet vim plugin that provides context colouring for javascript. I've fiddled with the colours so that they are just right, and am pretty pleased. However, there are times when I don't want the context colouring:
in insert mode, since the colouring doesn't work well while I'm editing
when I'm search, since the colouring seems to override the search hl
I resolved the first problem with:
autocmd InsertEnter *.js :JSContextColorToggle
autocmd InsertLeave *.js :JSContextColorToggle
However, the second problem is trickier. At first I thought I could just map /, my search key, so that it would first toggle the context colouring and then perform the search. I haven't been able to figure out how to write that mapping, though. How would I store "the original meaning of /" for use in my map?
Thanks,
p.s. check out this sweet context colouring (with solarized).
Hi I'm the author of the plugin. I fixed the conflict with hlsearch so it should just work now (try pulling latest version from git, I've not updated vim.org yet..)
As for the insert mode behaviour, there is a difference in behaviour between vim 7.3 and 7.4. 7.4 has the 'TextChanged' and 'TextChangedI' events, which fire when text is changed in normal , and insert mode respectively. This triggers the highlighting to update. However the TextChangedI event only fires on leaving insert mode. So if this is the behaviour you want, you can get it by upgrading to 7.4. In 7.3 I had to hook into the cursormoved event, which checks the 'b:changedtick' variable, which vim updates whenever changes occur... I'm not sure if this can occur during insert mode, but I think it might, which would explain odd behaviour if you're using 7.3.
I'm still trying to figure out what the best behaviour should be in insert mode...its tricky because the code syntax my be invalid during editing, and when the code cannot be parsed the plugin cannot work (and you will see regular syntax highlighting appear.. this may be a good thing as one value of syntax highlighting is visual syntax checking!). Another option would be to assume it is the same level as at the point where editing started, and offset the following text by however many characters are added/deleted during editing. Yet another option would be to have syntax highlighting in the area being edited (current line?) .
/ will enter command-mode.
if you only want to toggle highlighting with /, I think you need map <expr>.
something like
nnoremap <expr> / YourFunction()
in YourFunction(), you first do turn off the syntax hi, then return a /.
However you have to think about when to restore the hi. you can create another command mode mapping, map <cr> to first turn on the js hi, then return <cr>.
or just create an autocmd, when entering Normal mode, turn on the highlighting.
I didn't test above idea, hope it helps.

What line of code I could add to vimrc to make <leader>ig command run when vim starts?

I'm new to Vim, sorry for this newbie question. I'm using a vim plugin vim-indent-guides
The default mapping to toggle the plugin is <leader>ig. What modification should be made to make it toggled on when vim-started
Sounds like something the plugin might support in its configuration.
Vim plugins are generally configured by putting something like
let g:global_variable_for_plugin = 1
in your ~/.vimrc.
Lo and behold, from the vim-indent-guides documentation:
Use this option to control whether the plugin is enabled on Vim startup.
Default: 0. Values: 0 or 1.
>
let g:indent_guides_enable_on_vim_startup = 0
<
What you need is to run the following command at vim startup
:IndentGuidesEnable
One of the ways is to add an autocommand to your .vimrc like
au VimEnter * IndentGuidesEnable
There are probably other ways but this looks pretty simple for me.
I have no idea why that plugin is not enabled by default but the
IndentGuidesEnable
command seems to do just that. Add it somewhere in your ~/.vimrc.
More generally, mappings are just convenient shortcuts for commands or sequences of commands. You want to execute the command, not the mapping.
I know this is question is long ago but just an update. The selected answer doesn't work on Neovim. To fix that, add this in .vimrc:
autocmd VimEnter * :IndentGuidesEnable

How to make vim move cursor a character to theright on insertleave?

Since mac os x's terminal.app does not support many of the vim visual aspects, including the cursor change from block to line when switching to insert mode, I use osascript to accopmlisch something similar.
In my .vimrc file I've written:
autocmd InsertEnter * silent !osascript -e 'tell application "Terminal" to set current settings of first window to settings set 11`j
autocmd InsertLeave * silent !osascript -e 'tell application "Terminal" to set current settings of first window to settings set 12`j
where settings set 11 is a set of terminal setting that has a line cursor and settings set 12 is one that has a block cursor.
This actually works quite well but there is one small problem.. On InsertLeave the cursor always gets moved one character to the left, which isn't such a big deal but it can be anoying.
I tried to compensate by putting autocmd InsertLeave h into my .vimrc, but to no avail (it gives me an error).
How should I tell vim to:
not shift to the left?
if the above isn't possible, to compensate by shifting to the right
Before answering the question, I'd recommend you to have a look in MacVim (if you haven't). If you would like (or need) to stick with terminal, maybe another terminal like iTerm will provide more functionality. Anyway, the cursor change between block - bar is not present in iTerm (at least I think so) and your way to solve it was phenomenal, it's bookmarked here now. Thanks!
An easy way to solve it would be adding another autocommand, like you said. But in yours, the pattern and the correct command to execute are missing.
The h is not a command. To execute a normal mode sequence, use the :normal command. This may work correctly:
au InsertLeave * normal! h

Vim: How to change text from within an indent script

I recently switched from Eclipse to Vim. I'm loving it. There are a few hangups I'm working on, but one of the ones I'm having lots of trouble with is the PHP doc comments. In eclipse I could type:
/** [enter]
and the next line would auto fill with
*
So I'd have:
/**
* [comment goes here]
I'm wondering if there's anything like this for vim. It seems there are some plugins to autogenerate doc comments by running a command, but I'd love to have it do them as I'm typing.
I was playing around with the PHP indent script (http://www.vim.org/scripts/script.php?script_id=1120) and I got it to recognize when it's inside of a doc comment block, but I can't figure out how to get it to actually change the text and add a " * " after hitting enter when inside the block.
I've tried what I've seen other plugins do:
let #z = ' * '
put! z
tried this too:
exe 'normal!' '"zgp'
but no luck. Is this not possible from an indent script, and if not, how do I actually get Vim to recognize a doc comment block and act accordingly while I'm typing?
Any help would be greatly appreciated!
No need to mess around with the indentation files. Vim's formatoptions will do this for you and in a variety of languages (not just PHP).
Ensure you have r included in your formatoptions:
:setlocal fo+=r "to set
:set fo? "to query
You can include this in your .vimrc or in .vim/ftplugin/php.vim (if you just want to activate this for PHP).
For more information on formatoptions and file-type plugins, see:
:help 'formatoptions'
:help fo-table
:help ftplugins
Would adding the below code to your vimrc do something similar to what you want?
autocmd BufNewFile,BufRead *.php setlocal formatoptions+=r formatoptions+=o
autocmd BufNewFile,BufRead *.php setlocal comments=s1:/*,mb:*,ex:*/,://,:#
I currently can't quite figure out how to make it work without overriding the <!-- ---> commenting, which this does. I.e. this will break auto-indenting with <!-- --> comments.
Edit. Added ://,:# to comments as Johnsyweb's distribution does.
Try adding this to your vimrc:
let g:PHP_autoformatcomment=1
I'm on a Mac and it seems to be enabled by default. Functions exactly how you stated.

Resources