Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
26 votes
Accepted

Remove new line from result of system()

If you don't want to use systemlist for whatever reason, you can explicitly remove the newline and/or whitespace. Since vim 8.0.1630 (very recent), there is a trim() function which removes whitespace,...
Mass's user avatar
  • 14.1k
15 votes
Accepted

Detecting whether vim supports functions

The problem is that "tiny" is missing the +eval feature and that's what prevents it from seeing your function definitions. So you could try something like: if has('eval') function! Greet() ...
filbranden's user avatar
  • 28.9k
13 votes
Accepted

How to test if a string is a vim command?

I you only need to know if it is a built-in command, you can use exists(':yourstring') See :h exists() for more details.
Bart's user avatar
  • 171
10 votes
Accepted

Use variable for mapping inside vimrc

The :map command takes :help key-notation, but not variables. Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to ...
Ingo Karkat's user avatar
  • 17.9k
10 votes
Accepted

What is the purpose of user `command` in Vi/Vim?

Let's get this confusion out of the way first: Are these ex commands or Vimscript? Vim scripts are made of ex commands. From :help script: Your first experience with Vim scripts is the vimrc file....
8bittree's user avatar
  • 1,536
9 votes
Accepted

VimL/Vimscript: how to access local variable of outer function from its inner function?

I though it wasn't possible, but I was wrong. See :h :func-closure You need to patch two things: add closure at the end of the inner of the function declaration respect the usual naming conventions ...
Luc Hermitte's user avatar
  • 17.4k
8 votes
Accepted

Change command depending on file extension/type

Sure, you can use the FileType autocmd. " Set the default behavior nnoremap <Leader>T :!vendor/bin/phpunit --filter " Now set it if we open a go file autocmd FileType go nnoremap<buffer> ...
Tumbler41's user avatar
  • 7,756
8 votes
Accepted

When should a function *not* be defined with "abort"

Discl.: It's just guessing on my part. abort didn't exist when function was introduced. I guess that in order to not break existing code it was decided to not change the current behaviour and to ...
Luc Hermitte's user avatar
  • 17.4k
8 votes
Accepted

Are script local functions (s:funcName()) unit testable?

