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
11
votes
2answers
8k 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: ...
11
votes
2answers
2k views

What does `idem` mean in the VIM docs?

One example would be in the List docs: :unlet list[3] "idem
11
votes
2answers
5k views

Vimscript: Help with Autoloading, Scope & <SID>

I've been working on modularizing & converting a code in my vimrc into some self-contained and reusable bundles/plugins plugins. I've run into an issue with autoloading & scope that I'm having ...
11
votes
1answer
2k views

Several lines instructions

Is there a way to split a one-line instruction into several lines? For example, I'd like to transform setlocal variable_name = condition1 ? "1" : condition2 ? "0" : condition3 ? "a long string" : "...
11
votes
1answer
1k views

Is it possible to create custom “set” commands in my rc file and call them from a modeline?

I recently asked a question in which I learned that it is not possible to pass anything except parameters to :set in vim modelines. I understand the security concerns, but I'm wondering if there isn't ...
11
votes
2answers
486 views

Is there a way to detect if I'm recording in vimscript?

Is there anything like eval("is_recording()") that I can call in my vimscript to find out if I am currently recording a macro? I would have thought this works: eval("mode(1)") but that just ...
10
votes
1answer
3k views

vimscript: how to repeat a string N times?

I know . operator concatenate strings. But how to repeat a string N times, similar to "abc" * 3 in python?
10
votes
1answer
6k views

vimL sleep within a function

I'm trying to define a function that will make it easier to see where the cursor is location by putting cross-hairs around it for 100ms. Here's my attempt: function! Flash() set cursorline ...
10
votes
1answer
638 views

Can I search through 'oldfiles' with a specific name or extension?

I use the oldfiles and browse oldfiles commands all the time. I find them extremely useful, because it's easier to hit my leader mapping, scan through the files really quickly, and hit q<number>...
10
votes
4answers
2k views

How to save and restore the result of the command 'set'?

From :h :set we know that :se[t] Show all options that differ from their default value. In a vimscript I'd like to save all these options which have been modified by the user, modify them and ...
10
votes
1answer
3k views

Usage of the =~ operator

I'm a little confused on VimScript's regex comparison operators. If I look through :h =~? my understanding is that =~ can be used to do a regex match (with the ? making it case-insensitive). But ...
10
votes
1answer
2k views

Using a variable in a regex pattern [duplicate]

I'm trying to write a function that will delete each line that matches a pattern. But that pattern will change based on the words held in a list (made from a dictionary). I was thinking it would be a ...
10
votes
1answer
2k views

Prevent refresh of screen until function completes

I have a function that does a lot of moving and outputting of text into the current vim buffer, and when I run it, seeing all that happening at blinding speed is a bit disconcerting. How can I ...
10
votes
2answers
2k views

How can you use vim as a stream editor? [duplicate]

Like most programmers, I perform a lot of repetitive tasks. In optimising my workflow, I'm taking some of those repetitive tasks, and refactoring them into shell scripts. One thing that I'm trying to ...
9
votes
5answers
5k views

Vimscript - Surround word under cursor with quotes

I'm writing a Vim plugin which will need to surround the word directly under your cursor with a quotes. I have tried using simple solutions which use commands like "b" and "e" executed in normal mode, ...
9
votes
2answers
4k views

Include symbols in <cword>

expand(<cword>) gives me the word under the cursor, but it excludes symbols such as @ or .. For example, if I call it at the end of @word, it will get me word and exclude the @. Is there a way ...
9
votes
2answers
3k views

How to write condition with regular expression?

I'm aware of the following comparison: if @% == "/tmp/crontab.zi5NeVPGRc" set nobackup set nowritebackup set noundofile set noswapfile endif But how can I use regular expression? For example ...
9
votes
2answers
813 views

How safe is it to use an unknown `_vimrc` or `vimscript` file?

I do not know if this question should be asked here or not. In almost every language, malware exists. Is the same also applicable for vimscript? Suppose vim is running with high system privileges. Is ...
9
votes
2answers
2k views

Manipulate chars with their ascii code

Let say I have let l:letter = 'A' is there a function that takes l:letter ascii code and transforms it to 'B'? I mean something like let l:next_letter = l:letter + 1 I know there's <Ctrl-A> ...
9
votes
1answer
2k views

Is there any way to distinguish between neovim and regular vim?

