Questions tagged [functions]

Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.

Filter by
Sorted by
Tagged with
2
votes
1answer
66 views

Use motion in normal mapping calling a function

I have this function which displays line numbers. I want it to be called in different ways: from a command, specifying the lines as arguments with a visual selection, printing the first and last line ...
2
votes
1answer
482 views

Carriage return as argument breaks the function

I have a remap that calls a function with a key combination as an argument. In this function I want to execute another remap to yet another function. But whatever I do, it complains about a missing ...
2
votes
1answer
59 views

Can't seem to get syntax information when in insert mode

This is for an indent function that should maintain the indentation should the cursor happen to be in a string region. It maintains the indentation when I hit == in normal mode but changes when I open ...
2
votes
1answer
246 views

vim argument substitution inside a function

I would like to use the arguments passed to a function, as an argument to replace a search. Something like this: function! Myfunction(...) %s/hello/ goodBye a:1, a:2/g endfunction Does anyone ...
2
votes
1answer
243 views

vimscript: strange behavior of search() with flags 'be'?

vim-help :h search() says it start search at column 0 of cursor position. Search for regexp pattern {pattern}. The search starts at the cursor position (you can use |cursor()| to set it). .... ...
2
votes
2answers
108 views

Running python code in a function

I'm having problems with latex-suite (some research indicates that many people are). The issue at hand is that they have a nifty python script that gives you a nice outline of your document and ...
2
votes
1answer
632 views

Function does not work when called as from a mapping