TL;DR: Yes, but you probably shouldn't (in general) Other answers Christian's answer offers two approaches that modify myplugin.vim in order to expose the script-local function (either as a Funcref ...
Ingo Karkat's user avatar
  • 17.9k
8 votes

How to ask vim where function or command was defined?

From :h :function-verbose: When 'verbose' is non-zero, listing a function will also display where it was last defined. Example: > :verbose function SetFileTypeSH function SetFileTypeSH(...
statox's user avatar
  • 50k
7 votes
Accepted

Function to refactor indents and tabs

The problem is that gg, = and G are normal mode commands, as opposed to ex commands which are used within a script. Writing normal gg=G should solve your problem. As @Carpetsmoker has pointed out in ...
Ingo's user avatar
  • 986
7 votes
Accepted

Strange behavior with "map(range ...)"

Looking at :h map: map({expr1}, {expr2}) map() {expr1} must be a List or a Dictionary. Replace each item in {expr1} with the result ...
muru's user avatar
  • 24.9k
7 votes
Accepted

Print an error message without "Error detected while processing function"

The solution is described in the manual: If you just want a highlighted message use :echohl. And to get a beep: :exe "normal \<Esc>" Thus: function! PrintError(msg) abort execute '...
Sato Katsura's user avatar
  • 4,009
7 votes

How do you undefine existing functions and key maps?

To remove a command-line mapping, use: :cunmap {lhs} Similar commands exist for the other mapping modes. See :help :unmap To remove a function, use: :delfunction {name}
Rich's user avatar
  • 32.1k
7 votes
Accepted

:exe moves cursor to first line of buffer

In vim, a function which does not explicitly return a value actually returns 0. So, your code is equivalent to exe 0, which is like :0 meaning move the cursor to the first line.
Mass's user avatar
  • 14.1k
7 votes

Yank both sides of equation

This can be done with either regex+substitute or macros Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred ...
Mass's user avatar
  • 14.1k
7 votes
Accepted

How to check whether a command is run with range or not?

A range with commands is always about lines. It seems you want to work with line/column positions. Whether a range was given can be checked by using <range>: command! -range Test call TestFunc(...
Ralf's user avatar
  • 9,237
6 votes
Accepted

How to organize autoload functions?

Especially with autoload, there should be no same-name clashes. The first part of the autoload function is determined (and enforced) to be the autoload script name, and you're free to choose any ...
Ingo Karkat's user avatar
  • 17.9k
6 votes
Accepted

Command to loop through spelling suggestions

Here's what I came up with: Spell Rotate Features The '[ and '] marks are used to keep track of the text being worked on. Making a change elsewhere will effectively "accept" the suggested change. ...
Tommy A's user avatar
  • 6,780
6 votes
Accepted

Use motion in normal mapping calling a function

You'll nee to use the g@ feature (see :h g@) This operator allow to specify a motion after a function call and position the marks '[ and '] at the beginning and the end of the text selected by the ...
statox's user avatar
  • 50k
6 votes
Accepted

Way to get content of Visual Selection?

If you really want to avoid the registers, you can use the visual marks to figure out byte positions of the selected text: function! s:pos2byte(pos) abort return line2byte(a:pos[1]) + a:pos[2] ...
Tommy A's user avatar
  • 6,780
6 votes
Accepted

Count number of visible lines in document with folding?

Nice idea! Here's a basic function that achieves your goal using a different technique: function! FitOnPage() normal! zR while &foldlevel > 0 if line('w0') == 1 && line('w$') ==...
Rich's user avatar
  • 32.1k
6 votes

Remove new line from result of system()

You probably want to use :h systemlist(): systemlist({expr} [, {input}]) *systemlist()* Same as system(), but returns a List with lines (parts of output separated by NL) ...
statox's user avatar
  • 50k
6 votes
Accepted

function to search for a pattern

To create a custom command line command :command is a good choice: :command! -nargs=1 SL g/<args>/z#.1 You'll need to use a name that starts with a capital letter, though, so I'm using "SL" ...
B Layer's user avatar
  • 19.9k
6 votes
Accepted

Make mapping that accepts count independent of line count

The answer is actually not as complicated as you might think. A trick that I have found very useful in situations similar to this is to remove the <cr> from the end of your mapping and see what ...
DJMcMayhem's user avatar
  • 17.6k
6 votes
Accepted

How can I run a function / action every period of time (once an hour)?

You can use timer_start() to set "timers" to run a function every n milliseconds. For example: fun! s:set_bg(timer_id) let &background = (strftime('%H') < 12 ? 'light' : 'dark') endfun ...
Martin Tournoij's user avatar
6 votes
Accepted

How to list / show all built-in functions?

Try this: new | put! =getcompletion('*', 'function')->filter({_, v -> v =~# '^[a-z][^#]*$'}) Broken down: new - Create a new window and start editing an empty file in it put! - Put the text ...
user938271's user avatar
  • 5,987
6 votes
Accepted

What is the most idiomatic way to achieve filetype-specific functions?

Function defined in ftplugins In ftplugins we can define script local functions and global functions that are defined only when a buffer of the given filtetype is loaded. In the second case, the ...
Luc Hermitte's user avatar
  • 17.4k
6 votes

What's the difference between command and function?

Think it as "shell scripting": "Command" is a word that stands at the start of command-line. What follows a command is either "string parameter(s)" or "expression(s)&...
Matt's user avatar
  • 20.8k
6 votes
Accepted

How can I expand a:000 list in order to pass through all varargs parameters to another function?

Use :h call(): func! Bar(...) abort return a:000 endfunc func! Foo(...) abort echo call('Bar', a:000) endfunc call Foo('asdf', 'qwer') " --> ['asdf', 'qwer']
Maxim Kim's user avatar
  • 13.5k

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