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
0
votes
1answer
17 views

How can I open a split terminal with :term and then run a command in that terminal?

I'm trying to write a function to open a split terminal in Vim and then run the current file in that terminal. A non-working example trying to explain what I'm trying to do is function! Foo() :bel ...
0
votes
2answers
43 views

Function-local inner functions

These exist with lambdas function A() let B = { -> 'thing' } endfunction // B does no exist But the lambda returns a value and not a sequence of commands. (I know I could put execute("..&...
0
votes
2answers
29 views

using EasyAlign in a function

I frequently use EasyAlign in my tex files to align tables around the $ delimiter such that this header1 & header2 & header3 \\ data1 & data2 & data3 \\ becomes this header1 & ...
1
vote
1answer
37 views

Character level buffer modification

It is possible to modify buffer at the line granularity. E.g. :py3 vim.current.buffer[0] = "abc". But it deletes the attached marks even if a single character is replaced. Is it possible to ...
0
votes
0answers
17 views

Does vim have support for processing its comma-separated options?

Vim has comma-separated dictionary options like 'comments' and 'listchars': :set comments? comments=s:--[[,m: ,e:]],:-- :set listchars? listchars=tab:⇥ ,trail:␣,extends:⇉,precedes:⇇,nbsp:⚭ Commas ...
0
votes
1answer
30 views

Can I define a function with '#'-namespace in a file with hyphen '-' in its name?

I often see #-namespaces in plugins source code, e.g coc#status(), fzf#run(), fugitive#extract_git_dir. I would like to use namespaces in my own config, mappings and commands source code also. But I'm ...
0
votes
1answer
43 views

Turn a search & replace regex one liner, with counters, into a function with a for next or while loop

this post has been simplified and re-written here: Making a function work with a loop I have this one liner, which takes the regex pattern match and replaces it with a string and a couple of counters: ...
1
vote
0answers
30 views

neovim, vimscript execute keystroke from function

I am trying to execute a keystroke from function when its called. However, it does not work. function SmartNERDTreeFocus() if expand('%') =~ 'NERD_tree.*' normal <C-w><C-p> ...
0
votes
1answer
30 views

What's the difference between command and function?

As I know, both of them can do quite the same things, but one should call to invoke and other one should use execute to invoke. But whats the difference between command and function? when should I use ...
2
votes
2answers
65 views

Making functions react to keypress

Im very new to vim scripting. I have the following script in my .vimrc: If there is no user input aka I press <CR> => go to recent buffer If input is 'd' => delete all buffers except for ...
0
votes
1answer
37 views

Calling a function before autocmd event is triggered

In my vimrc I have a key mapping for switching buffers that also automatically resizes the splits by calling a function: nnoremap <silent> <M-h> <c-w>h:call Splitresize()<CR> ...
0
votes
0answers
19 views

Remove or Unset a function while vim is running [duplicate]

I know how to create a function: function Something() ... endfunction And how to overwrite it: function! Something() ... endfunction But how to actually remove/unset the function, so it doesn't ...
2
votes
1answer
24 views

vimscript to skip a block of text with lines containing similar words

I need a little help with some vimscript-ing. I have a huge log file with multiple blocks of text resembling the following: [log file start] ... ... Information: Setting NDR rule shield_rule on net a ...
0
votes
1answer
36 views

How can I append to a terminal buffer in vimscript?

I am trying to implement a vimscript function which appends to a terminal buffer. It works when I write this as a single mapping which uses the normal mode 'a' command to append: nnoremap <Leader&...
2
votes
1answer
46 views

Passing arguments function with --argument or -a format

Is it possible to pass arguments to Vimscript functions with the --argument or -a format, similar to arguments in the command line? I am writing a command that submits a job in a remote machine. I ...
0
votes
0answers
28 views

Measurement/ tracking the time for editing a file (overall and statistics)

