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
74 votes
6 answers
26k views

Detect OS in Vimscript

Can I retrieve the current operating system (Windows, Linux, OS X, ..) using pure Vimscript (no Python or Perl)? I want to enable different settings in my (synchronized) .vimrc for different types of ...
muffel's user avatar
  • 1,027
24 votes
3 answers
15k views

How to use a variable in the expression of a 'normal' command?

How can one use a variable when executing something via normal! in vimscript? Example: function! MyFunction(someArg) normal! (a:someArg)l endfunction This should move the cursor someArg steps ...
user avatar
40 votes
2 answers
14k views

Extract the result of a shell command to a variable in a .vimrc file?

In my .vimrc file, I would like to store in a variable the result of an external command, in my case: $ echo $LANG So that my .vimrc file would have : let language = output(!echo $LANG) if language ==...
Feffe's user avatar
  • 1,761
25 votes
2 answers
4k views

What is the difference between single and double quoted strings?

What is the difference between single and double quoted strings? Is there a way to get a variable expanded inside a string, or do they always need to be joined like: 'Hello, ' . s:name . '!'
Tom Hale's user avatar
  • 2,631
23 votes
6 answers
22k 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, ...
Ryan Fredez's user avatar
22 votes
3 answers
2k views

The simplest way to start Vim in private mode

I want to be able to easily start Vim in a private mode. Let's define a private mode as one that utilizes this set of commands: set history=0 set nobackup set nomodeline set noshelltemp set ...
Mateusz Piotrowski's user avatar
42 votes
5 answers
9k views

How do you “chomp” a string in Vim?

Suppose you run the following piece of vimscript: let @z = system("date") This will put a string version of the current date into the z register, but the string will end with a newline that I don’t ...
bdesham's user avatar
  • 2,973
39 votes
1 answer
13k views

What are the new "popup windows" in Vim 8.2?

The latest version of Vim, 8.2, has a new feature called "popups" or "popup windows". They sound interesting but I'm not quite sure what they're good for. Can you enlighten me...preferably with some ...
B Layer's user avatar
  • 19.7k
22 votes
2 answers
3k views

What is the difference between set and :set (commands with or without a leading colon)?

I was looking at Tim Pope's vim-sensible plugin and I noticed this in the README... See the source for the authoritative list of features. (Don't worry, it's mostly :set calls.) When I looked at ...
Ethan's user avatar
  • 473
18 votes
3 answers
2k views

How do I generate Vim documentation from Markdown notes or inline comments, or are there better ways to add documentation?

This is kind of two questions in one. Lately I've been spending a lot of time learning vimscript in-depth, learning vim's internals & taking my first steps writing vim plugins. During this process,...
nfarrar's user avatar
  • 425
12 votes
2 answers
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 ...
Flurrywinde's user avatar
7 votes
3 answers
4k 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 ...
Jacktose's user avatar
  • 863
1 vote
1 answer
93 views

How to display multiple changed files on disk in Vim?

Related to this thread, How can Vim tell me which buffer is changed on disk among multiple buffers? This Vim configuration automatically checks for changes in opened buffers and notifies when a single ...
Mega Bang's user avatar
  • 199
28 votes
6 answers
6k views

How can I really have some fun in Vim? [closed]

I love Vim. I spend my whole life using it: At work I develop code with Vim, I edit config files with it, I write my emails with it. At home I write my todo lists and my buckets lists with it. On my ...
statox's user avatar
  • 49.6k
23 votes
2 answers
7k views

How to restore the position of the cursor after executing a normal command?

I am trying to code a function that replaces the character in the sixth column of my current line with a dollar symbol ($), but I would like my cursor to remain at the position it was before calling ...
Feffe's user avatar
  • 1,761
13 votes
1 answer
3k 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 ...
Aaron Shen's user avatar
  • 2,509
11 votes
1 answer
2k views

How does one include a trailing space in a mapping?

I'm currently trying to create the following mapping in my vimrc: nnoremap <Leader>s :split The :split is followed by a trailing space. This makes sense so I can enter the file name without ...
fvgs's user avatar
  • 243
10 votes
2 answers
839 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 ...
Antoine Cotten's user avatar
8 votes
4 answers
2k 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
leeand00's user avatar
  • 3,491
6 votes
1 answer
427 views

Making a box in vim via python

I am attempting to learn more about scripting Vim and writing plugins with python, but I'm having trouble getting started. Although I've read :h python (first step, read the manual), I had trouble ...
davidlowryduda's user avatar
6 votes
3 answers
2k 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 ...
Flurrywinde's user avatar
4 votes
2 answers
587 views

How can I make zz not center cursor when approaching end of file?

When I press zz at the end of file, the line is displayed at the center of screen by having many empty lines having empty lines beyond end of file is great, but this is too much. Is there a way to ...
zyy's user avatar
  • 239
2 votes
1 answer
2k 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 ...
alec's user avatar
  • 785
2 votes
1 answer
272 views

Quit script after a certain condition is true

I have a script that checks for a certain condition (it checks if pip dependencies are installed). If they are installed and everything is ok, I want to import a bunch of python functions from my ...
SalmonKiller's user avatar
2 votes
1 answer
770 views

