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
21 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,...
user avatar
  • 12.6k
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() ...
user avatar
  • 25.8k
12 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.
user avatar
  • 161
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 ...
user avatar
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 ...
user avatar
9 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 ...
user avatar
  • 16.7k
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 ...
user avatar
  • 16.1k
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 ...
user avatar
  • 6,426
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 ...
user avatar
  • 16.1k
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 ...
user avatar
  • 16.7k
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 ...
user avatar
  • 901
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.
user avatar
  • 1,989
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 '...
user avatar
  • 3,869
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 ...
user avatar
  • 23.8k
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.
user avatar
  • 12.6k
7 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....
user avatar
  • 1,357
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 ...
user avatar
  • 12.6k
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
user avatar
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 ...
user avatar
  • 46.3k
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] ...
user avatar
  • 6,360
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. ...
user avatar
  • 6,360
6 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> ...
user avatar
  • 7,426
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}
user avatar
  • 27.5k
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$') ==...
user avatar
  • 27.5k
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) ...
user avatar
  • 46.3k
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" ...
user avatar
  • 18.3k
6 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(...
user avatar
  • 46.3k
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 ...
user avatar
  • 16.7k

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