I have a function that depends on the character under the cursor : if it's a ), then the function does nothing ; if it's not, the function adds a ). function! Ket() let char = getline(".")[col('.')...
2
votes
1answer
46 views

Remapping `v` in netrw/calling NetrwSplit(5) directly

So I've created some helper functions to help work around netrw. They are some keybind simplifications that help with things like window/cursor management when opening files, where to open them, ...
2
votes
1answer
509 views

Get column of last character in visual line - Vimscript

In one of my vim scripts I require the column of the last non-whitespace character of the current line and the column of the last character of the current line (when the current line is wrapped into ...
2
votes
1answer
31 views

How can I fold some lines on opening a buffer?

I want to fold from "Copyright" to "CONNECTION" every time I open a .c or .h file, so I don't have to see it. The license does have newlines in it in my files, so selecting shouldn'...
2
votes
0answers
50 views

Adjust terminal size using a system call from within Vim

All, I have a little function I'm trying to fix. I'd like to change my terminal Vim window size using the ASCII escape sequence \e[8;40;130t so that my writing window size stays the same when I use a ...
2
votes
0answers
170 views

Vim function somehow adds Ctrl-j to macro?

I've written a short vim function that allows me to edit the contents of vim registers: fun! RegBuff() let vimstuff = "/tmp/vimstuff" if !isdirectory(vimstuff) call mkdir(vimstuff) ...
2
votes
0answers
46 views

Why does append(…, readfile()) add a zero when run using Ctrl-R?

While looking for ways to solve How can I create a /* comments section with /// like in XCode?, I cam across this puzzling behaviour. Say I have foo.txt containing: /*! * @author Robbie Yi JIANG, 29-...
1
vote
1answer
49 views

Yank both sides of equation

While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this \begin{gather*} 5 \cdot \left( A A 3 \right) + 50 \cdot \...
1
vote
2answers
58 views

Function to remove all trailing whitespace

I have the following function to remove every trailing white space from the document, without changing my cursor position function! StripTrailingWhitespaces() let l = line('.') let c = col('.') ...
1
vote
4answers
362 views

How to delete a pair of parenthesis with backspace

I'm using vim to code C++ and I want to handle the parenthesis more easily. I knew that there was a plugin named Surround, which can help us but it is not what I expected. I set some shortcuts as ...
1
vote
1answer
84 views

Make mapping that accepts count independent of line count

I have a mapping that looks like this: nnoremap <expr> <c-x>b ":call MyFunction(" . v:count . ")\<cr>" I also tried to instead define function with range, but the effect is the ...
1
vote
2answers
511 views

vimscript: get result of cmdline-completion Ctrl-a

Need a function that, returns a string, which is result of cmdline-completion Ctrl-a, i.e., a list of words seperated by space. given an input string of partial ex-command. For example, given :h ...
1
vote
2answers
80 views

sourcing .vimrc function does not work

I am using nvim v0.4.3. This is the latest version available in repository. I have an Arch build with the most up to date packages and kernel 5.6.4-arch1-1. I have the below function in my ~/.vimrc: ...
1
vote
3answers
43 views

How to loop backwards through getline

I want to code a vim function, which searches backwards a specific char from the cursor at the current line. Here is what I've done: function! CustomSearch() let currentLine = getline(".") "...
1
vote
3answers
101 views

Is it possible to create map to call Funcref (including lambda)?

Is it possible to do this: function Foo(func_ref) abort nnoremap <buffer> <cr> :find_a_way_to_call_func_ref endfunction function s:callback() abort echo "123" endfunction command ...
1
vote
2answers
511 views

Call function on several lines

I have a function that changes the 6th caracter of a line to a dollar symbol ($). I want it to be performed on several lines, so I naively did a "while" loop using a counter (as shown under). But ...
1
vote
1answer
34 views

write easy accessable function

I want to run an external command by typing :runMyCommand (just run a shellcommand). I thought I'd put the shell command execution into a function, so I can call it with :call runMyCommand, but I want ...
1
vote
2answers
72 views

VIM command to insert multiline text with argument

new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type :pyp x then VIM will autofill where my cursor is @property def x(self):...
1
vote
3answers
161 views

NERDTree maximize on enter

I'd like to maximize NERDTree when I enter the NERDTree buffer. The command I have so far is: autocmd BufEnter NERD_tree_* <A> ...but the <A> part is wrong. I get the error: E21: Cannot ...
1
vote
1answer
26 views

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

For example, I have the following code command! -range Test call TestFunc() function! TestFunc() abort " if run with range " get selected content " else " get current word under the ...
1
vote
1answer
51 views

vim expression unusual behavior

While playing with map() I get an error, which I cannot understand. In essense, pu=map(getcompletion('*','event'), '"h" . v:val') pu=map(getcompletion('*','event'), "'h' . v:val") pu=map(...
1
vote
1answer
136 views

calling function from normal mode without *noremap

I realise this might be impossible, but thought it might be worth asking anyway. In vim, the typical way I map keys to functions is using *noremap commands. However the downside of this is the fact ...
1
vote
1answer
25 views

How to get a function to print something to colon command and let user edit it?

I need to write a function that when executed it opens colon command and paste some stuff on to it and let user edit it before executing the command. for example function! EditRelativePath() :e ...
1
vote
1answer
51 views

Vim is executing commands that were part of a function definition

On one copy of Vim that I use sometimes, I get errors like this when I start up: Error detected while processing /home/besham/.vimrc: line 524: E749: empty buffer line 555: E749: empty buffer line ...
1
vote
1answer
49 views

How do I get help for a specific package?

I am at a phase of cleaning up my choice of plugins and I want to read the documentation provided by a package through vim it self. With that in mind, I don't know a way to: get to the top of the ...
1
vote
1answer
64 views

How to transform this mapping into a function?

So I made this mapping "%p$?/<CR>d$V"uy:cd <C-r>u<CR> That is the crux of the mapping but it leaves artifacts in the current file, that is part of what I want to avoid. My attempt ...
1
vote
1answer
84 views

Can I map the number of search matches to a key?

I'd like to map 0 to whatever number of matches I currently have. For example: This here has 5 matches. If I wanted to perform a macro on every instance, i'd like to be able to simply type: 0Q (Q is ...
1
vote
1answer
55 views

Improve copy to clipboard using xclip

I'm trying to improve the answer here for copying to clipboard in a vim without the +clipboard option. The problem with the above approach is that :w always writes the entire line, so it is not ...
1
vote
2answers
22 views

How to I insert a return value from a function call into an existing buffer?

This following vim script calls a function that returns an array called lines: call vimwiki#diary#generate_diary_section() There seems to be a bug and nothing is done with the lines array—the content ...
1
vote
2answers
38 views

How to create an abbreviation with space

I'm trying to create an abbreviation with the command ab: if I type if ( under INSERT mode, I want some piece of code to be printed immediately. Here is what I've tried: ab if<Space>( if<...
1
vote
1answer
39 views

Unexpected behavior with feedkeys

Consider the following code snippet, which aims to provide y without moving the cursor. function! Yank_fixed_cursor() let save_cursor = getcurpos() call feedkeys("y", 'nitx') call setpos('...
1
vote
3answers
61 views

Create a mapping for a “smart” Home key

With other text editors, if you press Home, it first takes you to the indent, then all the way to the left. Is this possible with Vim?
1
vote
1answer
95 views

Error detected while processing grammarous.vim: E117: Unknown function

I am trying to install the vim-grammarous plugin for Vim 8.0.1453. I have cloned the repo into my .vim/plugin directory. Now every time I open Vim I get an error message which also occurs when I try ...
1
vote
1answer
48 views

vim8 - why does my 0read not read? [closed]

I have the following function in an autoload file: function! closet#FindSkeletonForFile() if exists('b:closet_skeleton') return 0 endif let b:closet_skeleton = s:closet....
1
vote
2answers
78 views

Insert text in the middle of a string vimscript

I'm trying to make a function in vimscript that converts markdown to html files using pandoc and putting that converted file in a specified directory. Currently i am putting the file in a directory ...
1
vote
2answers
335 views

Gvim - Change window position horizontally

I want to create a command that will place my gvim window to a different "x" location on my screen while maintaining the "y" position. I do this in my _gvimrc by calling winpos 319 16 I want to ...
1
vote
1answer
98 views

Possible to set `foldexpr` using a function reference?

Got this: let Func = function(folding_function) "folding_function is name of function setlocal foldexpr=call(Func(v:lnum)) This is so user can set a custom function for folding in their config file. ...
1
vote
1answer
137 views

Function arguments + shell

I'm trying to write a command for adding a Python import line. The problem is that what's added it import a:name and not the module name. Here's the code func! PyImport(name) w silent !isort ...
1
vote
1answer
96 views

Binding horizontal scroll in a function with feedkeys

I'm trying to create a function to make editing TSV files easier. The function changes spacing settings, turns off wrapping, and creates a 1 row tall split at the top so that the column headers are ...
1
vote
2answers
146 views

separating folds with constant amount of newlines

Is there a succinct way to separate folds, open or closed, by the same number of newlines throughout the buffer? I'm using fold by indent. The goal is two visible blank lines between each fold (...
1
vote
1answer
605 views

How to use a value stored in a variable in the command mode?

Consider the following function function! AbbreviationsOpen() "go to the first word of the last line normal G "store the word in the register named a let @a = expand('<cword>') ...
1
vote
1answer
448 views

How to source .vimrc inside a function in .vimrc?

This is part of my vimrc-- function! Abbreviations() let a:code=input("Which Abbreviation: ") "gen if(a:code=="gen") source ~/.vim/Abbreviations/GeneralAbbreviations.vim endif ...
1
vote
1answer
169 views

Convert newline to something else in variable using substitute

Inside of a variable, a newline is encoded as ^@ which represents a null byte. In the vim Ex command line it is possible to issue something like substitute(var, '^@', '\\n', 'g'), this produces \n ...
1
vote
1answer
237 views

How to use function returns?

I want to create a function to copy VHDL component. I can get the component start and end line number as returned value from other functions. But don't know how to use the returns to copy the ...