People who code: we want your input. Take the Survey

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
0
votes
1answer
27 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
62 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
48 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
109 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
216 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
109 views

Vim change keyboard language.. via shortcut

I want to change keyboard language via vimscript shortcut. So that i ...
1
vote
1answer
59 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
207 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
904 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
29 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
187 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
65 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
252 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
88 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
150 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
41 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
55 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
65 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
45 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
37 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
140 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
823 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
19k 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
71 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
54 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 ...
5
votes
2answers
1k views

Call function from string name

I am trying to achieve the following : Having a set of predefined functions in my script Having a map of relation between the functions and their argument Calling the functions with their argument ...
2
votes
1answer
73 views

How do I find the ex/vimscript equivalent of a normal-mode command?

Some operations can be done with either normal-mode commands or ex/vimscript commands. For example, :resize 1 does the same thing as 1 <C-W> _. In scripts, it's possible to simulate normal-mode ...
1
vote
1answer
62 views

What is right way for string concatenation in vimscript?

I wrote some vim plugin, that correctly works with vim8, but when I try run it with neovim I get error about invalid expression in string: let var = str_first .. str_second When I change .. to . all ...
2
votes
1answer
451 views

VimWiki - create a “smart” index page for subdir/section of wiki?

I'm using VimWiki for a writing project and for certain sections (like "chapters", contained in sub-directories) I want to create indexes that work similarly to the way VimWiki's diary index ...
2
votes
6answers
696 views

How to delete a pair of parenthesis with backspace

I'm using vim to code C++ and I want to handle the parenthesis more easily. I knew that there was a plugin named Surround, which can help us but it is not what I expected. I set some shortcuts as ...
1
vote
1answer
54 views

Vim: How to manipulate appearance of signs in .vimrc?

I have seen a guy on YouTube who has some sort of Markdown rendering in Vim. The * sign from a list is displayed as a •. When the cursor travels over it, the actual sign shows up. I have checked his ....
3
votes
1answer
221 views

how to loop through list of files?

I can do :echo globpath('~/path/to/dir', 'string*') to get a list like: /home/alec/path/to/dir/string_xyz123.mkd /home/alec/path/to/dir/string_xyz456.mkd /home/alec/path/to/dir/string_xyz789.mkd but ...
0
votes
1answer
54 views

Measuring how much I write in vim by comparing word count on each save

Suppose I want to write N words related to my studies in Vim everyday as my daily goal. Every time I am done writing something I can do g+Ctrl-g and then I can send that number variable to a file. I ...
0
votes
1answer
33 views

Vimscript: Placing the cursor at a defined position in the buffer

I use vim as a note-taking app by editing a text file and appending notes to it. I have written a simple function to insert a block of text as follows: function! AddBlock2(...) range let l:hdr = ...
2
votes
1answer
237 views

Is it safe to define more than 120 signs?

I am working on a plugin that could potentially define more than 120 different signs. The vim / neovim documentation specifies that you can define about 120 signs (not sure why this number and why so ...
2
votes
2answers
47 views

Get first and last visible line from other buffer than current

I know that I can get the first and last visible lines in the current buffer by using line('w0') and line('w$'), but is there a way to do it for other buffers that are visible but not active as well? ...
3
votes
1answer
57 views

Vim shows incorrect theme in error

Basically, all I'm trying to do is enable the syntax option. syntax enable colorscheme cobalt2 For some reason, I get no errors if I comment out the first line, but in case I don't comment it, I get: ...
7
votes
3answers
343 views

How to use register in a script without side effect?

I want to create a function like this: function Foo() copy something from buffer into a register do something restore register endfunction Foo must have no side effect, every register ...
1
vote
2answers
73 views

How can I turn these mappings into a vim function?

I use the following mappings to toggle relative numbers in visual mode: nnoremap <silent> v v:<c-u>set nonu rnu<cr>gv nnoremap <silent> V V0:<c-u>set nonu rnu<cr>gv ...
0
votes
1answer
242 views

How to exit vimscript? [duplicate]

How to exit .vim script file? I want something like this in my init.vim if exists('g:vscode') "Do not execute rest of init.vim, do not apply any configs exit endif "...some configs... ...
4
votes
2answers
122 views

Is it possible to override the filetype of a text file based on the first line?

I have some specifically formatted text files that I'd like to detect when I start vim. When I load these files now, the filetype is automatically set to text. I only want to override that if the ...
1
vote
1answer
53 views

Execute command with special characters from user input

I would like to correctly intepret the <cr> in a command that is created based on user input. For example, this function runs correctly and it will split the line a the location where the cursor ...
9
votes
1answer
4k views

How do I check whether a given directory exists?

I need to determine whether a certain path exists and points to a directory, like the -d test in the shell. I know about the filereadable() function, which does basically this for files: filereadable(...

1 2
3
4 5
24