Questions tagged [vimscript]

A scripting language embedded into Vim. It can be used to customize Vim to suit your needs and to create plugins. Also called VimL.

Filter by
Sorted by
Tagged with
7
votes
1answer
1k views

Is there a way to get the name of the current function in vim-script?

Is it possible to programmatically let a function knows its own name? What I am looking for is something which would allow me to write this code (or something similar): function! myUtils#TestMe() ...
7
votes
1answer
366 views

How to test if certain event is supported?

I want to test in a script if this version of vim supports the "CmdlineEnter" event. This is a relatively new addition in vim and neovim doesn't support it in it's current 0.2.2 version (although ...
7
votes
1answer
984 views

How does one include a trailing space in a mapping?

I'm currently trying to create the following mapping in my vimrc: nnoremap <Leader>s :split The :split is followed by a trailing space. This makes sense so I can enter the file name without ...
7
votes
2answers
2k views

Why do functions in Vimscript require a “call” statement?

In Vimscript, if you use a function as an R-value, then the function simply evaluates. For example: let count_pattern += str2nr( strpart( execute( command ), 1 ) ) However, if a function is used by ...
7
votes
1answer
247 views

What is a ^@ in vim?

I'm running a Vimscript that retrieves the result of a message and redirects it to a vimscript variable. When the contents of the vimscript variable are displayed, it has a ^@ in it. What does that ...
7
votes
1answer
586 views

Adding text to the left margin

Is there an easy way to add text to the left margin of a vim document (to the left of the line numbers)? I know that it is possible to do because plugins such as vim-signature do it, but I am curious ...
7
votes
1answer
3k views

How do I alphabetically compare two strings

Is there a standard way to compare two strings in vim so that I can quickly determine which string is sorted before the other. Something like strcmp(str1, str2) to return 0 if str1 == str2, 1 if ...
7
votes
2answers
1k views

vimscript conditional based on grep on current buffer

I'm looking for a way to execute some code within a function conditionally depending on the presence of some text within the edited file. I found a way to do it which works but doesn't feel very "...
7
votes
1answer
517 views

Is it possible to execute a :normal command ignoring user-defined maps?

Let us suppose that a user maps - to <nop>. How can I, as a script writer, execute a :normal command that correctly uses the default behaviour of -?
7
votes
1answer
779 views

How to convert hex to binary in Text mode?

I'm trying to convert hex values to binary. For example, I have got some text file containing: 0.0 010111010 B4 0.1 001001011 A3 ... And I'm trying to convert B4 to 10110100, and A3 too. But I ...
7
votes
1answer
1k views

Is it possible to add inline comments in a multiline statement?

I have a function that looks like fu! MultiLineTest(u, v) if a:u != 2 && \ a:v != 2 return 'yes' else return 'no' end endfu Of course, the actual ...
7
votes
1answer
1k views

Writing modified buffer to a temporary file

I'd like to write the modified buffer I'm currently editing to a temporary file periodically. In the background I could then, for example, use external tools to analyze the file. I don't want to ...
7
votes
1answer
630 views

Can I pass a custom string to the 'gx' command?

I have a file like this: vi.stackexchange.com example.com duckduckgo.com I want to be able to open these sites with gx; this doesn't work because there's no protocol (http://) in front of these ...
7
votes
2answers
2k views

Jumping to a file in vimscript

Using gF I can jump to a file with the format foobar.py:27:6 to open foobar.py on line 27. My question is how I can make this jump in vimscript? For example, this won't work: execute "normal gF " . ...
7
votes
1answer
404 views

Display current subroutine/subprogram in statusline

I am currently coding fortran, and I have huge "subprograms" (called "subroutine" in fortran). So I would like the name of the current subroutine (the subprogram my cursor is currently in) to be ...
7
votes
1answer
574 views

When is it recommended to use the scope s: and the argument abort to define a function?

When defining a function, is it always recommended to use the scope s: to make it local to the script and avoid overwriting a function with the same name in the global namespace ? And is it always ...
7
votes
1answer
187 views

Is it possible to make a numerically-prefixed hotkey run a function that many times?

I find that if you enter a numeric prefix prior to executing an ex command, the convention that it applies is to set up the command to prep an operation across a line range. It is explained in the ...
7
votes
1answer
365 views

Make zG and zW persistent

Edit: I turned this into a little plugin. This is my first attempt at writing one and I have no idea what I'm doing, so any help is much appreciated. :) Here it is: https://github.com/danielbmarques/...
6
votes
2answers
1k views

Why can't I write to a file in the home directory?

I want to write to a file in the home directory, so I tried the following command: :call writefile(["foo"], "~/event.log", "a") But I got an error: E482: Can't create ...
6
votes
3answers
435 views

Vimscript file rewrites itself and complains of a mismatched endfor

I want to remap <C-i>*character* to i*character*<Esc> for easy inserting of single characters. This is what I've written: let printable_characters = [ '!', '"', '#', '$', '%', '&', '''...
6
votes
3answers
5k views

Read file from vimscript variable

I can run this snippit of code within a vimscript block: 0read /home/user/.vim/bundle/vim-lorum/lorums/lorum.js As expected, it puts the contents of "lorum.js" into the current buffer But, I need ...
6
votes
2answers
2k views

How do I conditionally set :colorscheme?

My .vimrc chooses my preferred color scheme (solarized) which is installed via plugin management system. Yet on some systems which don't have that plugin the :colorscheme solarized command runs but ...
6
votes
2answers
1k views

How do I check if Vim is currently recording a macro?

Is there a way to programmatically check if Vim is currently in the recording mode triggered by q?
6
votes
2answers
1k views

Why is `let foo = $HOME/bar` invalid?

Why is let foo = $HOME valid and let foo = $HOME/bar invalid in vimscript? I have tried let foo = "$HOME/bar" but it is the equivalent of foo = '$HOME/bar' in shell script, $HOME is not ...
6
votes
4answers
2k views

How can I get the character at the cursor position in a multibyte aware manner?

Consider the following line: € € This file is encoded in UTF-8 and between the two Euro marks is a Tab character (the Stack Exchange software expands this to spaces). The byte value is: e2 82 ac ...
6
votes
2answers
1k views

Read values from a highlight group

I can read a value from an option like: let b = &background Is there a way to read a value from a highlight group? Something like this pseudo-code: let b = &StatusLine['ctermbg'] ...
6
votes
1answer
2k views

How to check with Vimscript whether python support is enabled?

I think it would be very nice if I can check whether current vim was compiled with python support, instead of having error message popping out every time vim is launched. Can this check be done in ...
6
votes
2answers
2k views

How do I search for a line that contains a pattern at column n

In writing some Vimscript to try to answer this question, I was exploring ways to "search for a line with pattern P in position X" (P is a search pattern, X is a number). One way to do this would be ...
6
votes
3answers
2k views

Sending command to different tmux pane from vim

behave test language need to execute different lines of the behave feature file, for example, if I want to test the line number 99, I'll need to input the following line in the terminal behave ...
6
votes
2answers
495 views

Keep windows reference in memory

I am writing a plugin which needs to have two windows displayed at the same time: _______ | | | | A | B | |___|___| I need to keep in memory a reference to those windows as I execute my plugin. ...
6
votes
2answers
3k views

Append to global variable and completion

I would like to create a global (configuration) variable that ideally would work as a comma separated list but that we could also append to it. The idea is to have the same behavior as when using += ...
6
votes
1answer
595 views

Can I do array destructuring in VimL?

Can I do this in Vimscript? let [a, b] = s:MyFunc() function! s:MyFunc() return [1, 2] endfunction
6
votes
1answer
5k views

Escape quotes within quotes in command

I'm trying to run a command (from within a function) that goes somewhat like this: execute a:firstline . ',' . a:last line . 'g/\(\.\|!\|?\)\()\|]\|"\|'\)*\($\|\s\)/execute "normal! v(:call ...
6
votes
1answer
2k views

How to I get around “normal” exiting visual mode?

I'm trying to write a vimscript function that moves the cursor all the way up or all the way down without changing the column. (similar to the way :set nostartofline works). It works, but when I ...
6
votes
2answers
22k views

Fixing a “not an editor command” error

I've toying with someone else's .vimrc file. I'm getting an error on a function call within the .vimrc file. Error detected while processing <FILE>: line 1500: E492: Not an editor command: ...
6
votes
1answer
1k views

How to cut/trim line in statusline?

I'm trying to cut string returned from fugitive plugin. It returns branch name of current file. By default returned string is Git(branchname) and this is why I want to get rid of odd Git(..) wrapper. ...
6
votes
1answer
140 views

Delete to ; if exists, otherwise to end of line

autocmd FileType cpp nnoremap <buffer> C ct; autocmd FileType cpp nnoremap <buffer> D dt; These two are quite useful, but sometimes there is no ; after the cursor and then D doesn't do ...
6
votes
1answer
1k views

How do I use a variable with an ex command?

I wrote a moderately complex routine for Vim and I need to programmatically set the height of windows. The way to do this is with :resize [N] as the help says. But my invocation didn't work until ...
6
votes
2answers
2k views

How to programmatically visually select a text block?

Is there a way to select a text block (e.g. a first word "TODO:") programmatically (with some commands or functions from a script)? Not simply highlight, like with a search, but visually select like ...
6
votes
2answers
547 views

How can I watch variables when debugging Vimscript?

I would like to have another pane active when debugging a Vimscript function which would show the values of chosen local variables and function arguments dynamically. Currently I have to echo or let ...
6
votes
1answer
400 views

How to use the argument of a vimscript function in an inner python function?

I have the following vimscript: it contains a vimscript function which executes a python code thanks to python << EOF: function! Test(myArgument) python << EOF def test(myArgument): ...
6
votes
2answers
750 views

Auto sorting of lists of include files

Many sections of stuff we right is such that it could be automatically formatted by user defined rules. Here are a couple of examples: List of include files in languages such as C. It is customary to ...
6
votes
1answer
3k views

Copying a String to a Variable or a Register from Within a Function

I want to write a function which copies, say, the last word of the next line which contains keyword into a variable say var or any named register say l. I did something inelegant: function! ...
6
votes
2answers
148 views

Convert absolute path to one relative to another absolute one

Context I’m working on an 'omnifunc' function for Markdown editing. It’s supposed to help me link to other Markdown files while I’m typing a link – based on file content though, not file name. Suppose ...
6
votes
1answer
79 views

When is a function or special character automatically evaluated / expanded and why?

Let's say I've got the following function : function! SomeFunction() return "/tmp/foo" endfunction If I type : :echo SomeFunction() The function is evaluated and the output in vim is the ...
6
votes
1answer
605 views

Why don't I get syntax highlighting when I open a file in an autocmd?

While trying to devise a solution to How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?, I have got so far using Python: function LookupFiles ...
6
votes
1answer
3k views

How do I convert a String to a Number, catching invalid strings?

I am trying to convert a String '1' to a Number (int) 1 in a vimL script. I have used the built-in command str2nr() so far, however I was not able to write this in an elegant and effective way. ...
6
votes
1answer
397 views

Fixing lag due to vim-troll-stopper plugin

I've recently installed a plugin called vim-troll-stopper. Some unicode characters are drawn exactly the same but have different meanings. I think they are called homoglyphs. Some programmers may use ...
6
votes
1answer
882 views

How to inspect the cursor position in Vim command line (Cmdline mode)?

How to inspect the cursor position in Vim command line (Cmdline mode) from Vim script? I would like to configure my Command-line mode key bindings in Vim to behave like in tcsh. In particular, i ...
6
votes
1answer
220 views

How to move the cursor to the next reference, option, heading in man pages?

If we load the built-in man.vim filetype plugin by writing in our vimrc: runtime ftplugin/man.vim We can read a man page inside a Vim buffer, using the :Man command described in :h :Man: MAN ...

1 2
3
4 5
25