I use vim for coding and also for writing scientific articles and book chapters. It would be very interesting to know the time I spent on a file. For example how many hours or minutes the buffer was ...
0
votes
1answer
33 views

Using a multiline closure to execute commands in vimscript

I'm writing a script in which I am in need of closure functionalities. Take this function, for instance: func! SetOpNorm(norm_comm, motion_key = 'y') let l:norm_comm = a:norm_comm let l:...
2
votes
1answer
35 views

How to use func to quickly add specific comments to certain lines of a file?

I wrote a function, the function is used to add comments to some lines, the content is as follows: func Add(x) call setline(a:x+1, "\ //*********************************************************...
1
vote
3answers
149 views

Function That Substitutes Across Multiple Files

I'm trying to create a function that implements the functionality outlined here: Vim search replace all files in current (project) folder My current attempt: nnoremap <C-F> :call ...
0
votes
1answer
39 views

How do I open a relative path under the cursor

I often have file log where there are several relative path to files. The paths are relative to the location of the filelog. My understanding is that I'm able to open the relative path if the CWD of ...
1
vote
2answers
29 views

Using ! in a function

I have the following command for arranging a few lines in a file using sed and column: :-3,+2!sed 's/- 2021/\&2021/' | sed 's/ 2021/\&2021/' | column -s '&' -t This works fine. For ...
4
votes
1answer
131 views

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

Is it possible to declare a function only for a specific filetype? Currently, if I have the following in ftplugin/tex.vim: function! Foo() echom "yes" endfunction Then when I load a *....
0
votes
1answer
39 views

Is it possible to call a shell command which includes default register contents inside a function?

I want to write a function which opens a vim plugin's Github page, eg calling the function while on this line: Plug 'tpope/vim-rhubarb' ... will do !open https://github.com/tpope/vim-rhubarb. Here's ...
3
votes
1answer
41 views

Backward search in current line with search() and z option

I am trying to write a couple of simple functions to find the next and previous links in Vimwiki and move to them. Links in Vimwiki are indicated with the syntax [[link to something]]. Since I only ...
0
votes
1answer
38 views

Change text without interrupting abbreviations

I'd like to add a semicolon automatically in my current line. So I have this in my init.vim: function Test() if matchstr(getline(line('.')), ';') != ';' && strlen(getline(line('.'))) > ...
0
votes
1answer
41 views

Make function run only on specific filetype

So i have a specific function I'd like to edit in my .vimrc, so that it work only on specific filetype (eg: .vim). The code for this example is taken from this answer. function OrgFold(lnum) let ...
0
votes
1answer
46 views

How to return keybinding from a function?

I want the below function to return keybindings but all it returns is a string. nmap <expr> <space>t ManageTerminal() function ManageTerminal() if bufname('terminal') ==# 'terminal' ...
3
votes
2answers
105 views

Reliably saving and restoring registers across a function call [duplicate]

When writing functions to use in mappings and user-defined commands (for plug-ins), it's often convenient to use a Normal-mode command to yank or delete buffer contents. In those cases, it's also nice ...
1
vote
2answers
140 views

<Space> doesn't insert a space within a function

So I'm trying to have a function type out a space which goes as follows: function! UnderlineHeading(level) if a:level == 1 normal! yypVr= elseif a:level == 2 normal! yypVr- else ...
0
votes
1answer
248 views

Vim function : how to expand vim variable in shell command?

I'm trying to write a vim function to update ctags and cscope databases. I'm trying to save the vim variable projectRoot in environment variable then expand this enviroment variable in the find ...
0
votes
1answer
18 views

Any way to create a VimL equivalent of Python's @property decorator?

As the title says, I'd like to be able to use something like Python's @property decorator (its semantics ofc, not the syntax) when writing a 'class' in VimL. Currently I have something along the ...
1
vote
2answers
76 views

Insert output of function with variable into buffer

I'm trying to make a vim function that takes a variable and uses that as part of a shell command and buts it into the current buffer. I tried using read but that just interprets the var as a string. ...
0
votes
1answer
65 views

switch toggle function - background

I'm trying to assign a key toggle to switch between dark or light background. I wrote this function: function ToggleBG() let l:bg='set background?' if l:bg==dark :set background=light ...
0
votes
1answer
608 views

Inside a mapping check if a variable is empty in vim

So i'm new to vim, and specially new to vimscript, and messing around i made the following code to get the name for a file and then create said file in a new buffer. I want a way to check if FileName_ ...
1
vote
1answer
42 views

in-place replacement of code in HTML documents with TOhtml?

I'm working on some old HTML documentation, where are multiple code snippets (several lines long). I need to replace this old HTML-ized code with new one using modern styling. What I have now: ... ... ...
3
votes
2answers
128 views

How to list / show all built-in functions?

How can I list / show all built-in functions, similar to the :function command which lists all (non-builtin?) functions?
1
vote
1answer
33 views

Is there a function which returns the filepath at the cursor?

I'm looking for a function(probably doesn't exist) which can return the filepath under the cursor. I know that there is a command/shortcut gf that helps me jump to that file if the file exists. But I ...
2
votes
1answer
33 views

