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.

Filter by
Sorted by
Tagged with
1 vote
1 answer
45 views

Do we need to escape backslash in character in Vimscript?

As title, specifically, is it correct that one of the backslashes in the following regex is redundant in vimscript: syntax match MyGroup "[a-zA-Z0-9_:\\]" I tried to ask Copilot but while ...
1 vote
4 answers
2k views

How can I view a list of all of the features that can be checked for using `has()` in Vim?

How can I view a list of all of the features that can be checked for using has() in Vim? I recently came to know the has() function for testing the presence of "features" (see below). :h ...
1 vote
2 answers
58 views

Press CTRL+W in a vimterminal programatically from vimscript?

Say my cursor is in a Vim terminal. Normally I press Ctrl w Ctrl w to flip to from the Vim terminal to my buffer. How do I do that with vimscript? I tried the whole CTRL+SHIFT+V thing when writing the ...
0 votes
1 answer
54 views

Need help to make a by-default `:RunWithFile` of this toggle function

related to this post, How to make a toggle function in .vimrc that toggles multiple configs of almost same type? @BLayer's code func! MapRunCmd() abort if exists("b:rwfcmd1") && ...
0 votes
1 answer
37 views

Is there any way to change the colour of individual tabs?

It's possible to change the colours used for the whole tab bar, but is it possible to set the colours used for individual tabs? That way I could colour-code tabs based on the file path, or extension, ...
3 votes
2 answers
707 views

Detecting the Enter and Leave of Visual mode event

Are there any events similar to InsertEnter and InsertLeave that I could use for visual mode? Looking through the helpfiles, I did not fine any. If not, how would I be able to detect if Vim enters or ...
0 votes
1 answer
35 views

init.vim: E194: No alternate file name to substitute for '#'

I have a problem with adding a keymap to delete all my buffers. Namely, I used the following map in my init.vim file (source): nnoremap <leader>B :%bd|e#|bd#<CR> And I get the following ...
1 vote
1 answer
324 views

How to autocmd BufWritePost stdout to tidy upon :w?

tldr; How do pass stdout of BufWritePost to an external command? On command line I can run: cat index.html | tidy -indent -modify I'm trying to call tidy automatically when I :w This is what I have ...
0 votes
1 answer
32 views

Go to error messages' `:help`

How can I search the help entry for the last errors after the execution? Example " try to run a command that doesn't exist :qwerty E492: Not an editor command: qwerty I usually use :echo errmsg, ...
0 votes
1 answer
117 views

Get default autocomplete list using Vimscript

