Questions tagged [user-commands]

Questions about :command and how the user can define custom commands.

Filter by
Sorted by
Tagged with
2 votes
3 answers
185 views

Insert current date into buffer

I want to insert current date into buffer. I did in my .vimrc - it works: nnoremap <F5> "=strftime("%Y%m%d %H:%M:%S %Z")<CR>P But how can I create a Vim command (e.g.: :...
Clem Fandango's user avatar
1 vote
1 answer
99 views

Can I make a user command non-silent?

Summary I use a command and it prints a message. When I create a user command that runs the same underlying function, I don't see the message. Example The command :Telescope git_status from the ...
nullromo's user avatar
  • 206
0 votes
1 answer
279 views

How to pass an argument to a user command that contains a space

I have a user command that grep-replaces files recursively using vimgrep and cfdo: -- Grep vim.api.nvim_create_user_command( 'Grep', function(opts) if #opts.fargs < 1 then print("...
Martin Braun's user avatar
1 vote
1 answer
105 views

Lowercase user-commands?

After years of torture, my trusty rusty keyboard is finally waning its power—it now seems to believe that I'm jitter-clicking whenever I press q. Before getting a new one, I would like to ask if there ...
Futarimiti's user avatar
1 vote
3 answers
2k views

Can I use Telescope to find all commands and functions?

I am switching over from Emacs, Doom Emacs specifically. It had a very nice feature where you could just press M-x and then start typing the command you're looking for and it will fuzzy find it for ...
Vipul Rajan's user avatar
1 vote
0 answers
117 views

How to get both the start and end (row, col) of the visual-selected region in `nvim_create_user_command`?

I want to create a user command that can get the visual-selected text (may across multiple lines) from users when the command is calling upon the v-selected range. (so the cursor columns of both the ...
NeoZoom.lua's user avatar
  • 1,462
0 votes
1 answer
57 views

How do I call a user-defined command from a function, and wait until it finishes?

The two functions in the script below can be called independently via key mappings <leader>1 and <leader>2. In a perfect world, <leader>1 will always be called first. If the user ...
Phil R's user avatar
  • 491
1 vote
1 answer
219 views

Sort from current column from visual selection

I am trying to write a :sort wrapper that sorts from current column. command! -bang -range=% -nargs=* Sortc exe '<line1>,<line2>sort<bang> <args> /\%' .. virtcol('.') .. 'v/' ...
balki's user avatar
  • 355
3 votes
1 answer
252 views

How can I rename a command but still use the original command?

I have the following Vim code: function! TabClose(...) if a:1 == "" tabonly elseif a:1 == "left" Tabops Close Left elseif a:1 == "right" ...
Amarakon's user avatar
  • 261
1 vote
1 answer
468 views

How to write a user-defined command in vim?

I have read map.txt in vim doc and tried to create my Ex command. But the doc is quite hard to understand. I wanna write a command to call 2 command is PlugInstall and CocInstall. command! MyCommand :...
Pham Hung's user avatar
  • 113
3 votes
1 answer
3k views

Custom Arguments to User Command

I am trying to create a command that runs a function, however, I can seem to get custom arguments that are autocompleted when pressing Tab. How would it be possible to set these arguments? The current ...
InsertSON's user avatar
  • 376
0 votes
1 answer
39 views

How to delete all user-defined commands starting with XYZ?

How can I delete all user-defined commands where names share a common prefix (e.g. XYZSomeCmd, XYZAnotherCmd, ...)? Found myself with a lot of dead commands while writing a plugin and changing prefix ...
user3342816's user avatar
2 votes
2 answers
785 views

How to make an user-defined command that works in normal and visual mode?

Goal I would like to create a command that can automatically put an x inside a []. This command should work in both normal mode and visual mode. normal mode If it is in normal mode, and the cursor is ...
melvio's user avatar
  • 599
1 vote
1 answer
143 views

How do I call a function from a user command? (Getting a "trailing character" error, E488)

So I was trying to setup a custom command to change tab width on the fly. function! SetTab(width) set tabstop=a:width endfunction :command Tab -nargs=1 call SetTab(<args>) The idea is to ...
ramgorur's user avatar
  • 113
0 votes
1 answer
110 views

Intercepting & modifying commands

Is it possible to intercept incoming user commands? For example, if the user executes :echo 'hello', then I would like to compare the string and the command, and change it to execute :echoerr 'hello' ...
daedsidog's user avatar
  • 153
0 votes
2 answers
39 views

How to create user command to delete current file and saveas to new file?

I use :saveas to move a file while maintaining its undo history. Most of the time I immediately delete the old file. So if I'm editing foo.txt I'll issue :saveas bar.txt :!rm foo.txt I wanted to ...
Alec Jacobson's user avatar
0 votes
1 answer
60 views

How can I execute commands or move to other tabs without first closing the terminal executed by `:term`?

I've created a new terminal by executing :tabnew +term. How can I execute other Vim commands or move to another tab without first closing the terminal (:term)? Currently, all input is directed to ...
Shuzheng's user avatar
  • 1,235
2 votes
0 answers
32 views

Chaining user-defined commands [duplicate]

Assuming I have defined 2 commands in .vimrc MultipleEmptyLinesToOne RemoveExcessiveSpaces I can run them in command-line mode: :exec "'<,'>MultipleEmptyLinesToOne" | exec "'<,'>...
ka3ak's user avatar
  • 261
1 vote
1 answer
204 views

How do I remap the :make command to run a docker container?

I use a custom docker container to build my project so my "make" command is actually docker run --rm -v $(pwd):/src container make $arg. I'd like to be able to type :make $arg in command mode and ...
Josiah's user avatar
  • 113
0 votes
1 answer
205 views

How to use variables in a user defined command? [duplicate]

I have this user defined command: command! PS silent! w | PlugSnapshot! ~/.vim/plugins/plugins.lock How could I change it to take the filepath from a variable, something like this: let ...
ruohola's user avatar
  • 666
7 votes
1 answer
544 views

What is the purpose of user `command` in Vi/Vim?

What is the purpose of user command in Vi/Vim? I noticed that a lot of commands are usually used to call functions like this: command! TrimWhitespace call TrimWhitespace() From my observation, ...
otter.pro's user avatar
  • 498
0 votes
1 answer
60 views

Is it possible to non-recursively remap a command?

So Fugitive defines :Glog. This is, apparently, aliased to a call to the script-local s:Log() function (according to :command Glog.) My muscle-memory has been using glog at the command-line, for ...
ELLIOTTCABLE's user avatar
4 votes
2 answers
577 views

How to edit a file in a new tab?

I want to map :e file.txt to :tabnew file.txt, or the equivalent setting for editing file in a new tab. What do I need to put into my vimrc?
Bicheng's user avatar
  • 247
1 vote
1 answer
2k views

Using arguments in a .vimrc command

I've discovered that I can edit the .vimrc file to create custom commands and would like to create simpler search and replace alternative commands, but have no idea how to edit %s/foo/bar/gc so that ...
Hussar's user avatar
  • 75
0 votes
2 answers
492 views

Mapping using tpope/vim-surround only works with command

I have this mapping to surround a Word with jQuery id selector $("#...") and it works fine using a command: command! JqId normal viwo^[i#^[ysiW"ysiW)i$^[E nnoremap <Leader>jqid :JqId<CR> ...
raul.vila's user avatar
  • 113
0 votes
2 answers
63 views

Extract JavaScript variable (feature example): how to improve it?

I am a Vim newbie with one week experience and I'm already enjoying it. I've successfully written an (ugly) command+function+mapping with which I can extract some code to a new variable in JavaScript....
raul.vila's user avatar
  • 113
9 votes
3 answers
8k views

Passing visual range to a :command as its argument

I have a command: command! -nargs=1 TmuxSession !tmux.session "<args>" and I want to run it in visual mode so that selected text will be passed as argument. By selected text I mean selected in ...
lewiatan's user avatar
  • 421
1 vote
1 answer
744 views

How to create user command without a capital letter as first letter?

I know I can set user-defined commands using the :command Foo bar syntax, but I want a way to set a command without using capitals. For example, to save all open buffers, the command is :wa, but that'...
Brandon Sturgeon's user avatar
2 votes
1 answer
594 views

Better "bro ol"

Inspired by a nice solution on a Stack Overflow, the following was put in my vimrc: command! Bro :vnew +setl\ buftype=nofile | 0put =v:oldfiles \| nnoremap <buffer> <CR> :e <C-r>=...
bongbang's user avatar
  • 199
0 votes
1 answer
59 views

Why does the <count> escape sequence insert the current line number in my user command?

I've added the following to my .vimrc in order to test how <count> works in a user defined command because I was noticing odd behaviour: :command! -count=1 CountTest :exe "echo " . <count>...
AMackie's user avatar
1 vote
1 answer
57 views

Why only part of a command is executed

I have created a new command command! Tags execute '!GTAGSFORCECPP=1 gtags' | cscope kill -1 | cscope add GTAGS However, for some reason cscope add GTAGS doesn't execute when I execute :Tags. Can ...
flashburn's user avatar
  • 689
3 votes
2 answers
2k views

Filtering text out to new tab with custom command

I would like to add to my vimrc a custom command (:customFilter {pattern}) for filtering text and placing result in a new tab. I tried using :global or :vimgrep /{pattern}/ % but the problem is that ...
lewiatan's user avatar
  • 421
13 votes
2 answers
12k views

Is there a command to enter Visual Block mode?

I've set up a bunch of key mappings that would cause many Vim users to cringe. Now I am unable to get into Visual Block mode using Ctrl-V (Ctrl-Q isn't mapped, but it doesn't work). Is there any ...
Dan's user avatar
  • 485
4 votes
1 answer
2k views

How to define a custom user defined command to filter a range?

I want to select some lines in visual mode, and enter a custom command :Ex<CR> so that: '<,'>!expand -t4 is run to expand all tabs in selected range. How to define the Ex command? I ...
Ciro Santilli OurBigBook.com's user avatar