62 votes
Accepted

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

Background: I have a plugin for navigating sections in markup documents and wanted to show the section hierarchy in a popup. I distilled what I learned while implementing this down to the following ...
B Layer's user avatar
  • 19.6k
53 votes

How to make a vimscript function with optional arguments?

Late to the party a bit but I didn't see my favorite one: function! FunctionName(arg1,...) let arg2 = get(a:, 1, 0) let arg3 = get(a:, 2, 0) if arg2 "Do stuff with arguments" ...
phicr's user avatar
  • 693
51 votes
Accepted

Use conditional operators AND or OR in an IF statement?

Vimscript use C-like operators && and ||. You can find description of their usage on :h expr2. Some important points mentioned by the doc are the following You'll find that the operators can ...
statox's user avatar
  • 49.4k
44 votes
Accepted

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

You can do this with the system function: let language = system('echo $LANG') Bonus point: if your output is a list, you can use the systemlist instead to get back a list. e.g. let files = ...
nobe4's user avatar
  • 15.9k
42 votes
Accepted

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

As Jorengarenar said in their answer "{{{2 has to do with foldmarkers. Now, what are foldmarkers anyway? What are they useful for? And how do you use them? First let's notice that the strings you ...
statox's user avatar
  • 49.4k
38 votes
Accepted

Vimscript - Surround word under cursor with quotes

You can surround the word currently under the cursor in quotes with the following normal mode commands: ciw""EscP Replace iw with any other Vim motion/text object to surround other things ...
Rich's user avatar
  • 31.3k
35 votes
Accepted

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

The execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do: function! MyFunction(someArg) execute "normal! ". a:someArg. "l" ...
statox's user avatar
  • 49.4k
33 votes
Accepted

What is the difference between single and double quoted strings?

The only difference between single and double quoted string is related to backslash. To display special characters like newline, bells, tabs, etc, you need to use double-quotes -> "\n". Within a ...
Luc Hermitte's user avatar
33 votes
Accepted

How do I check whether a given directory exists?

You’re looking for the isdirectory() function: isdirectory({directory}) *isdirectory()* The result is a Number, which is non-zero when a directory with the name {...
bdesham's user avatar
  • 2,923
31 votes
Accepted

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

You should use getpos(): To save you position in a variable: let save_pos = getpos(".") getpos() takes as argument a mark, here "." represents the current position of your cursor. And to restore ...
statox's user avatar
  • 49.4k
30 votes
Accepted

if else endif oneliner

You need to put another | before else otherwise vim will think your command is set listchars+=space:· else instead of two separate commands. That's why you got the error message: else is an unknown ...
statox's user avatar
  • 49.4k
29 votes
Accepted

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

A "script" does nothing more than run a sequence of ex commands. An "ex command" is what you type when you use : in Vim. For example :wq, :set wrap, :e file, etc. are all ex commands. The : is not ...
Martin Tournoij's user avatar
27 votes
Accepted

Get the current window/buffer/tabpage in Vimscript

Well… it's just as simple in vimscript. Current window Python: current_win = vim.current.window Vimscript: let current_win = winnr() Current buffer Python: current_buff = vim.current.buffer ...
romainl's user avatar
  • 39.3k
27 votes
Accepted

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

That's part of the autoload mechanism. From Autoloading at Learn Vimscript the Hard Way and :help autoload: Autoload lets you delay loading code until it's actually needed, so if the following ...
Jair López's user avatar
  • 1,834
26 votes

How do you find where a variable was last assigned?

If you were looking for the last point where an option was set (instead of a variable), you could use the :verbose command: :4verbose set is? incsearch Last set from ~/.vim/vimrc But ...
mMontu's user avatar
  • 6,600
24 votes

How do I check the version of NeoVim in vimscript?

As mentioned in comments elsewhere, you can use has. For generic "is this neovim": :echo has('nvim') For at least minor version 0.1 (latest as of this writing): :echo has('nvim-0.1') For at least ...
dsummersl's user avatar
  • 351
21 votes
Accepted

How can I source a visual selection of vimscript?

On the command line hit <C-u> then type @* and enter. <C-u> will delete the visual range '<,'> that has been automatically inserted if some text is visually selected. :@* will ...
saginaw's user avatar
  • 6,706
21 votes
Accepted

Includes in vimrc?

Unlike :source, :runtime doesn't need an absolute path to work. It is much better than :source for building portable setups. See :help :runtime. In the following example, we tell Vim to look for a ...
romainl's user avatar
  • 39.3k
21 votes
Accepted

How to iterate over buffers in Vimscript

You can iterate over buffers if you use :bnext and all. I highly advice against this method. It'll trigger autocommands, and you'll have to remember where you were. In other words, it can be damn slow,...
Luc Hermitte's user avatar
20 votes

How can I really have some fun in Vim?

There's a lot of way to have fun in Vim: for example, one could install the vim-script nibble plugin (this one has a dependency which also must be installed). Once the plugin is installed the command ...
statox's user avatar
  • 49.4k
20 votes

Most annoying slow-down of a plain-text editor!

Can somebody tell me how to avoid the very annoying vim's slowdown? Hopefully in a user friendly non-geeky way? I run Vim on a company-provided 2013 15" Retina MacBook with a 2.3 GHz i7 processor, 16 ...
romainl's user avatar
  • 39.3k
20 votes
Accepted

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

Assuming you have Vim 8 you can use :filter :browse filter /pattern/ oldfiles For more help see: :h :browse :h :filter :h oldfiles :h new-items-8
Peter Rincker's user avatar
20 votes
Accepted

vimscript: how to repeat a string N times?

To repeat a string you want to use the (appropriately named) function repeat() The doc is here :h repeat() And you can use it like that: let foo = repeat("abc", 3)
statox's user avatar
  • 49.4k
20 votes
Accepted

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

Your intuition is correct: the two autocommands are very similar. The second autocommand runs when you read or start editing a new file with a name that ends in the .py extension. The first runs when ...
Rich's user avatar
  • 31.3k
20 votes

Vimscript - Surround word under cursor with quotes

Yes, a plug-in for surrounding with quotes exists! vim-surround is what you're looking for. To surround the current word in double quotes, you can use ysiw" once you have the plug-in installed. ys ...
filbranden's user avatar
  • 28.4k
20 votes
Accepted

Select multiple words, one at a time, then replace them all

I’m assuming all the things to change are the same. If not, I would probably proceed with either multiples of the steps below, or combine all the patterns into a single substitute pattern. So, not ...
D. Ben Knoble's user avatar
  • 25.4k
19 votes

How to make a vimscript function with optional arguments?

Since Vim 8.1.1310 Vim also supports real optional function arguments. However, that means that most vim installation don't support this yet. Neovim has that feature since version 0.7.0. Example from :...
radlan's user avatar
  • 677
19 votes

How to grep in ex command output?

The :filter command is a simple, one-line approach to this: :filter /indent/ scriptnames This would show only lines matching the pattern indent from the output of the :scriptnames command. Unusually, ...
ZeroKnight's user avatar
  • 1,071
19 votes
Accepted

How to check if a shell command exists

Incidentally vim has a special function just for this: executable().
Lucas's user avatar
  • 1,599
19 votes
Accepted

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

When you press leader _, you enter command-line mode from visual mode. If you try to enter command-line mode from visual mode manually, you'll see that Vim automatically inserts this range: :'<,'&...
user938271's user avatar
  • 5,867

Only top scored, non community-wiki answers of a minimum length are eligible