If I type an unfinished word in Vim (in insert mode) and press CTRL+N I get a list of autocomplete suggestions. (this list contains words being used in that file and other files that are currently ...
0 votes
2 answers
81 views

How to re-open the same window with the same buffer therein?

Consider the following snippet: vim9script # Set stuff in the newly created window var my_win_nr = winnr('$') var my_win_id = win_getid(my_win_nr) win_execute(my_win_id, 'wincmd L') win_execute(...
1 vote
4 answers
1k views

Cannot switch tabs when in terminal tab

For using the terminal while working with Rust. I've added this config (NVIM) (TTerm one). if has('nvim') command Ter vsplit term://zsh command Tter tabe term://zsh else command Terminal ...
5 votes
4 answers
3k views

Send keys to a terminal buffer

I want to map a keyboard shortcut to execute the last command in a terminal buffer that I have open. I've made some progress: I am storing the buffer's channel id correctly but when I try to send ...
4 votes
1 answer
118 views

Constant-update Words Per Minute in Vim?

I'm interested (mostly just for fun) in seeing my typing speed while working in Vim. I saw this post but this requires starting and stopping a function. I would prefer this to be a constantly updating ...
1 vote
1 answer
183 views

How to use command to insert a space in insert mode?

As title. Given that I'm already in insert mode and there is some insert mode function. Inside the function, I want to insert a space by command, how?
0 votes
1 answer
163 views

How to get the same message and error behavior as normal `n` when calling `normal n` in function?

I wrote a vimscript function that prevents the cursor from changing its location on the screen when I press n: function! s:my_n() let l:start=line("w0") let end = line("w$")...
0 votes
1 answer
220 views

Useful script to manage buffer and window?

I want to create a vertical split from current buffer for reference another file/the same file. After I've done with this split I don't want to see this buffer-for-reference in the split, but: If ...
4 votes
2 answers
326 views

Is there a Set datatype in VimScript?

I am aware of lists and dictionaries in Vim. But are sets supported in Vim to store only unique values? Or do I just have to use a dictionary with some dummy value?
4 votes
2 answers
587 views

How can I make zz not center cursor when approaching end of file?

When I press zz at the end of file, the line is displayed at the center of screen by having many empty lines having empty lines beyond end of file is great, but this is too much. Is there a way to ...
4 votes
2 answers
807 views

Can the arguments to a custom Vim command be grouped by quotes?

I would like to define a custom Vim command that takes arguments grouped by single or double quotes. For example, if my arguments are a "b c" then I want a to be considered the first ...
0 votes
1 answer
71 views

Stop flushing the last command when hitting enter

Suppose I wrote this command: command! -nargs=1 E echo ' -> <args>' and when user feeds an argument, :E abc I intend to see the following effect as they hit Enter: :E abc -> abc Of ...
1 vote
1 answer
54 views

Going to insert mode at the end of command with lua function

I am using Neovim and I want to go to insert mode at the end of this command. vim.cmd([[command! DunB lua run_only_current_file("below") ]]) I have tried | startinsert and | :startinsert. ...
0 votes
2 answers
76 views

Count whitespaces between cursor and preceding non space character

In case I have the following line // Component: <Component name here> | where | is the cursor marker. How can I count, using vimscript, the total number of white spaces ...
2 votes
1 answer
121 views

How to catch the last WinResized event?

The WinResized event is useful to know the change of window height and width. It works fine upon :resize and CTRL W<. But when a user drags and drops the status line or vertical split bar to ...
2 votes
1 answer
2k views

How to get the content between two specific lines in vimscript?

How can I write a function to get the content between mark 1 and mark 2 for further processing? mark can be a string, or a function call, it doesn't matter. mark 1 foo1 foo2 foo3 mark 2
0 votes
2 answers
241 views

How can I write a custom formatexpr to specify the point at which 'gq' wraps?

I don't want VIM to automatically wrap text, so setting textwidth is not a solution to this problem. This comment gives a useful outline, but I'm not really sure how to go about doing this. My first ...
10 votes
7 answers
10k views

How to refer to "~/vimfiles" on Windows and "~/.vim" on Linux?

Consider an answer to How do I install a plugin in Vim/vi?: call plug#begin('~/.vim/plugged') " For MS Windows, this is probably better: "call plug#begin('~/vimfiles/plugged') And more generally, ...
0 votes
4 answers
83 views

How can I camel-case extant lowercase text?

I use vim for coding. I have a disability where some of my fingers hurt and I have trouble holding down shift and a letter to produce uppercase. The same goes for parentheses. How can I convert ...
0 votes
1 answer
53 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 ...
16 votes
1 answer
5k views

What does three commented curly brackets ("{{{) at the end of a line mean? Is it just a comment?

In the tcomment plugin, specifically in: ~/.vim/pack/tml/start/tcomment.vim/autoload/tcomment.vim and ~/.vim/pack/tml/start/tcomment.vim/plugin/tcomment.vim I'm seeing this at the end of some lines: ...
1 vote
1 answer
58 views

Vim in tmux and WSL: cannot highlight using ctermbg

I'm using Vim inside tmux, which runs in WSL (due to work reasons). As per documentation, I decided to change my Visual highlighting style using :hi cterm=NONE ctermbg=<insert-color-here>. ...
0 votes
1 answer
108 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" ...
2 votes
2 answers
75 views

Vim script: How to swap a line with multiple lines?

In a Vim function, I want to append \ and carriage return after AND. I modify the line using the following expression: let new_line = substitute(new_line, ") AND (", ") AND \\\n (",...
0 votes
1 answer
139 views

How to set the mode from a vimscript?

If I save the result of :call mode() in a variable, how can I restore that mode later?
1 vote
1 answer
98 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
500 views

How to read Vim stack trace?

When debugging Vim plugin you may be confronted to error messages of the kind: Error detected while processing function GrandFather[2]..Father[5]..Son Line 6: E484: Can't open file foo.txt What ...
1 vote
0 answers
29 views

How can I handle whitespace of the path with nvim_buf_get_name?

I am using Neovim and I use this code to get the full path of the current file. local currnetPath=vim.api.nvim_buf_get_name(0) I want to use this variable for opening the current HTML file, so I need ...
0 votes
1 answer
96 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?
0 votes
1 answer
60 views

mkview file code meaning

I use :mkview %.vimview to create a view file under current directory, and in the file created there is a line of code like this: if bufexists(fnamemodify("~/PycharmProjects/rl/report/...
0 votes
2 answers
102 views

How to use a variable from register in command mode?

I have a list of cities London Madrid Paris Brasil I want to write a macro, that will add a ul tag and wrap each city in to an li. I think, i need to create a variable like: let i = 4 Than to write ...
0 votes
1 answer
154 views

How can I open a vim fold so that it is not affected by `foldclose=all`?

In my config file I have set foldclose=all so that folds get automatically closed upon cursor leaving them. However, sometimes, I want a specific fold not to close for a while. Because folds below ...
16 votes
3 answers
11k views

How can I append text to the current line?

I'm currently trying to figure out vimscript and I stumbled upon the append() function. The documentation states that you can specify the line to which an expression should be appended to, so I tried: ...
1 vote
3 answers
108 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
0 answers
27 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. ...
0 votes
1 answer
160 views

How to enter terminal buffer using command?

As title. Assume my cursor is already at the terminal buffer, but it's still in normal mode. I'm trying to use TermEnter to enter terminal mode when ever I switch to a terminal buffer.
7 votes
1 answer
3k views

What is the "." (period/dot) operator in Vim?

I'm trying to fully understand the following command in Vim: :exe "grep -R " . shellescape(expand("<cWORD>")) . " ."<cr> I got the use of expand function (force the expansion of into the ...
0 votes
1 answer
50 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
25 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}.....
1 vote
1 answer
66 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
75 views

vnoremap to sort highlighted lines by length not working as expected

I found a command that would sort highlighted lines by their length and wanted to make a mapping for it so that I could avoid typing it every time. vnoremap ,s :<C-U>'<,'> ! awk '{ print ...

1
2 3 4 5
32