5
You can use timer_start() to set "timers" to run a function every n milliseconds. For example:
fun! s:set_bg(timer_id)
let &background = (strftime('%H') < 12 ? 'light' : 'dark')
endfun
call timer_start(1000 * 60, function('s:set_bg'), {'repeat': -1})
call s:set_bg(0) " Run on startup
This will run s:set_bg() every 1 minute (60,000 milliseconds),...
answered Apr 18 '20 at 11:30
Martin Tournoij
48.9k1919 gold badges154154 silver badges240240 bronze badges
5
Try this:
new | put! =getcompletion('*', 'function')->filter({_, v -> v =~# '^[a-z][^#]*$'})
Broken down:
new - Create a new window and start editing an empty file in it
put! - Put the text from a register before the current line
= - The expression register
getcompletion('*', 'function') - Return a list of command-line completion for all functions
-&...
4
You can do it with several substitutions.
" bookmark current position
normal! m`
" remove whitespace on the current line
silent! keepj s/\%#\s\+$//
" same for lines above
silent! keepj 1,- s/\s\+$//
" same for lines below
silent! keepj ''+,$ s/\s\+$//
" restore cursor position
normal! g``
4
:h :command will put you on the right track.
It can be as simple as
command! RunMyCommand call RunMyCommand()
Or much more complex if you want to support arguments, range...
4
In this specific case, you have repeat(4, 'X').
Otherwise, normal and command mode commands don't return anything. They may echo messages (which we can get with execute()), or they can do any kind of action (compile, move cursor, change focus, open/close buffers/windows, change the buffer...). We can't provide a generic answer to your question as there is ...
4
If your Vim includes the patch 8.2.0924, then you can use getreginfo(). Note that – since this patch – setreg() also accepts a dictionary as its second argument. The same kind of dictionary which getreginfo() returns, with all the necessary information to restore the unnamed register: that includes its contents, its type and the name of the register which ...
3
A range with commands is always about lines. It seems you want to work with line/column positions.
Whether a range was given can be checked by using <range>:
command! -range Test call TestFunc(<range>,<line1>,<line2>)
function! TestFunc(r,l1,l2) abort
if a:r == 0
echo "No range"
elseif a:r == 1
echo "Single ...
3
First, <cr> in the mapping validates the command -- as we do interactively in the command/ex mode. In a script/function, we shall not add <cr> to commands -- each line validates a command. TL;DR: It should be source ~/.vimrc in the .vimrc
Then, as the error message says: we cannot redefine a function while it's being executed. IOW, a function ...
3
It's really not that complicated, you don't even need regex for this to work:
inoremap <expr> <bs> <sid>remove_pair()
imap <c-h> <bs>
function s:remove_pair() abort
let pair = getline('.')[ col('.')-2 : col('.')-1 ]
return stridx('""''''()[]<>{}', pair) % 2 == 0 ? "\<del>\<c-h>" : "\<bs>"
...
3
I saw the previous answer, but if you don't want to burden yourself with a plugin, you can achieve that with a function and a mapping:
let g:couples = ['(#)', '[#]', '{#}', '<#>', '<div>#</div>']
function BetterBS()
for l:couple in g:couples
if ! (l:couple =~ '#')
continue
endif
let l:regex = ...
3
It is possible. There are plugins like Lexima, auto-pairs and others that handle deletion, but handling pairs of characters doesn't seem to be easy to solve in insert mode: I've used both and there's always a corner case that doesn't give you the right result. I think Surround is more reliable
3
As a workaround one can instead target the common FocusLost / FocusGained events.
function DynamicColorThemeSwitch()
if strftime("%H") < 18
set background=light
colorscheme zellner
else
set background=dark
colorscheme nightfly
endif
endfunction
autocmd FocusLost,FocusGained * call DynamicColorThemeSwitch()
call ...
3
It's really doable with execute... and call()
function Foo(func_ref) abort
execute printf('nnoremap <buffer><cr> :call call(%s, [])<CR>',
\ string(get(a:func_ref, 'name')))
endfunction
3
The :help command, just like every other built-in commands, is defined in the C code that is compiled into the Vim binary.
You can find in src/ex_cmds.c in the Vim source code.
(Note also that user-defined commands are only allowed to start with an uppercase, precisely to separate them from built-in commands. Also, functions and commands are completely ...
3
Yes, expand(.) with argument '<cfile>':
expand({expr} [, {nosuf} [, {list}]]) *expand()*
Expand wildcards and the following special keywords in {expr}.
'wildignorecase' applies.
...
When {expr} starts with '%', '#' or '<', the expansion is done
like for the |cmdline-special| variables with their ...
2
The function is called deletebufline()
let l:bufnr = bufnr('thatbuf')
if l:bufnr != -1
silent call deletebufline(l:bufnr, 1, '$')
call setbufline(l:bufnr, 1, GetListOfLines())
endif
UPD. deletebufline() was added in Vim 8.1.0039
2
I feel like this is all overly complicated, you can realize this with just a few lines of regex:
s/<\(\w\+\)>\%#/&<\/\1>/g
This expression will allow you to add a closing tag after the cursor:
- < matches the start of the opening tag
- \w\+ matches the tag content, we include it in braces so that we can use it in the replacement
- > ...
2
The .vim/plugin directory is not where you want to unpack your plug-ins! (Yes, I known the name would suggest so...)
If you want to use Vim 8 packages (which I believe you do), then you should unpack it under a directory named ~/.vim/pack/*/start, where the component after pack can be named anything (suggestions are bundle, vendor or your username.)
See :...
2
Came across this question is looking into a different ^J phenomenon.
Register Situation
In your RegBuff function, you yank text of the buffer into the register with yy. This is linewise yank, thus it appends the newline ^J to the register.
Printing the register ("qp) will not show the newline, but it will insert a line below cursor on which it prints ...
2
I don't think it is possible to directly call this funcref from within mapping -- by the time you press mapped key funcref is already disposed/freed/unavailable.
You can try to persist funcref though (either in global or script local dict):
let g:my_funcs={}
function! Foo(func_ref) abort
let g:my_funcs.f = a:func_ref
nnoremap <buffer> <cr&...
2
Here's what you're looking for:
function! CustomSearch()
let currentLine = getline(".")
let col = col('.')
while col >= 0
let col -= 1
if currentLine[col] == 'a'
echo 'found a!'
break
endif
endwhile
endfunction
inoremap <C-v><C-b> <C-o>:call CustomSearch()<CR>
I'm ...
2
The issue with your function is that you're using plain normal, when you need normal! to make it non-recursive, which you need to ignore the b mapping you created. That should be enough to fix the problem.
But a simpler way to implement what you're trying to accomplish is to use an <expr> mapping and the ternary operator, to expand to b when on a ...
2
Without the yr mapping (or any other mapping that starts with y), the code does not work. Why?
That's because you're calling feedkeys() with the 'x' option, which will also process the typeahead buffer. So you need the next keypress to be in that buffer in order to execute the full command.
For example, if you type yG, you need the G to be on the buffer at ...
2
I would start from
echo match(reverse(split(getline('.')[:col('.')-2], '\zs')), the_char)
that is likely to be much more efficient than any loop in vimscript.
But, what is the exact objective? There are many more possibilities depending on what you are exactly looking for:
a boolean that indicates whether there is one char? (=> no need to reverse)
a ...
2
As @LucHermitte pointed out, the problem causing the E127 error is that you're trying to redefine function ReSource() while that function is running.
Note that you don't need to use a function to save and restore the cursor while sourcing your vimrc, only thing is that you'll end up using a global variable, but you can clean up after yourself in the same ...
2
I'm maintaining lh-brackets that provides several things here:
redoable insertion of various pair of brackets
adding a new pair is quite easy e.g.
:Brackets <+ +>
:Brackets <div> </div> -trigger=<m-d>
removal of empty pairs on <BS> when the cursor is in the middle of the pair (note that it works with the previous (<+|+&...
2
You're missing a final <CR> at the end of your normal command. If at the end of normal you have an incomplete command, it will cancel it as if <Esc> or <C-c> was pressed.
So this should fix it:
execute "normal! :'<,'>retab\<CR>"
(Using \r inside the double quotes is also possible.)
But in this case, there's probably no need ...
2
Have you tried partials?
{'on_exit': function('OnEvent', [parameters])}
This is what I do in lh-tags+lh-vim-lib to register things like: delete a temporary file and print a finished message on close_cb with vim -- I've never tried to use neovim.
2
You can use the zero-width \%# to match the cursor position, so something like
substitute/\%#\s\+$//e
on the current line, and your other pattern on the other lines:
global/\%#/substitute/\%#\s\+$//e
vglobal/\%#/substitute/\s\+$//e
Unfortunately, by the time either :substitute is happening, the cursor position has moved. So let's save it and make the ...
2
This is what happens:
i - enter insert mode
a - call func -> var = 1, but you left insert mode!
a - executed in normal mode -> append -> you are back in insert mode
a - call func -> var = 2, but you left insert mode!
a - executed in normal mode -> append -> you are back in insert mode
...
You might want to try
inoremap a <c-o>:let b:var = Foo(b:var)...
Only top voted, non community-wiki answers of a minimum length are eligible
Related Tags
functions × 196vimscript × 77
key-bindings × 32
vimrc × 19
neovim × 10
variables × 10
regular-expression × 8
autocmd × 8
search × 7
command-line × 6
buffers × 6
visual-mode × 6
macro × 5
error × 5
indentation × 4
cut-copy-paste × 4
insert-mode × 4
normal-mode × 4
vim-windows × 4
external-command × 4
folding × 4
quickfix × 4
range × 4
command × 4
syntax-highlighting × 3