New answers tagged

1

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.


2

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] enddef 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 ...


3

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. ...


3

Here's an example that doesn't depend on 'W'. Enter ALFALFA, position the cursor on the first L and compare: :call search('ALFA') 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. The ...


5

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: call search('...


4

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 ...


1

Note: Please use the other solution I posted instead of this kind of broken one. Original post: 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 ...


2

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. echo "Yes" else echo "No" endif To check that the item is not in the list, use index(g:some_list, a:item) == -1 instead.


1

In case of * (or similar normal mode commands) you just remap it: func! MyFunc() echom "Hello World" endfunc nnoremap * *:call MyFunc()<CR> For the search /xxxx you can redefine <CR> mapping in commandline: func! SearchCR() if getcmdtype() =~ "[/?]" echom "Run Something if CR was pressed in /search"...


0

Riffing off of this answer, I added these lines to my .vimrc to enable mouse support only when starting, entering, or leaving netrw buffer. function! NetrwMouseOn() set mouse=n endfunction function! NetrwMouseOff() set mouse= endfunction au FileType netrw :call NetrwMouseOn() au FileType netrw au BufEnter <buffer> :call NetrwMouseOn() au ...


3

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 ...


3

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 ...


0

If you need to set filetype for cases like "myfile.ext.template" you could use this autocmd BufRead,BufNewFile *.template execute "doautocmd filetypedetect BufRead " . fnameescape(expand("<afile>:r"))


1

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 ...


2

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 ...


1

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 (...


1

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 ...


1

The idiomatic answer is :%substitute/pat//gn 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.


5

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). The ...


0

fu! StartsWith(longer, shorter) abort return a:longer[0:len(a:shorter)-1] ==# a:shorter endfunction


2

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) endfunction Alternatively, your Fn can accept two arguments (index, value) instead of one (and, maybe, even somehow cache "...


2

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)[0] When I need the index, I use map + index :echo index(map(copy(state.commits), 'predicate ? 1 : 0'), 1)


Top 50 recent answers are included