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 ...
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"
...
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 ...
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 = ...
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 ...
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 ...
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"
...
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 ...
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 {...
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 ...
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 ...
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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 ...
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 ...
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
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)
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 ...
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 ...
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 ...
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 :...
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, ...
19
votes
Accepted
How to check if a shell command exists
Incidentally vim has a special function just for this: executable().
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:
:'<,'&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
vimscript × 1552vimrc × 257
key-bindings × 132
neovim × 125
functions × 113
regular-expression × 56
autocmd × 53
syntax-highlighting × 48
buffers × 40
search × 38
gvim × 37
command-line × 36
variables × 35
visual-mode × 32
vim-windows × 31
plugin-system × 26
external-command × 25
statusline × 24
autocompletion × 21
cursor-movement × 21
register × 21
lua × 21
terminal × 20
vimscript-python × 20
highlight × 19