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
6
votes
1answer
578 views

How can I expand a symlinked file path instead of the real file path?

I am editing a file which lives in a directory that is a symlink to another directory, i.e. % ls -alh ~/www/experiments total 0 drwxr-xr-x 3 ubuntu ubuntu 80 Dec 5 19:52 . drwxr-xr-x 3 ubuntu ...
6
votes
1answer
298 views

Is there an autocmd-like event for when operator pending mode is entered?

I'd like to make a plugin to display currently pending keys while in operator-pending mode. As the first part of that, how would I register an autocmd to run when operator-pending mode is entered? I'm ...
6
votes
0answers
817 views

Resources for scripting vim with python [closed]

I love vim, I love python, and I dislike vimscript. So I would like to know how to write plugins (or throwaway scripts) for vim in python. Of course, the first reference should be :h python, but I ...
5
votes
5answers
6k views

How to generate closing tags for HTML?

I want to do the following: When I insert an HTML tag (which is a sequence of text that starts with <tagname tag_attributes and ends with >), as soon as I press > to insert the > character, ...
5
votes
4answers
1k views

How to generate patterns for comments?

As a way of organizing my codes, I like to include some titles as comments with specific patterns (I wish I can add ASCII art!). Consider for example, putting the following to mark a specific block of ...
5
votes
2answers
3k views

Calling function to insert text with inoremap

