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
23 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 "...
1
vote
1answer
112 views

How to check if a string starts with another string in vimscript

I know I can use regex matching with ~= and that stridx(haystack, needle) == 0 does work, but if I just want to verify that one string starts with another, I don't want to waste resources checking the ...
2
votes
2answers
74 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 ...
2
votes
1answer
104 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 ...
0
votes
0answers
45 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 ...
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') " ...
2
votes
2answers
212 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
83 views

Vim change keyboard language.. via shortcut

I want to change keyboard language via vimscript shortcut. So that i ...
1
vote
1answer
45 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
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
52 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
47 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> :...
6
votes
2answers
119 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
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
42 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
34 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, ...
0
votes
1answer
44 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' ...
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 ...
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 ...
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/....
1
vote
1answer
46 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 ...
2
votes
1answer
71 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
57 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
159 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
128 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(...
2
votes
1answer
60 views

Quickly correcting spelling mistakes but omitting some cases

I am using vim as a note-taking app. Following an earlier answer here , I have the following mappings which allow me to quickly correct earlier spelling mistakes: " spelling imap <C-l> <...
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 ....
2
votes
1answer
385 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 ...
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 = ...
0
votes
1answer
50 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 ...
2
votes
1answer
236 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 ...
3
votes
1answer
194 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 ...
2
votes
2answers
44 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
44 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: ...
-1
votes
1answer
192 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
102 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
2answers
71 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 ...
3
votes
1answer
134 views

Vim does not call functions correctly when wrapped in another function

Please consider these 3 small functions: " place sign >> at line 10 function PlaceSign() call sign_define('a', {'text': '>>', 'texthl': 'LineNr'}) call sign_place(10, 'g1', 'a', ...
1
vote
1answer
52 views

Vim expands percent sign '%' in URL

In my script, I am trying to open an URL in my default browser using silent exec '!rundll32 url.dll,FileProtocolHandler '.a:url where a:url is an URL-encoded string containing percent % signs. Vim ...
2
votes
2answers
57 views

Buffer scope variable not exists when run autocmd BufUnload

Describe the issue I use a vim plugin that when use Vim to open a ipynb (python notebook) file, will call system function to use jupytext create corresponding python file to enable use like, YCM to ...
-1
votes
2answers
57 views

How can i make this autocmd BufEnter command work?

i am setting the default vim-statusline, and i made this line on my colorscheme: hi MyStatusLineDevIcon ctermfg=7 ctermbg=0 cterm=NONE So i can color the devicons on my statusline.vim config I call ...
0
votes
1answer
30 views

When hitting Enter keyboard on a file path, open a new tab with the file content

I'm new to vim language and I'd like to see a new tab opening when I hit the enter keyboard on a line containing only a path of a file. When I create a map like this nnoremap <buffer> <cr> ...
2
votes
1answer
94 views

why doesn't regex work in vimscript (while it does in normal search)?

I have simple regex to search for lines that only contain alphabetic characters: ^\(\a\)\{1,}$ which works in normal search /^\(\a\)\{1,}$ but not when invoked from my script: "in .vimrc file: ...
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 ...
3
votes
2answers
167 views

Reversing all characters on each line of a range

In order to invert all characters on each line of a range I could think of the following :s/.*/\=list2str(reverse(str2list(getline('.'))))" where I can put a [range] if I want. Is there any ...
0
votes
1answer
87 views

Get visually selected text - weird behavior when last selected char is multi-byte

I have made a simple function to echo the visually selected text. The function is simplistic as I am only interested in max one-line selection. The problem is, if the last character of the visual ...
2
votes
0answers
50 views

Conditional mapping for two plugins (UltiSnips and Vimwiki)

Vimwiki table default mappings include <tab> for moving to the next table column in insert mode. Also UltiSnips default mappings include <tab> for expanding snippets in insert mode. Here ...
1
vote
1answer
66 views

Get list of marks inside Vimscript

some time ago, I wrote a small plugin in vimscript that was using builtin function getmarklist. Recently I reinstalled Vim and was surprissed that this plugin no longer worked because Vim couldn't ...

1
2
3 4 5
24