Skip to main content

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
119 votes
9 answers
144k 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,848
75 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,037
51 votes
6 answers
17k 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: ...
iProgram's user avatar
  • 1,599
49 votes
1 answer
31k 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. ...
Jezen Thomas's user avatar
48 votes
1 answer
29k 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 ...
Feffe's user avatar
  • 1,771
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
  • 3,003
42 votes
3 answers
23k 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.
iProgram's user avatar
  • 1,599
40 votes
1 answer
14k 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
  • 20k
40 votes
2 answers
15k 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,771
39 votes
2 answers
15k 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 ...
Anthony's user avatar
  • 501
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
  • 50.2k
27 votes
6 answers
3k views

After copying a visual selection, 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 ...
Jonah's user avatar
  • 753
27 votes
8 answers
5k 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 ...
the_velour_fog's user avatar
26 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,711
25 votes
1 answer
10k 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(...
bdesham's user avatar
  • 3,003
25 votes
1 answer
30k 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 ...
Andrew Ferrier's user avatar
25 votes
2 answers
9k 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'...
Stolas's user avatar
  • 415
24 votes
3 answers
16k 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
23 votes
6 answers
25k 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
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,771
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
22 votes
1 answer
11k views

if else endif oneliner in VIM

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 ...
SebMa's user avatar
  • 3,158
22 votes
5 answers
2k 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? ...
iago-lito's user avatar
  • 1,122
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
21 votes
3 answers
21k views

How do I check the version of NeoVim in vimscript?

I can check vim's version by using v:version, and I can check if I am running in nvim by doing has('nvim'). Is there a variable like v:version in neovim to let me know the nvim version, like 0.1.4 ...
skywind3000's user avatar
21 votes
1 answer
4k 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()
Alex's user avatar
  • 347
20 votes
1 answer
10k 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....
naiveai's user avatar
  • 365
20 votes
2 answers
19k 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.
Aaron Thomas's user avatar
19 votes
4 answers
9k 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?
leeand00's user avatar
  • 3,621
19 votes
3 answers
13k 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 : ...
edi9999's user avatar
  • 1,222
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
19 votes
2 answers
14k views

What is the difference of using `au BufNewFile,BufRead *.py` and `au Filetype python` in .vimrc?

As I am setting .vimrc, I found these two code blocks have the same functionality. au Filetype python set \ tabstop=4 \ softtabstop=4 \ shiftwidth=4 \ textwidth=79 and au ...
Songcheng Li's user avatar
19 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
  • 435
18 votes
2 answers
5k 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 ...
Cezar's user avatar
  • 283
18 votes
2 answers
3k 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&...
Runium's user avatar
  • 1,247
18 votes
1 answer
3k 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 = ...
René Nyffenegger's user avatar
17 votes
4 answers
31k 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: ...
Sam's user avatar
  • 301
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
17 votes
2 answers
8k 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, ...
tamlok's user avatar
  • 851
16 votes
1 answer
5k 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: ...
Elliptical view's user avatar
16 votes
3 answers
11k 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: ...
Cookie Thumper's user avatar
16 votes
1 answer
3k 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
16 votes
2 answers
504 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 ...
muru's user avatar
  • 25k
15 votes
3 answers
3k 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
  • 325
15 votes
3 answers
7k 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 ...
René Nyffenegger's user avatar
15 votes
2 answers
3k 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 ...
statox's user avatar
  • 50.2k
15 votes
1 answer
3k 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.
orlp's user avatar
  • 320
14 votes
2 answers
977 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>...
DJMcMayhem's user avatar
  • 17.7k
14 votes
1 answer
4k 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
  • 442

1
2 3 4 5
32