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
4
votes
2answers
411 views

Reliably get the character at a byte index in a string

I'm doing some string processing in vimscript. I obtained a byte index for a character in the string and now want to obtain the character at that byte index. For single-byte charsets (like latin1) ...
0
votes
0answers
303 views

Vim-Airline TabLine not displaying with arrows

I have set up Vim-Airline, but Vim-Airline-Tabline's tab seperator is showing vertically rather than as arrows. My tabline: Expected tabline: init.vim relating to airline: let g:...
3
votes
3answers
417 views

What does the 'z' flag for search() do?

I was looking at the documentation for search() (:help search()). I noticed that there is a "z" flag: 'z'    start searching at the cursor column instead of zero When the 'z' flag is not ...
3
votes
2answers
762 views

Vimscripts: check if item in list

I was wondering how to check if an element is in a list. I have the following function defined in a file: let g:some_list = ["a", "b", "c", "d", "e", &...
0
votes
1answer
135 views

VIM: How to call a vimscript function after press a shortcut or execute a command?

I want to know how to call a function after a normal or Ex command. Examples: bind a function to search command. When I input /xxxx to search xxxx, also call the function. press * to highlight ...
1
vote
2answers
557 views

How to delete a line from a variable?

Inside a function, saved multiple lines of external command output to a variable. How to delete a line from the variable? Like first line. I can run let tmp = split(variable, "\n") to assign the ...
0
votes
1answer
62 views

How do I enable mouse in netrw only (terminal vim)?

I would like to be able to use my mouse to scroll and click on directories to expand them in netrw. I don't want the mouse to be on for any other filetype/buffertype, so that I can still highlight/...
5
votes
2answers
146 views

Unit Testing Vimscript built-ins: possible to override/mock or inject substitutes?

I found a bug in the CoC plugin, and I was going to fix the bug, but first I wanted to write some documentation/learning tests as well as a regression test. The question was how: unit testing does not ...
1
vote
2answers
1k views

Get filetype by extension or filename in vimscript

Is there a way to get filetype for given extension or filename For example: let my_f='text.rb' let my_ft=GetFileType(my_f) echo my_ft " should output ruby
3
votes
1answer
48 views

Backward search in current line with search() and z option

I am trying to write a couple of simple functions to find the next and previous links in Vimwiki and move to them. Links in Vimwiki are indicated with the syntax [[link to something]]. Since I only ...
4
votes
1answer
2k views

Searching with vimscript function [duplicate]

I want to create a custom search in vimscript, using this line for searching: :execute "normal gg/[A-Z]\\+*.:\<cr>" it will tell me (correctly) that it searched for the pattern: /[A-Z]\+3a.*: ...
2
votes
2answers
83 views

How to find first item that satisfies predicate?

:help index explains that only exact matches are returned. How would you add a predicate to the search, so that we could find the first element that matches that? I want to write this: let i = 0 ...
1
vote
1answer
357 views

How to use TexMathZones in Markdown Syntax, for Snippets?

I am a recent and very happy Vim User. I write Mathematics and Computer Science Lecture Notes in Markdown Files, organized in a Vimwiki. I use the UltiSnips Snippet Plugin. This plugin provides me ...
5
votes
3answers
4k views

Select multiple words, one at a time, then replace them all

Goal: Select multiple words, one at a time, then replace them all. I'm new to Vim; I come from Visual Studio. I would like to achieve one important feature that helped me a lot in my productivity: ...
6
votes
2answers
182 views

Convert absolute path to one relative to another absolute one

Context I’m working on an 'omnifunc' function for Markdown editing. It’s supposed to help me link to other Markdown files while I’m typing a link – based on file content though, not file name. Suppose ...
0
votes
1answer
45 views

Map the - key to "delete the current line, then paste it below the one we're on now"

I am doing the exercise of learn vim the hard way I have trouble mapping "the - key to "delete the current line, then paste it below the one we're on now". I tried: :map - dd p :map - :...
0
votes
2answers
74 views

What is the vimscript's no operation statement?

What is the NOP code for vimscript?
0
votes
0answers
42 views

Tool for convert multi files vimrc into single file

i try to collect my .vimrc files into single file is there anyway to generate single file using vimscript depending on source vimscript keyword or python code example of my .vimrc if isdirectory($HOME....
0
votes
1answer
51 views

Get count of string in vim

To get the count of a string I usually do something a bit hack-ish such as the following: Do a :%substitution on the string in question, such as: # how many times does the string "<Item "...
5
votes
1answer
63 views

How can I get the full command name from shortcuts such as :syn, :synt?

The commands :sy, :syn, :synt, and :synta all run the :syntax command. For a script I'd like to know which command the user typed on the commandline, for example: get_full_command('syn') " ...
0
votes
0answers
56 views

inoremap doesn't work

Question summary Currently I'm writing a pull request for this vim plugin which should set commas and semicolons automatically. At the moment it looks if it should set a comma/semicolon/double point ...
2
votes
1answer
139 views

How do I make a function stop when an error occurs?

I just wrote a function which tries to call another function, but I got the syntax wrong. However, I was horrified to see that the next line in my function happily started executing anyway. It is as ...
2
votes
2answers
229 views

Set line numbers based on line count

If a file has a large number of lines, I'd like to enable line numbers, otherwise leave them off by default. If I run the command: if line('$') > 150 | :set number | endif while vim is open, file ...
1
vote
1answer
170 views

Vim change keyboard language.. via shortcut

I want to change keyboard language via vimscript shortcut. So that i ...
1
vote
1answer
101 views

imap <expr> call multiple functions

I want to map <CR> to call multiple functions e.g. Func1() Func2() Func3() I tried: imap <expr> <CR> Func1() Func2() Func3() But I got "E15:Invalid expression" How can I ...
2
votes
2answers
258 views

How do I pass a full filepath to a function in a Vim plug-in?

I have an exe program that checks for certain errors in a text file. I want to create a shortcut (like \+M) to run that exe file against the text file. My exe file's path is C:\Users\xyz\Documents\...
3
votes
3answers
1k views

How to find plugin vimscript function definition?

Assume I am using some plugin function e.g. For vim-plug, I put this code in my .vimrc at the end call plug#end() How to find out where is this function plug#end() is located? from vimrc directly ...
1
vote
1answer
30 views

How to invoke Linux shell env variable into vimrc?

How to invoke Linux shell env variable into vimrc ? I split vimrc configurations into multiple files and included these files in vimrc via :runtime [file]. This works well. :runtime vimrc.d/vimrc....
2
votes
2answers
240 views

How to provide shared variables for plugins?

I've written some Vimscript code* in which it's convenient to have a set of shared color names. To use the names, so far I've had to rely on code like this: execute 'source ' . expand('<sfile>:...
0
votes
1answer
79 views

How are quotation marks used in this dotfile?

I'm a complete Linux beginner, trying to understand dotfiles. I've got the .vimrc dotfile, and tried to add the following command to the bottom: " <Ctrl-l> redraws the screen and removes ...
1
vote
1answer
281 views

What are the allowable characters in identifiers?

In Vim, what characters are allowed in identifiers, such as variable names and function names? I looked up the help for both variables and functions and neither one defines the allowable character ...
2
votes
1answer
162 views

What does `_` do in this map() expression?

I'm trying to understand the code suggested in https://vi.stackexchange.com/a/28506/6189: function MyLinks(pat, spat, ssub, sflags) abort return glob(a:pat, v:false, v:true) \ ->map({_, v ...
1
vote
2answers
89 views

why are variabes prefixed with `a:`?

I'm trying to understand the code suggested in https://vi.stackexchange.com/a/28506/6189: function MyLinks(pat, spat, ssub, sflags) abort return glob(a:pat, v:false, v:true) \ ->map({_, v ...
4
votes
2answers
241 views

What is `v` in this vimscript?

I'm trying to understand the code suggested in https://vi.stackexchange.com/a/28506/6189: function MyLinks(pat, strip) abort return glob(a:pat, v:false, v:true) \ ->map({_, v -> printf(...
0
votes
1answer
26 views

append strings with order

The original text: line1 lline2 line I want to add strings into the below format: line1 lline2 x y z line Move cursor at the 2th line,and ctrl+;,enter into ex mode: for s in ['x','y','z'] call ...
2
votes
1answer
43 views

Convert working commands to VIMRC friendly mappings (ie: :%s with regex)

Is there a simple way of taking a previously issued command and converting it into a .vimrc-friendly keybinding? For example, I have a simple substitution utilizing regular expressions that won't work ...
0
votes
1answer
83 views

synIDattr returns nothing in function

I have this part of code: function! Test() echom synIDattr(synID(line('.'), col('.'), 1), 'name') endfunction autocmd TextChangedI <buffer> call Test() I expected that every time I change ...
0
votes
0answers
119 views

E716: Key not present in dictionary: "3"

I have this global at the top of my .vimrc: let DevEnv = { 'enabled' : 0, 'open_dirs' : 0, 'term_bufnr' : 0 } One of my .vimrc functions calls: exec 'bd! ' . g:DevEnv.term_bufnr Note that g:DevEnv....
0
votes
0answers
17 views

How to open terminal in split in vimrc with keybinding [duplicate]

I am trying to open a new terminal (size of 10) inside of neovim. I use the command, :split|resize20|term. It works well, but I want to make a hotkey to do this in my init.vim nnoremap <C-P> :...
0
votes
0answers
32 views

Effect of changing directories in a vim command [duplicate]

When executing a vim command, such as the following: :w!<CR>:!cd %:p:h What is the effect after the command is finished? Is the new cwd the directory that I've cd'ed into? Or does that only ...
0
votes
1answer
48 views

How do I go back to my previous position after onoremap yank?

I have an operator function that yanks double quotes or single quotes, ciq works fine because I want to be inside the quotes but when I do yaq (yank a quote) I don't want to move to the place where ...
0
votes
0answers
59 views

Print command output on terminal on vim exit

I am trying to run shellcheck upon exiting vim, however I do not get the output of the command I run. I've tested running something like touch foo instead, and I managed to see the file being created, ...
2
votes
2answers
146 views

How can I determine if *any* window in the current tabpage is in diff-mode?

So, I found this useful answer about iterating buffers; but due to the tabpage-specific duality of windows in Vim, I failed at adapting it to my needs. Thus: how can I construct a boolean expression, ...
1
vote
2answers
852 views

Filter dict items using list of keys

Give the following dictionary: let mydict = {'foo': {'a': 1, 'b': 2}, 'bar': {'c': 3, 'd': 4}} And a list of keys: let myfilter = ['foo', 'asdf'] How would one do a concise "left outer join (if ...
0
votes
1answer
46 views

How to return keybinding from a function?

I want the below function to return keybindings but all it returns is a string. nmap <expr> <space>t ManageTerminal() function ManageTerminal() if bufname('terminal') ==# 'terminal' ...
36
votes
1answer
20k views

Call a Vim function silently

I wrote a little Vim function that moves the cursor to the first character of the current line. If the cursor was already on the first character, then the cursor is moved to the first column instead. ...
0
votes
2answers
32 views

Swap selected block of code upward/downward?

I want to write a Vim Script which can achieve the following function, which is my favorite feature in some IDE. Say I have the following example code: I'm a line and I just stay here. I'm another ...
2
votes
2answers
73 views

Why does system(cmd) append ^@ to its output?

I want to set node's executable path for neovim. So I have added this line in init.vim. let g:coc_node_path = system('which node') But when I open nvim I get this error. [coc.nvim] "/home/akash/....
0
votes
0answers
22 views

How can I know when a real file is closed/opened from/into a visiable window?

What I'm trying to do is, when a window is closed(bw,bunload)/opened (NOT fzf, quickfix q:, buftype=nofile, etc..) resize all the other windows. This code works but I was wondering if there was a ...
1
vote
1answer
77 views

How to 'join' using formatexpr and function

One can find several examples of formatting one sentence per line such as right here: How to set up Vim to work with one sentence per line? The question states that: My current solution is to join a ...

1 2 3
4
5
25