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
48
votes
5answers
15k 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 ...
38
votes
5answers
10k views

How to make a vimscript function with optional arguments?

I want to make a function that has optional arguments. In python I would do something like this: def functionName(arg1,arg2=false,arg3=false): if arg2: #Do stuff with arguments else: ...
35
votes
4answers
7k 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 ...
35
votes
1answer
17k views

Use conditional operators AND or OR in an IF statement?

This is probably an incredibly simple question, but I did not find any answer so far (I must lack the right sources, and I don't know where to search in vim's help). I have a condition and I would ...
34
votes
2answers
9k views

What's the difference between let and set?

I don't know much of the theory behind Vim. It looks as if we set some variables, and let other variables. What's the difference, and when is one required over the other? Also, the solarized vim ...
34
votes
2answers
13k views

How do I get the name and extension of the current file?

Is there a way to get the name and extension of a file using vimscript? If so I would like the name and extension separately.
32
votes
1answer
15k 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. ...
28
votes
2answers
9k 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 ==...
27
votes
6answers
5k 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 ...
27
votes
1answer
3k 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 ...
20
votes
1answer
19k views

Why am I getting a “E488: Trailing characters” error on this custom command?

I have a ~/.vimrc that contains just this: function! NewFile() let filename = input("Filename:") endfunction command NewFile :call NewFile()<cr> (of course my real .vimrc is more ...
20
votes
2answers
1k 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 ...
19
votes
2answers
2k 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 ...
19
votes
1answer
2k 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 . '!'
18
votes
5answers
1k views

After copying a visual selection to keyboard, return to original location

My cursor is in the middle of a paragraph when I decide suddenly I need to copy that paragraph: So I type vip to select the paragraph visually: Now I hit cmd+C to copy to clipboard (this may vary on ...
18
votes
2answers
5k views

How do I check the value of a Vim option in Vimscript?

In Vimscript, how can I check the current value of a Vim option? To elaborate, I want to make a command to switch from Full-GUI Mode ( guioptions=+tM) to Hacker Mode (guioptions=-tM). However, I can'...
17
votes
4answers
1k views

Merge blocks by interleaving lines

Is there a dedicated way to merge two blocks of text by interleaving lines, like passing from this: a1 a2 a3 a4 b1 b2 b3 b4 to that: a1 b1 a2 b2 a3 b3 a4 b4 in a few commands? ...
16
votes
3answers
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?
16
votes
4answers
691 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, ...
16
votes
2answers
875 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,...
16
votes
1answer
2k views

Is it possible to get the matched string after calling :search()

In a vimscript-function, I need to assign a matched string to a variable l:matched after calling search() and I am wondering if there is a shorter way than what I am currently doing: let l:pattern =...
15
votes
2answers
4k views

What does three commented curly brackets ("{{{) at the end of a line mean? Is it just a comment?

In the tcomment plugin, specifically in: ~/.vim/pack/tml/start/tcomment.vim/autoload/tcomment.vim and ~/.vim/pack/tml/start/tcomment.vim/plugin/tcomment.vim I'm seeing this at the end of some lines: ...
15
votes
2answers
4k 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 ...
15
votes
2answers
10k 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 ...
15
votes
2answers
8k views

How do I insert a comment into my vimrc fie?

What is the syntax for inserting a comment or comments into the vimrc file? Having trouble finding this on vim documentation online.
15
votes
2answers
2k views

Is cpo&vim a special syntax?

In scripts it is customary to do something like: let s:save_cpo = &cpo set cpo&vim ... script ... let &cpo = s:save_cpo To ensure nocompatible mode for the script. Is: set cpo&...
15
votes
2answers
411 views

Does Vimscript have an official name?

The name of the language sometimes known as Vimscript is a topic of debate. Some places use Viml (or is it VimL?), others Vimscript. Does it have an official name, and if it does, what is it and since ...
14
votes
1answer
2k views

How to check whether Vim is built as a 32-bit or 64-bit application?

How do I check if Vim is compiled as 64-bit or 32-bit from within vimscript? Specifically on Windows.
14
votes
2answers
257 views

Automatically add a closing " or ' when inserting one, but only if the cursor isn't inside a string already

I've defined mappings in my vimrc to automatically enter close parentheses and braces when I enter opening ones with inoremap ( ()<esc>i. I'd like to do the same for " and ', but only when the ...
13
votes
4answers
2k views

How to reverse every 4 lines?

First of all, with this being my first post here, I'd just like to say that I've found VIM to be a great tool and the forum here to be very helpful in finding answers to questions, with lots of ...
13
votes
4answers
3k views

Includes in vimrc?

Is it possible to include external files to be sourced in the vimrc file? Is it as simple a matter as just adding source somefile?
13
votes
3answers
7k views

Substitute with pure vimscript (without `:s`)

I have the following in my vimrc: func! AddSpaceBeforeEqual() s/\([a-z)_0-9"'\[\]]\)=/\1 =/ge endfunc I'm using vint to lint my vimrc, and got the following warning : ...
13
votes
2answers
2k views

Is it possible to use a delegate or to pass a function as argument in Vimscript?

I am trying to create a small plugin to learn vimscript, my goal is to create some functions processing a selected text and replacing it with the result. The script contains the following items: Two ...
13
votes
1answer
2k views

What is the meaining of '#' symbol in function name?

I would like to know what is the meaning of the # symbol in function names e.g.: execute pathogen#infect()
13
votes
2answers
2k views

How to get the current byte offset in whole file

I saw that you could display the current byte offset in the statusline using %o, but I found no function or command which does the same. Is there a way of getting the current byte offset ...
12
votes
2answers
2k views

How do you find where a variable was last assigned?

I am trying to set g:netrw_list_hide and it doesn't seem to work. No matter what I set it to in my .vimrc it always has the value ,^\.\.\=/\=$. Only if I source my vimrc file again, then it holds the ...
12
votes
2answers
2k views

What to follow to create a vim plugin?

So my question is simple. Is it a good idea to learn vim scripting just to create vim or we should adopt some other more accepted languages to that. For example This link opens a YouTube videos which ...
12
votes
1answer
5k views

if else endif oneliner

I have this oneliner in my vimrc but it does not work : if has("patch-7.4.710") | set listchars+=space:· else | set listchars+=trail:· | endif VIM says : E518: Unknown option: else Can you help me ...
12
votes
1answer
5k views

Get the current window/buffer/tabpage in Vimscript

In python, when using the vim module, it is incredibly simple to get the current window/buffer/tabpage: current_win = vim.current.window current_buff = vim.current.buffer current_tabpage = vim....
12
votes
3answers
3k views

How do I reverse a string with Vim script

I am trying to reverse a string, so that 'abc defg hij' becomes 'jih gfed cba'. The solution I came up with is join(reverse(split(l:text, '.\zs')), '') Is there a more elegant solution or should I ...
12
votes
4answers
995 views

Command to loop through spelling suggestions

I mapped zz to 1z=, which is great most of the time, but every now and then the first suggestion isn't the right one. So I'd like to keep repeating zz (or .) to cycle through the other suggestions. ...
12
votes
3answers
3k views

How can I source a visual selection of vimscript?

If I have a couple of lines of vimscript that I want to test before putting in my vimrc, is it possible to visually select, then source them? I have tried make visual selection v, then motions to ...
12
votes
2answers
1k views

Do VimScript functions have to start with a capital letter and if so, why?

Is there a good reason why VimScript functions that I put in my .vimrc or _vimrc have to begin with a capital letter?
11
votes
3answers
1k 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 ...
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

How to deal with string containing spaces in Vim script?

I have the following command in my vimrc: execute "set path+=".getcwd()."/**" It works well if current working directory is like /home/tamlok/my_work/. However, if the directory contains spaces, ...
11
votes
1answer
932 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
1answer
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': '...
11
votes
2answers
432 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
2answers
3k 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 ...

1
2 3 4 5
22