How to remove inner content of html tag conditionally?

I've html file (e.g. curl -o page.html http://example.com/) where I would like to remove inner html block for given html tag. I've the following code which works: $ ex -V1 page.html <<-EOF /&...
kenorb's user avatar
  • 18.3k
1 vote
1 answer
507 views

Unmapping filetype mappings

I'm trying to unmap some key sequences mapped by python filetype plugin. While I know a better way, I'm still curious if it can be done with autocommands. Here's what I've got so far: augroup ...
x-yuri's user avatar
  • 349
116 votes
8 answers
139k views

How to edit binary files with Vim?

Is there a way to edit binary files in some kind of hexadecimal mode? For example if I have some binary data shown by xxd or hexdump -C like this: $ hexdump -C a.bin | head -n 5 00000000 cf fa ed ...
janos's user avatar
  • 2,808
19 votes
4 answers
1k views

Set the paste option, but for one insertion only

I want to have a quick way of setting the Vim 'paste' option, inserting some text, and reverting to the previous value of 'paste'. A typical use case insert the OS clipboard content literally, ...
Gilles 'SO- stop being evil''s user avatar
17 votes
3 answers
2k views

Why does vim allow integer division by zero?

I just discovered that vim obviously allows division by zero: :let a=42/0 :echo a prints 2147483647 (which is the value of a). Is this documented somewhere and why does vim allow division by zero?
René Nyffenegger's user avatar
16 votes
1 answer
2k views

How can I merge two dictionaries in Vim?

I have two dicts: :let defaults = {'hello': 'world', 'bye': 'jupiter'} :let override = {'hello': 'mars'} How can I merge the keys from override so that I end up with a new dict like so: {'hello': '...
Martin Tournoij's user avatar
15 votes
3 answers
2k views

How to grep in ex command output?

How does one search for a pattern in ex command output? For example, how to search for a file name pattern in the output of :scriptnames command? In the help for all flavours of grep and vimgrep there ...
Al Berger's user avatar
  • 393
15 votes
2 answers
5k views

How to iterate over buffers in Vimscript

How can I iterate over the current open buffers and say, load the buffer numbers into an array in vimscript? I know there exists arrays or similar data structures in vimscript. I know that every ...
lsund's user avatar
  • 315
14 votes
1 answer
3k 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" : "...
Jcao02's user avatar
  • 432
12 votes
1 answer
4k 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?
qeatzy's user avatar
  • 960
12 votes
1 answer
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 ...
Caleb's user avatar
  • 2,245
12 votes
3 answers
1k 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 ...
dedowsdi's user avatar
  • 6,238
10 votes
2 answers
3k views

Why do functions in Vimscript require a "call" statement?

In Vimscript, if you use a function as an R-value (right hand side value) of an assignment statement, then the function simply evaluates. For example: let count_pattern += str2nr( strpart( execute( ...
Tyler Durden's user avatar
  • 2,091
10 votes
2 answers
4k 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 ...
magnus's user avatar
  • 427
10 votes
2 answers
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> ...
Luke Davis's user avatar
  • 1,397
9 votes
2 answers
4k 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 ...
kAldown's user avatar
  • 203
9 votes
2 answers
2k 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? ...
Jasha's user avatar
  • 493
8 votes
1 answer
749 views

Why don't I get syntax highlighting when I open a file in an autocmd?

While trying to devise a solution to How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?, I have got so far using Python: function LookupFiles ...
muru's user avatar
  • 24.8k
8 votes
1 answer
355 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 ...
Martin Tournoij's user avatar
8 votes
1 answer
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?
dedowsdi's user avatar
  • 6,238
8 votes
2 answers
2k views

vimscript conditional based on grep on current buffer

I'm looking for a way to execute some code within a function conditionally depending on the presence of some text within the edited file. I found a way to do it which works but doesn't feel very "...
Dalker's user avatar
  • 455
8 votes
1 answer
492 views

Display current subroutine/subprogram in statusline

I am currently coding fortran, and I have huge "subprograms" (called "subroutine" in fortran). So I would like the name of the current subroutine (the subprogram my cursor is currently in) to be ...
Feffe's user avatar
  • 1,761
8 votes
4 answers
4k views

How can I get the character at the cursor position in a multibyte aware manner?

Consider the following line: € € This file is encoded in UTF-8 and between the two Euro marks is a Tab character (the Stack Exchange software expands this to spaces). The byte value is: e2 82 ac ...
Martin Tournoij's user avatar
7 votes
1 answer
4k views

vmap and visual block: how do I write a function to operate once for the entire block?

I'm trying to write a simple function that wraps a visual selection of lines with Markdown ``` lines. Here's what I currently have: function! MarkCodeBlock() " Add Markdown code-block delimiters ...
Steve Hollasch's user avatar
7 votes
1 answer
398 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/\(*\+\)\(\[\[\(.*\)\)\...
leeand00's user avatar
  • 3,491
6 votes
2 answers
441 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 ...
lahwran's user avatar
  • 161