Questions tagged [vimscript]
A scripting language embedded into Vim. It can be used to customize Vim to suit your needs and to create plugins. Also called VimL.
1,552
questions
0
votes
0
answers
12
views
How to detach/disable a buffer-update callback in nvim
In vim, we can implement a vim script, which monitors text changes in details, with listener_add(), listener_remove(), listener_flush(). In nvim, nvim_buf_attach() is available to attach a callback. ...
1
vote
3
answers
89
views
How to swap lines programmatically in file?
Suppose, we have a perl file where we want to swap shebangs located in the first line:
"#!/usr/bin/perl -d" <-> "#!/usr/bin/env perl"
The first one runs the file with ...
0
votes
1
answer
38
views
Have Neovim automagically invoke external program on the Neovim-saved file while exiting
I have the following python code stored in my $HOME/bin/email-process
#!/usr/bin/python3
from sys import argv
# open up the email contents file
infile = open(argv[1], 'r')
# read it into an array ...
1
vote
1
answer
34
views
Script in autoload is not running
I'm using Linux, Vim (nox) 9.0, and Neovim 0.9.4; I have both environments synchronized.
What would prevent a .vim file in ~/.vim/autoload/ or ~/.config/nvim/autoload/ from autoloading?
The only ...
0
votes
1
answer
43
views
Best way to downport funcrefs and partials from 8.2 to 7.4
The 8.X version of vim introduced new possibilities for the function() command: the ability to
create Partial functions or to package funcrefs with arguments.
The difference of the function() help is ...
0
votes
1
answer
31
views
Is there an efficient way to delete the duplicated block with same start and end to keep unique?
Description
I have many large size files may including the same function definition.
The block description is start with .start .* and end with .end
In this example there is one duplicate definition ....
1
vote
1
answer
79
views
Put the mutiple search or replace results into different split window
Dsecription
I am using :g/regex to do global search and it always list --More-- So I need to click Enter or Space to see the full results, it takes too much time.
I found one way to achieve my goal ...
1
vote
1
answer
49
views
How to save the files when global replace completed in one command line
I have so many huge size of files and always need to do search and replacement
so I map the command as nnoremap aa :let i=1 \| g/regexp/ s/^/\=printf("%1d. ",i)/g \|let i =i+1\|:w! 1.txt
...
1
vote
1
answer
196
views
How is the right hand side of an <expr> binding evaluated?
I like using <cr> to save a file, except in a quickfix window where I want <cr> to enter the directory or file shown.
Here's one way to achieve this (works as a .vimrc by itself). I'm ...
3
votes
5
answers
206
views
How can I determine the Linux distro in vimscript?
In shell I do conditional stuff depending on distro like this:
eval "$(source /etc/os-release && typeset -p ID)"
if [[ $ID =~ ^(rhel|fedora.*|amzn|ol|rocky|almalinux)$ ]]; then
...
...
0
votes
1
answer
22
views
Recursive fold only up to &foldlevel?
I often use zC to close nested folds under the cursor. However this command is not useful in files with a single, global level-zero fold (e.g. fortran program...end program and latex \begin{document}.....
0
votes
1
answer
49
views
Calling variables from SHELL and using them in Vim config (vimrc) [closed]
Question
I am trying to use variables from SHELL inside my Vim configuration file, now, I have researched how to do this and asked ChatGPT about how as well. It is possibly due to the specific ...
1
vote
1
answer
44
views
How can I use Regex in helpgrep?
I would like to use Regex in helpgrep.
For example, searching for lines or sections that contain two words. Something like :helpg 'word1' & 'word4' to find this line as my result:
word1 word2 ...
0
votes
1
answer
88
views
When I use helpgrep, how can I highlight my search results?
I'm using :helpg session in my Vim.
I would like to highlight the pattern in the results like :set hls does for normal search.
Is there a way to do this?
1
vote
1
answer
138
views
Why so % does not work when vimrc changes with another editor?
I opened a file in Vim.
Then opened vimrc file with another editor (VsCode) and made some changes.
I would like those changes to be reflected in my Vim session.
I tried :so % but it didn't work ...
0
votes
2
answers
62
views
Why doesn't echo % do anything in command mode?
I read the section of current-file in the document. (see :h current-file) And I have a question.
if % stands for the current file name, why I can not use echo %?
1
vote
1
answer
43
views
What is the meaning of lbr in Vim?
I write set lbr in my vimrc file but I don't know the meaning of that. What does this option do?
I know it's about line-breaking but I need more explanation.
This is from the Vim document and I can ...
0
votes
1
answer
50
views
How to show the clock and battery usage in lightline?
I have Lightline installed for a more detailed statusline. Unfortunately, any time I do something like this
set statusline+=%{strftime(\"%H:%M\")}
it seems like Lightline will override it, ...
2
votes
1
answer
47
views
How to use function argument in a mapping?
Let's take a look at the example below.
function! Print (text)
echo a:text
endfunction
function! Mapping_test (parameter)
nnoremap m1 :call Print (a:parameter)<CR>
nnoremap m2 :call ...
1
vote
1
answer
45
views
Using the completion function for a command that is passed as an argument to another command
Is it possible to programmatically get a command's definition so that its completion function can be called manually? Alternatively, is it possible to define a command such that the first argument is ...
1
vote
1
answer
42
views
How can I match a regexp containing newlines?
When developing a vim plugin I'll publish soon, I want to highlight a text contained in a variable g:last_completion_text.
Here below the code to
define an highlight group named PrompterCompletion
...
0
votes
0
answers
38
views
How to use <Leader> in a normal command today?
The answer to the question with the same title doesn't seem to work for me. Not sure if the behavior has just changed overtime.
To reproduce the issue, open Vim via vim -Nu filebelow.vim, where the ...
2
votes
1
answer
75
views
Neovim paste without overwriting register w/ Lua
So I know with vimscript you could make paste not overwrite your register with this:
vnoremap p pgvy
And I've tried a few ways to get this to with with Lua such as:
vim.api.nvim_set_keymap('n', 'p', '...
1
vote
1
answer
78
views
What are the escaping rules of command arguments?
The following does nothing:
:file /tmp/[some file]
or:
:file "/tmp/[some file]"
The following set's the file to /tmp/[some file]:
:file /tmp/\[some file]
What are the rules that :file ...
1
vote
1
answer
38
views
How to configure my own highlight for Vim editor in Linux using ~/.vimrc and ~/.vim/colors/mycolors.vim files? [closed]
I want to configure and customize my own syntax highlight for Vim editor in Linux Ubuntu 20.04. In my home directory, I created two files, ~/.vimrc and ~/.vim/colors/mycolors.vim. Here is the full ...
1
vote
1
answer
66
views
How to generate random string with required length in Vim?
I am using this code for console log
nmap cll yiwocll<Esc>p
I wanted to add some random string inside this console log, assume 10 characters symbols length.
How to achieve this?
1
vote
1
answer
39
views
How do I get size of "whole" Vim instead of a single window?
I want to create a floating window, with the width of the entire Vim viewport. I had used winwidth() function, but I've encountered problems when windows are split.
I could get a list of windows in a ...
2
votes
1
answer
81
views
Make { ( and ) } normal mode motions skip blank lines
Normal mode ) } and ( { navigate paragraphs and lines but they don't skip blank lines. (They land on them.)
This is obviously the preferable behavior when writing code, but when writing "normal&...
1
vote
2
answers
150
views
Is there a Vim plugin for testing VimScript patterns?
I'm new to VimScript and trying to learn how to construct patterns.
Is there a Vim plugin like RegExr.com for testing patterns?
-1
votes
1
answer
50
views
Is It Allowed to Assign popup_create() To a Variable?
I know it returns the window ID, but when I run the function that does what's on the question title, it keeps telling me that a 'List is required'. I don't know what's going on.
" Check the type ...
0
votes
1
answer
86
views
Does Vim Cache the Output of system()?
Does it? I'm not sure. I'm getting this error, and it seems to be looking for a nonexistent cache folder:
E282: Cannot read from "/var/folders/yh/z3ydby_56qz7mltrddsv3lqw0000gn/T/vi3voZp/0"
...
1
vote
1
answer
66
views
How Do I Turn a String into URL Encoded String?
I'm trying to turn a JSON format string into parsable URL encoded string.
Here's my process:
" Read the contents of the file separated into lines
let lines = join(getline(1, '$'), '\n')
" ...
0
votes
2
answers
45
views
How to convert a string into a list of matching and non-matching substrings?
I have a list that I'm trying to filter.
I'm planning to separate a string into parts of the string that matches a pattern and parts that doesn't.
Like if the string is:
<h1>hello</h1>
...
1
vote
2
answers
78
views
VimL: check if Lua is supported
How do I check if Lua is supported?
From my .vimrc, I only want to put an if around a Lua heredoc so that my config works in plain vim or versions of neovim which didn't support Lua (pre 0.5.0?).
What'...
0
votes
1
answer
46
views
Using marks in custom functions/plugins without changing user's marks
I am looking for a way to save and restore marks at the beginning and end of my custom Vimscript function. I am writing a plugin and I don't want my plugin to change any of the user's marks, so I ...
1
vote
1
answer
81
views
Is there a hotkey to jump to the beginning or end of a python if statement in Vim?
I use VsCode with the Vim plugin. In other languages using Vim to jump to brackets is good enough, but python if statements don't have brackets.
I have found no Vim plugin or macro for this, is it ...
1
vote
1
answer
48
views
Syntax highlighting of a submatch
I have the following
syn match specialNumber /1/ contained
syn match normalNumber /\d/ contained
syn match line /^\d$/ contains=specialNumber,normalNumber
hi def link ...
0
votes
2
answers
60
views
Use "normal" in function
I'm writing a function to automate some task.
I did this:
function InitTTbuffer()
" Split file and find the first imap
:exe "normal \<C-w>\<C-s>/imap\<CR>"
...
0
votes
4
answers
82
views
Check if a script is sourced
I use quite a number of plugins now and the init.vim script for nvim has become quite unwieldy. So I started separating out the individual configuration files for each individual plugin.
And now I ...
0
votes
3
answers
116
views
What exactly does Vim need from you to make a separate folder for your scripts?
Every coding configuration need I have for Vim works just fine if I put it all in the .vimrc file, but I'm curious: let's just say that I wanted to make my own scripts or plugins, and my .vimrc file ...
2
votes
0
answers
30
views
How do search's b and z flags interact?
I'm using searchpos instead of search is I can echo the column where the match happens, which, with example file below coincides with the matched character.
123456789
^
|
+-- cursor is ...
3
votes
3
answers
95
views
Auto-fix missing Python f-string prefix in insert mode
I am constantly forgetting to add the f prefix in Python when I'm within a string and start inserting a variable ("foo bar {var}").
I then do: <ESC>F"if<ESC>f{a and finish ...
1
vote
1
answer
165
views
What is `normal! zvzz` in vimscript?
I found here the following command normal! zvzz
But I don't know what it does.
0
votes
0
answers
43
views
How can I get the number of matches of pattern without altering the search buffer nor moving the cursor?
My end goal is to be able to highlight all the matches of the word being typed and echo their number, but this question is about one specific aspect of this, which I've been having trouble with, ...
1
vote
1
answer
49
views
How to evaluate vim script in the substitution field and put text with it?
I have to replace a lot of variables s1.size() with wa, and replace s2.size() with wb accordingly. I have made next substitution request:
:'<,'>s/\vs([1|2])\.size\(\)/w\={"1":"a&...
0
votes
1
answer
24
views
How can I add more to this command?
I have been trying to make this command into a function which gets called by command, but I am not having much luck with it.
" Clear registers
command! WipeReg for i in range(34,122) | silent! ...
0
votes
2
answers
101
views
Cannot get block-wise visual selections in vimscript?
I am searching for how to get visual selection in vimscript. And I found this: https://stackoverflow.com/a/61486601/4438921, it is great, but the only issue is that, in block-wise visual mode, I ...
0
votes
1
answer
199
views
How to pass an argument for glob in fzf.vim Rg to support searching in specific files?
I am using fzf.vim plugin with ripgrep.
Fzf comes with the command :RG and :Rgright out of the box that lets me to search for keywords in files.
However, often I need to search for keywords in ...
0
votes
1
answer
51
views
Using variable to find & replace while incrementing number with regex
Given the following text:
0 : Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(24.0),
child: RichText(
text: TextSpan(
...
0
votes
1
answer
40
views
My plugin got sourced first before init.lua
I make a colorscheme plugin with vimscript (the code sucks, as I've never write a plugin before), there it checks if a global var exist or not.
" nvim-colors/colors/kalm.vim
...
if !exists('g:...