I was trying to migrate to neovim from vim. But in the end, there's some plugins which require python3 support and neovim package doesn't build with python3 by default( I'm on ubuntu, install neovim ...
9
votes
2answers
634 views

How to source a part of a file

I can source a file, e.g. my vimrc with :so % or :so <filename>. But what do I do when I need to source only part of a file. I want some way to visually select a portion of the file with :h v ...
9
votes
2answers
2k views

How to jump to buffer from ls?

Is there any way to jump to a buffer by entering buffer number right from :ls? I mean, why this command expect me to Press ENTER or type command to continue, if pressing for example 2, wouldn't do ...
9
votes
1answer
5k views

function to call shell script with arguments

I am making my own function to call a bash script that compiles and runs my program. Here is the code I have tried. function Compile_and_run(game, major, minor) !_compile a:game a:major a:minor ...
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(...
9
votes
2answers
2k views

Are script local functions (s:funcName()) unit testable?

Consider a script local function in a plugin/myplugin.vim file. For example myplugin.vim: func! s:someFunction() return 4 endfunc I would like to unit test this function. In the simplest ...
9
votes
1answer
634 views

How to allow count *before* my custom operator?

I'd like to define my own operator. Vim's builtin help has a nice little tutorial on it, where they show you can create your own operator with opfunc and g@: nmap <silent> <F4> :set ...
9
votes
1answer
232 views

How to define an operator which is updating while it is taking input?

I have defined an operator mapping which takes a region of text, then asks for an input string and then aligns the region with Tabular using the input string as an argument. It works nicely. I have ...
9
votes
1answer
334 views

Output Vim display to Text

I wonder if there is any command similar to :TOhtml, but for plain text only and representing the whole Vim display. For example, given the display: It would create the following text file: 1 B ...
9
votes
1answer
965 views

Regex that prefers shorter matches within a match? (This is more involved than non-greedy)

I'm trying to create a script for performing fuzzy search inside a buffer. The main idea is to take some input, and insert .\{-} between every pair of characters, e.g. foo becomes f.\{-}o.\{-}o. This ...
8
votes
2answers
960 views

Add a “Press any key to continue” in VimL?

In an unusual state, I want to ensure that a user sees a warning message upon startup, and presses a key to continue. I could use sleep <seconds>, but I'm sure that vim can do this. Go vim!
8
votes
7answers
8k 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, ...
8
votes
2answers
669 views

What's the functional difference between the key notations “\<xxx>” and '<xxx>'?

I stumbled recently upon a mapping in the Vim Tips Wiki that uses an expr-quote: nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR> and realized it still worked ...
8
votes
3answers
2k views

Tutorial for vim programming? [duplicate]

I am a veteran user of vi. When I started using it, there was basically nothing... Now, we have vim. But, how do I program it? Where does one start? With help? Not sure this is a good start.
8
votes
1answer
296 views

Run a cmap only when searching

I have the following mapping: cnoremap <silent> <CR> <CR>:echo 'Enter pressed'<CR> This works as expected; the :echo command runs every time I press <CR> in the command ...
8
votes
1answer
325 views

How to escape significant keywords from syntax files?

I came accross this line in a syntax file.. it does not work: syntax keyword Type Transparent I guess this has something to see with the fact that transparent is a significant keyword for Vim. What ...
8
votes
2answers
1k views

Drawbacks of using Python to develop new code in a VIM plugin

In one of the vim plugins I use, we have been thinking about using python vs vimscript going forward (https://github.com/python-mode/python-mode/issues/813#issuecomment-344754563). I understand that ...
8
votes
1answer
336 views

How to color comments after a defined column

I once was given these materials to enable this helper for vim to show kind of a vertical line in order to help enforce PEP8 rules on line length limit at column 80: " Long lines highlighting. ...
8
votes
1answer
3k views

What's the difference between `/x` and `:/x`

Assume buffer content: aa bb yy xx aa bb What's the different between /xx and :/xx?
8
votes
3answers
372 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 ...
8
votes
2answers
2k views

How to design a command in a plugin that can be called from vimrc

I'm working on a plugin that allows users to create custom operators. The operators would apply a VimL expression to the motion or text object that the operator moves over. Keeping a clean user ...
8
votes
1answer
2k views

Visual scrolling/visual <C-e> and <C-y> across wrapped lines?

This has been asked on stackoverflow before, but it seems it still has no solution. Perhaps that has changed with Vim 8.0. I'd like to be able to scroll (i.e. scroll-wheel, or <C-y>/<C-e> ...
8
votes
2answers
366 views

Automatically delete closing pair without causing unbalanced pairs

I am writing a VimL function that, in insert mode, allows a <BS> through the opening part of a pair ([, {, etc.) to delete the closing part of the pair if it is the next character after the ...
8
votes
1answer
2k views

How to change the value of timeout for one key command?

What I want To temporarily change the timeout value for certain key commands Where I've looked From :help 'timeout' I have : The timeout only happens when the 'timeout' and 'ttimeout' options tell so. ...
7
votes
1answer
6k views

Most annoying slow-down of a plain-text editor! [closed]

I am a happy user of VIM, except for some bits which I really want vim to improve on. One main issue is frequent slowdown of VIM spoiling the amazingly productive text-editing experience on a fastest ...
7
votes
4answers
2k views

Is it possible to populate the quickfix list with the errors of vimscript functions?

I often write functions in my ~/.vimrc or in ~/.vim/autoload/myfunctions.vim, which when executed result in an error such as: Error detected while processing function SomeFunction: line xx: ...
7
votes
3answers
3k views

Within a function, Is there a way to determine if vim is in insert mode?

Within a function, is there a way to determine if vim is in insert mode (or was in insert mode prior to calling the function)?
7
votes
2answers
3k views

How to check if a shell command exists

I'm now using silent! let l:status = system(l:command . ' > /dev/null') if match(l:status, '\cunknown') != -1 || match(l:status, '\cnot') != -1 echo 'command not found' return 0`enter ...
7
votes
1answer
602 views

Why is there a limit on the number of function arguments?

I am reading :help :call, which states the following: Call a function. The name of the function and its arguments are as specified with :function. Up to 20 arguments can be used. This makes me ...
7
votes
1answer
5k views

Understanding the line() built-in function

I'm reading vim help about the line function and here is an example I found: :let failed = append(line('$'), "# THE END") In my vimrc I also found line('.'). The documentation says: line( {expr}) ...
7
votes
4answers
1k views

Store the number of matches in VimScript function?

Is there any way to store the number of matches in a variable in a VIMScript function? For instance I'm using: %s/,//gn

1
2
3 4 5
25