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,...
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()
...
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.
11
votes
Accepted
Go to the next word starting with specific letter on current line
You can use getchar() and search() to accomplish your goal.
nnoremap <key> :call search('\<' . nr2char(getchar()), 'W', line('.'))<cr>
The idea is we use nr2char(getchar()) to wait ...
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 ...
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....
9
votes
Accepted
How to wait for user input in the middle of a mapping?
You can use a mapping with the <expr> flag to achieve this. Mappings with the <expr> flag will evaluate the right hand side of the mapping as an expression and then apply the result as ...
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
...
8
votes
Go to the next word starting with specific letter on current line
Edit : Peter Rincker's answer is shorter, easier to explain, and can be repeated as many times as you want. My solution is too long, and can't be repeated for several words. I should delete the answer ...
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> ...
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 ...
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 ...
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(...
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 ...
7
votes
Accepted
Tutorial for vim programming?
I can recommend Learn Vimscript the hard way from Steve Losh. It is a good tutorial which gives you most of the relevant knowledge and links you to :help when it makes sense.
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 '...
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 ...
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.
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 ...
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(...
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 ...
6
votes
Prevent refresh of screen until function completes
I think the problem is not :lazyredraw which, as I understand from docs, should work for functions (see :help :redraw, it says "Useful to update the screen halfway executing a script or function").
...
6
votes
Accepted
if/else condition for matching a regex in a file
You can use the search() function. Something like this should work:
if search('\e\[\d\+;\d\+m', 'nW')>0
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]
...
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.
...
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 ...
6
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}
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$') ==...
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) ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
functions × 281vimscript × 113
key-bindings × 38
vimrc × 30
neovim × 23
autocmd × 12
regular-expression × 11
variables × 11
visual-mode × 10
command × 10
search × 9
buffers × 9
command-line × 8
syntax-highlighting × 6
macro × 6
error × 6
user-commands × 6
normal-mode × 5
external-command × 5
register × 5
quickfix × 5
range × 5
gvim × 4
cut-copy-paste × 4
substitute × 4