Where is the help function defined or located?

I'm trying to find where the help function/command is defined. So far I tried: :verbose function help but since the h isn't in uppercase, it doesn't work (not sure if it's even a function at this ...
3
votes
1answer
57 views

bufnr return unexpected number

from neovim help: bufnr([{expr} [, {create}]]) The result is the number of a buffer, as it is displayed by the ":ls" command. For the use of {expr}, see |bufname()| But it ...
4
votes
1answer
79 views

Set variable equal to search match subsection

Unnecessary Context: I want to write a function that closes open HTML tags, because closetag.vim seems complicated, and I like to understand all the stuff I run in Vim. My plan for this is to match ...
1
vote
2answers
387 views

Global options as function local variables

I'm asking about this code snippet: :function TextwidthIsTooWide() : if &l:textwidth ># 80 : return 1 : endif :endfunction This is what vim-help says on the l (local) prefix: Inside ...
0
votes
1answer
35 views

What's the fastest way to go about defining functions in Python?

def counter(num): I always get stuck inside of the parenthesis and find myself pressing ESC, going back to Normal mode, then pressing A to append the colon to the end of the line. Is there a faster ...
1
vote
1answer
74 views

Get correct length for unicode characters

Basically title, the length of unicode characters as calculated by the len() function is off by one, for example :echo len("à") 2 or :echo len("°") 2 is there a way to fix this, ...
1
vote
1answer
222 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 ...
0
votes
1answer
100 views

Formatting using BufWritePost ends up with WARNING

I am trying to format PHP files using the following function: function! PHPCSFixer() let l:fixer_path = '$HOME/....
1
vote
1answer
41 views

Buffer opened when compiling succeeds

I am writing a vim plugin for the fortran filetype. In an autoload script, I have done: " File Name: autoload/makes.vim let s:Compiler = get(g:, "fortran_compiler", "gfortran"...
1
vote
2answers
93 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 ...
0
votes
1answer
75 views

Use normal command output as variable

I want to be able to use some normal commands I tend to use in the day to day as an input for another function, for example 4X, I would like 4X to be expanded to XXXX and then use it in a function as :...
1
vote
3answers
37 views

How to recover the color to previous status?

I wrote a function to show some special characters with red color: function! ShowChar() set list set listchars=tab:→\ ,space:·,nbsp:␣,eol:¶ highlight SpecialKey ctermfg=blue ctermbg=red ...
0
votes
0answers
53 views

How to clear and write current buffer in a function

New to vimscript. I'm writing a function that I want to be able to clear the contents of the current buffer and write this to the corresponding file (or alternatively, clear the contents of the file ...

1
2 3 4 5