I have a function that is called via a mapping when I am in insert mode: inoremap <buffer> <M-i> <ESC>:call TheFunc()<CR> The function is supposed to "calculate" a string (l:...
5
votes
4answers
2k views

How to shorten the result of `ls` to get only the file name and not the whole path?

I've been using Gvim for a while, but lately stopped using GUI options and went to just commands. :ls is used all the time, but I have a huge project and it is hard to see the file name quickly in the ...
5
votes
2answers
413 views

How can I set an option differently depending on time of day?

I would like a piece of Vimscript which would query the time of day check whether it's later than, say, 6pm set an option based on the result Why? So that I get the dark solarized color scheme ...
5
votes
3answers
3k 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: ...
5
votes
2answers
1k views

What's the best way to initialize a list of a predefined length?

I need to create a list containing a predefined number of zeros. I know that I can create an empty one with let foo = [] but I would need to initialize it with a number of zeros contained in a ...
5
votes
1answer
1k views

get exit status from system() call

Calling the function system("<command>") or systemlist("<command>") retrieves the stdout output from a shell call to <command>. How can I retrieve the exit status of that command? ...
5
votes
2answers
613 views

Is there a way to yank directly to a vimscript variable?

I have some boilerplate code in one of my vimscript functions: " Save register 'a' so we don't clobber it let savereg = @a " Yank the current WORD into register 'a' and save it to 'input' normal "...
5
votes
1answer
976 views

Function to toggle set colorcolumn

I like the set colorcolumn=80 option which highlights the column at 80 characters, however I find it annoying when working with multiple split windows. I'd like to have an option to enable or disable ...
5
votes
2answers
486 views

How to get all/global match results for a string?

With vim script I can do let found = matchstr("abbaba", 'a') and get back "a" as a result. How would I matchstr to get back a list of all matches for the string? found == ["a", "a", "a"] as in g/a/
5
votes
2answers
511 views

`mode()` always seems to return 'n'

So I do a visual selection and hit a vnoremap which calls a function and executes mode(), but everything it ever does it output n, indicating I'm in normal mode, even though that is certainly not true....
5
votes
1answer
2k views

How to initialize variables in while loops

In C you can do something like this: int foo(int a) { return a + 1; } int main(void) { int a = 0; while ((a = foo(a)) < 10) { /* */ } } How can I do the same thing in ...
5
votes
2answers
592 views

How can I run a function / action every period of time (once an hour)?

I want to run https://stackoverflow.com/questions/7588531/vim-set-color-theme-based-off-time-of-day So something like: if strftime("%H") < 12 set background=light else set background=dark ...
5
votes
1answer
732 views

Check if key is already bound in vimscript / Map key only if not mapped already

I'm wondering if it is possible to check in vimscript if a key (or key combination) is already bound? I know about the :map command which lets me see visually which keys are bound to what actions, ...
5
votes
2answers
302 views

Why vim plugins have a check on &cp

I'm trying to wrap my head around vim plugin development and I see a constant check on a variable to see if the plugin has already been loaded like: if exists('g:loaded_plugin') || &cp finish ...
5
votes
1answer
2k views

Is there a function which can return a list of matching text for groups in a regex?

Bash can natively match regular expressions in the [[ ]] construct, using the =~ operator. When it matches something, the result is stored in the BASH_REMATCH array variable. BASH_REMATCH contains, in ...
5
votes
2answers
622 views

Is there a version of “strpart” that is aware of characters, rather than bytes

According to :help strpart(), the parameters start and len are bytes, not characters, so that returns a string of 2 characters, starting with the letter Beth. What I'd like to have is a function, ...
5
votes
2answers
139 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 ...
5
votes
2answers
244 views

Is it necessary to always add ! to function and command

I can't remember start from when and why, i always add ! to function and command: function! .. ... endfunction command! ... But :h E122 says: When a function by this name already ...
5
votes
1answer
647 views

VimL/Vimscript: how to access local variable of outer function from its inner function?

I posted this question on stackoverflow already, but realized afterwards that it's probably better to ask here. So I have a function of the following form: function! s:my_function(dict_arg) let ...
5
votes
1answer
703 views

Is 'source'-ing a vim file simlar to 'include' in other languages?

When a vim file is source-ed is it like including the raw text of the file into the current script is it more like executing its contents and returning to the current file?
5
votes
2answers
3k views

How does the findfile() function work?

I don't understand the findfile() function in Vim-Script, what am i doing wrong? set encoding = utf-8 cd %:p:h let b:gitiginorfiles = findfile('.gitignore', system('git rev-parse --show-toplevel'), -...
5
votes
3answers
3k views

How to format a mapping to an if statement?

I'm had some trouble defining a mapping in my .vimrc. I want <leader>sv to source my .vimrc, but first I want to write, if I'm currently editing it. Goal The intention of the code is this: if ...
5
votes
2answers
511 views

Vim syntax highlighting for ChatScript language?

I'm looking for a ChatScript language Syntax highlighting, of course for the Vim editor. ChatScript is a great (underrated) open-source language to build chatbots dialog flows scripts, made by Bruce ...
5
votes
1answer
3k views

Is it possible to obtain the displayable area, width and height of current buffer in Vim?

I know Vim provides functions to display window's dimensions. But does it also provide ways for me to know current buffer/split's dimensions? Like how many lines and columns in displable area? ...
5
votes
1answer
2k views

How to use <Leader> in a normal command?

In the command-line mode or in a vimscript I can use the following to duplicate a line: normal! Yp Now let's says that I map my leader key to do that: nnoremap <Leader>a Yp. If I use this key ...
5
votes
2answers
224 views

Is there a better way to create this “semi-strong” left motion?

I'm pretty new to vimscript, and I wrote this little function to map H so that the cursor will go to the first non-whitespace character of the line, or to the beginning of the line if the cursor is ...
5
votes
4answers
3k views

How to pass control characters, e.g. <c-e> to a function in a mapping?

I often find myself accidentally pressing <c-e>/^E while in normal mode inside a terminal buffer. This causes the terminal buffer to scroll down and a line with a ~ is displayed, like when your ...
5
votes
2answers
2k views

Is there a way that I can identify which window is a terminal after calling the `:term` command in neovim?

Is there way I can identify a window by a number in neovim and then jump to it with a macro ? Or better yet...Is there a way that I can identify which window is a terminal after calling the :term ...
5
votes
1answer
4k views

How to escape single quote in Vim

I have this mapping in my .vimrc nnoremap <silent><leader>vlc :exe "!open -a vlc '".getline('.')."'"<CR> It does work great with some files, but I'm getting ...
5
votes
1answer
292 views

Putting the value of an argument into part of a regex in vimscript?

Is it possible to pass a variable to a function in vimscript, and then use that value in a search and replace regex? For example: function! AddTW5Links(tiddlerSetName) %s/\(*\+\)\(\[\[\(.*\)\)\...
5
votes
1answer
1k views

How can I avoid warnings about unused variables?

I have some code in my ~/.vimrc that (simplified) looks like this: function! SomeAppendFunction() let failed = append(0, ["Some header text"]) endfunction command SomeAppendFunction :call ...
5
votes
3answers
1k views

How can I make a search in vimscript let n and N look for more?

I'm writing a script that does a search like so: normal /needle^M. This works like I want, except that I want to be able to use n or N to repeat the search. In case it helps, here's the full ...
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') " ...
5
votes
1answer
450 views

What are/could be the potential advantages of learning VimL or vim scripting language? [closed]

I have been using VIM for quite some time now and I wish to level up my VIM skills. I have started going through some VimL or vim script as it's called. To motivate me further, I am just curious to ...
5
votes
3answers
954 views

Is it possible to create a function with the same name for different filetypes (and different implementations)?

Is it possible to create several functions with the same name whose implementation depends on the file type? For example, I could imagine a function named CurrentFunctionName that returns the name of ...
5
votes
2answers
388 views

Skip over character in insert mode, even at the end of a line

I have this map in my .vimrc: inoremap <C-space> <C-o>l So that, eg, if I'm in insert mode: I can just type <C-space> to move out of parens: It all works great in the middle of a ...
5
votes
3answers
4k views

How to access a custom python module inside vim?

This is a Python 2.7 question. I have a custom python 2.7, that I have compiled and have been using successfully across my system. My custom python has the YAML modules installed as well. The system ...
5
votes
1answer
536 views

Vim Function to Send Highlighted Text to a Git Commit

Problem: Suppose I have the following text in my vim buffer: This is a commit msg. Suppose further I have a git repo located at ~/my_repo. Goal: Make a vim script so that I can highlight the text ...
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 ...
5
votes
1answer
489 views

Can I set breakpoint in a vim plugin script written in python?

When a vim plugin is developed in a mixed script of vim script and python, can we set breakpoints and debug the python script. I tried to use PDB module, but vim is dead when it go to the pdb....
5
votes
1answer
424 views

How can I insert text from a function without triggering autoinsertion of comments?

I have this in my .vimrc function HeaderTpl(fchar, boxchar, width) let sfile = expand("%:p") return a:fchar . " " . repeat(a:boxchar, a:width) . "\n" \ . a:fchar . " " . sfile . "\...
5
votes
1answer
699 views

Interrupting (blink-highlighting) function if mapping is invoked again

I have an adapted version of Damian Conway's Die Blinkënmatchen in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N: " 'hlsearch' makes it hard to tell which is ...
5
votes
1answer
513 views

How do you convert between non-literal strings and literal strings?

I was writing a small plugin, when I decided that I should give the option of customizing which key triggers the plugin's function. Here is an example of this: let g:foo_key = "\<Tab>" While ...
5
votes
1answer
804 views

What is an effective way to learn vimscript? [closed]

Let's say I've been working with vim for some time; I've dabbled with my ~/.vimrc and copied-and-pasted some snippets in I find on the way, with minimal changes. I'm beginning to get a hazy ...
5
votes
1answer
2k views

In an autocmd what's the use of the BufRead event?

I'm trying to learn a bit of vimscript and currently reading the 12th chapter of Learn Vimscript the Hard Way. In this page, the author says : A common idiom in Vim scripting is to pair the ...

1 2 3
4
5
25