Patch 8.2.2233 introduced the charidx() function that returns the character index given the byte index in a string. You can pass this index to the strgetchar() function to get the character at that index.
If you have the patch 8.2.2324, you can use the charcol() function in a :def function:
def Getchar(): string
return getline('.')[charcol('.') - 1]
nnoremap GG :echom Getchar()<CR>
It works because in Vim9 script, inside a string slice, indexes match characters, not bytes. This is documented at :h expr-:
In Vim9 script:
If expr8 is a ...
During development of vimscript code, having to source the whole file where it resides just to try out a snippet is sub-optimal. In my experience, there are always a bunch of variants of the code we're trying to make work that we don't want to throw out (yet), but we need to keep commenting it out (or in) so that the :so % doesn't trip up all over the place.
Here's an example that doesn't depend on 'W'. Enter ALFALFA, position the cursor on the first L and compare:
This moves the cursor left to the first A in the line. Surprising! Why didn't it move forward?
:call search('ALFA', 'z')
This moves the cursor right to the second A in the line. This seems more like what you'd expect.
An example that illustrates the use of this flag is below.
Add the text 'abc123' to a buffer. Place the cursor on 'b' and execute the following command:
call search('abc\zs123', 'W')
The cursor will be positioned on '123' even though the cursor is after the start of the match. Now move the cursor back to 'b' and execute the following command:
The "z" flag is really used to what it describes in the documentation, it'll make the search start from wherever your cursor is placed at the moment.
As for the behaviour you have experienced, what you might have missed on the docs is:
'w' Wrap around the end of the file
'W' don't Wrap around the end of the file
If neither 'w' or 'W' is ...
Note: Please use the other solution I posted instead of this kind of broken one.
I have this mapping in my .vimrc to do precisely what you ask:
:vmap <f9> y:exec substitute(@", '\n\s*\\', ' ', 'g')<cr>
So, visually select some text, press F9, and it will be vim-compiled.
It has one important shortcoming: it fails if the ...
index() can be used to check whether or not an item is in a list.
The body of your function should be written like this:
if index(g:some_list, a:item) >= 0 " If item is in the list.
To check that the item is not in the list, use index(g:some_list, a:item) == -1 instead.
In case of * (or similar normal mode commands) you just remap it:
echom "Hello World"
nnoremap * *:call MyFunc()<CR>
For the search /xxxx you can redefine <CR> mapping in commandline:
if getcmdtype() =~ "[/?]"
echom "Run Something if CR was pressed in /search"...
Riffing off of this answer, I added these lines to my .vimrc to enable mouse support only when starting, entering, or leaving netrw buffer.
au FileType netrw :call NetrwMouseOn()
au FileType netrw au BufEnter <buffer> :call NetrwMouseOn()
I would argue that even if it were possible, it's not a good practice to mock has( in this way. Instead, you should test directly against the versions of vim which you are trying to support. Many vim plugins do have CI pipelines which test the most latest version of vim, vim 8.0, vim 7.4, various neovim versions, etc. Automated testing frameworks in ...
I don't think this is possible either.
I'd say you may found a solution depending on how the call to has() is used.
Sometimes you will be able to overwrite the direct consequence of a call to has() (like changing/removing a function/a command... that depends on has() result), other times it wouldn't be possible.
In all cases, if the plugin you want to ...
So I tested your code a bit and I realized that the issue with your backward search comes from the flag z, I don't really have a convincing explanation because reading the doc I would have used it too.
'z' start searching at the cursor column instead of Zero
I changed your pattern to a simple . and realized that <c-h> only matched the last character ...
Before creating a mapping you should try the actual command by yourself: Here you want to delete a line with dd and then paste it with p. When you do that in normal mode you simply press the keys ddp without anything else in between, so that is what you need to do too in your mapping.
This means that you mapping should be map - ddp. You don't need to use the ...
According to the url provided:
The simplest possible statement in C that behaves like a NOP is the so-called null statement, which is just a semi-colon in a context requiring a statement. (A compiler is not required to generate a NOP instruction in this case; typically, no instructions whatsoever would be generated.)
Alternatively, an empty block (...
VimScript interpreter ignores colons and spaces, so : could be qualified as "nop" command. For example, echo 'foo' | : is the same as just echo 'foo'.
However, there's a problem: due to Vi compatibility :| is forcefully parsed as :print, so echo 'foo' | : | echo 'bar' surprisingly transforms into echo 'foo' | print | echo 'bar'.
To deal with this ...
The idiomatic answer is
Mass mentions searchcount() which returns a dictionary of interesting items. You'll need to set the last search pattern, though (e.g., / or write to @/), or pass pattern in the options to the function.
The way shortcuts work is that cmdidxs1 and cmdidxs2 are defined in ex_cmdidxs.h, which contain a index to the actual command definition. It's not easy to re-implement this in Vim, as it depends on the order in which the commands are defined in ex_cmds.h (the reason :s is :substitute is because this occurs before any other command starting with :s).
This sort of "functional programming" in VimScript could be implemented by :h filter() function. For example,
function! FindItem(object, Fn) abort
return get(filter(copy(a:object), "a:Fn(v:val)"), 0, v:null)
Alternatively, your Fn can accept two arguments (index, value) instead of one (and, maybe, even somehow cache "...
The fastest way I've found (in execution time) was to filter everything and check the first item if not empty. This is because loops are really slow
:echo filter(copy(state.commits), predicate)
When I need the index, I use map + index
:echo index(map(copy(state.commits), 'predicate ? 1 : 0'), 1)