Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
69 votes
Accepted

Detect OS in Vimscript

The best way is to use has(), with this function you can check for features of Vim; OS specific features from :help feature-list: macunix Macintosh version of Vim, using Unix files (...
user avatar
52 votes
Accepted

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

Background: I was playing around with Vim 8.2's popups recently (I have a small plugin that allows navigation of sections in a markup document and I was looking at showing section hierarchies in ...
user avatar
  • 18.1k
51 votes
Accepted

How to make a vimscript function with optional arguments?

Yes, you can take optional arguments in a function. It is not as convenient as python's way of doing it, but this is how you do it: function FooBar(...) " This is like *args in python echom a:0 "...
user avatar
50 votes
Accepted

Call a Vim function silently

You can call the function silently by defining a silent map: noremap <silent> H :call FirstCharOrFirstCol()<cr> For more info, see :h :map-<silent>. Note in particular that this ...
user avatar
48 votes
Accepted

What's the difference between let and set?

let assigns a value to a variable, and set assigns a value to one of Vim's internal options. For example, you would use :let mystring='Hello!' to declare a new variable, but you would use :set ...
user avatar
  • 13.6k
48 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 ...
user avatar
  • 45.9k
47 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" ...
user avatar
  • 633
41 votes

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

From :he filename-modifiers: :t Tail of the file name (last component of the name). Must precede any :r or :e. :r Root of the file name (the last extension removed). ...
user avatar
  • 23.7k
41 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 = ...
user avatar
  • 15k
41 votes

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 ...
user avatar
  • 45.9k
39 votes

Detect OS in Vimscript

has() sounds like a good idea until you try it on Mac OS X: in the default /usr/bin/vim, has('unix') is true but both has('macunix') and has('mac') are false while, in the regular MacVim download, all ...
user avatar
  • 32.6k
33 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" ...
user avatar
  • 45.9k
29 votes
Accepted

How do you “chomp” a string in Vim?

You can use substitute(), or define a function: function! Chomp(string) return substitute(a:string, '\n\+$', '', '') endfunction This variant will call system for you and then chomp the result: ...
user avatar
  • 16.6k
29 votes
Accepted

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

Remove the trailing <cr> That is only needed for mappings, but not for commands.
user avatar
29 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 ...
user avatar
28 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 ...
user avatar
28 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 ...
user avatar
  • 45.9k
27 votes

How to make a vimscript function with optional arguments?

This pattern will let you assign meaningful names to each argument, and provide a default value for any arguments that were not provided: function FunctionName(foo, ...) let bar = a:0 >= 1 ? a:1 ...
user avatar
  • 3,392
27 votes

How do you “chomp” a string in Vim?

let @z = systemlist('date')[0] removes the newline for you.
user avatar
  • 32.6k
25 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 ...
user avatar
  • 45.9k
22 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 ...
user avatar
  • 27.4k
21 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 ...
user avatar
  • 6,370
21 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 ...
user avatar
  • 1,714
20 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 ...
user avatar
  • 32.6k
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 ...
user avatar
  • 45.9k
20 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 ...
user avatar
  • 32.6k
20 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 ...
user avatar
  • 311
20 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,...
user avatar
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 ...
user avatar
  • 32.6k
20 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 {...
user avatar
  • 2,433

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