vimrc command to add uid in insert mode - vim

In my .vimrc I have the following, which in Vim Visual mode inserts an unique alphanumeric string.
vnoremap <leader>u :!pwgen -A 8 1<CR>
For convenience, I want to do the same thing in Vim Insert mode: something like this (not working: .vimrc):
iabbrev uuid `pwgen -A 8 1`
where the abbreviation uuid (or :uid: or similar) triggers the BASH pwgen command (e.g replacing uuid with moh6sei5).
Edit
Per the comment immediately following mentioning Defining linux command inside vim abbreviations
the issue is that strtime (there) is a native Vim function (:h strftime), whereas pwgen is a BASH function. In .vimrc:
works:
iabbrev xxx <C-r>=strftime('%c')<CR>
" Sun 02 Oct 2022 11:03:58 AM PDT
does not work:
iabbrev uidd <C-r>=pwgen -A 8 1<CR>
iabbrev uidd <C-r>=!pwgen -A 8 1<CR>
iabbrev uidd <C-r>=`pwgen -A 8 1`<CR>
...

inoreabbrev <expr> uuid system('pwgen -A 8 1')->trim()
Breakdown:
<expr> tells Vim that the right hand side of the abbreviation is to be evaluated as a Vim expression.
Since pwgen is an external command, you need :help system() to capture its output.
External commands often end with a newline character so you need to :help trim() it.

This works, .vimrc:
inoremap <F3> <c-r>=trim(system('pwgen -A 8 1'))<cr>
Test: Insert mode, typing apple <F3> banana gives apple ipav7lier banana, where <F3> is that key. Each <F3> keypress inserts a UID.
system(): Vim system() command (:h system in Vim)
pwgen: BASH alphanumeric password generator command (man pwgen in BASH terminal)
pwgen -A 8 1: lowercase only (-A), 8 characters, 1 password
<F3> : keyboard function 3 key
See also: https://www.grailbox.com/2021/07/insert-uuids-in-vim-neovim/ ... links to (explanation) https://old.reddit.com/r/vim/comments/bnh7su/inserting_the_output_of_an_external_program_inline/

Related

How to put current filename in vim commands in .vimrc

i want to create a shortcut to run typescript in vim how can i access the filename in .vimrc
i want to run short scripts.
nmap <leader>r :! ts-node current-file<return>
First try %, as this gets substituted by the file name (as explained in :help :!):
nmap <Leader>r :! ts-node %<CR>
But if the file name contains whitespaces or special characters, you might see some errors. In that case, you need to escape the file name as follows:
nmap <Leader>r :execute '!ts-node ' . shellescape(expand("%"))<CR>
You're strongly adviced to read :h cmdline.txt, section 6 :h cmdline-special from top down to bottom.
nnoremap <leader>r :!ts-node %:S<CR>

<C-j> mapping not working in Vim when calling Vim from ZSH Vi mode

When calling Vim from zsh via vim, <C-j> works perfectly and I am able to use it to select completions in the same manner as <C-n>.
When starting Vim from zsh's vi mode via the v key, I cannot get <C-j> to behave the same as <C-n>, whether I input the key-bind manually or place it in the config.
Additionally, when I invoke edit-command-line manually, <C-j> works.
Note: this issue does not arise when calling Vim in a similar manner in Bash's vi mode.
.vimrc with Vim 8.0
inoremap <expr> <C-j> "\<C-N>"
inoremap <expr> <C-k> "\<C-P>"
.zshrc with zsh 5.3.1 (x86_64-apple-darwin14.5.0)
EDITOR="vim"
set -o vi
autoload -Uz edit-command-line
zle -N edit-command-line
bind key -M vicmd 'v' edit-command-line
EDIT: some further investigation reveals that when I start Vim through zsh's vi mode, <C-v><C-j> produces ^M; however, when I start Vim normally, <C-v><C-j> produces ^#.

How to -enable- vim's "Press ENTER or type command to continue"

There are lots of questions on here about how to disable the "Press ENTER or type command to continue" text when running an external command from vim. But I want to know how to enable it. As you can see below, I'm not using silent or any other means to disable it.
Here is the relevant portion of my .vimrc:
nmap <leader>r :call NoFrills()<CR> "(r)eveal hidden chars
nmap <leader>i :set paste!<CR> "(i)ndenting on/off
nmap <leader>h :set nohlsearch!<CR> "(h)ighlighting of search terms on/off
nmap <leader>w :call SudoWrite()<CR> "(w)rite file, sudoing first
nmap <leader>a :! sudo service httpd restart<CR>"(a)pache restart
nmap <leader>p :! perl -c %<CR> "(p)erl -c
When I execute a command that should have output, like with the key sequence \ p, the command executes but any output is quickly dismissed from the screen without pausing or prompting me to continue. Why is that?
If I execute :! perl -c % from vim's command mode, it shows me the output and prompts me to continue, as expected.
:version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Feb 17 2012 10:23:31)
:set
--- Options ---
background=dark filetype=perl incsearch scrolloff=3 ttyfast
comments=:# helplang=en list shiftwidth=4 ttymouse=xterm
commentstring=#%s history=50 number syntax=perl viminfo='20,"50
define=[^A-Za-z_] hlsearch ruler tabstop=4 wildmenu
backspace=indent,eol,start
complete=.,w,b,u,t,i,k
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,latin1
formatoptions=tcrq
guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
include=\<\(use\|require\)\>
includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.pm','')
indentexpr=GetPerlIndent()
indentkeys=0{,0},:,0#,!^F,o,O,e,0=,0),0=or,0=and
isfname=#,48-57,/,.,-,_,+,,,#,$,%,~,=,:
keywordprg=perldoc -f
listchars=tab:>-,trail:o
path=/usr/local/lib64/perl5,/usr/local/share/perl5,/usr/lib64/perl5/vendor_perl,/usr/share/perl5/vendor_perl,/usr/lib64/perl
5,/usr/share/perl5,,
wildmode=longest,list,full
As #romainl has already indicated in the comments, the appended comments are part of the mappings, and one of those characters (e.g. the <Space>) dismisses the hit-enter prompt. You could include a trailing comment by separating with | (which must be escaped or written as <Bar>) inside a mapping):
nnoremap <leader>p :! perl -c %<CR>| "(p)erl -c
But I'd recommend to put the comments on a separate line:
" (p)erl -c
nnoremap <leader>p :! perl -c %<CR>
PS: You should use :noremap; it makes the mapping immune to remapping and recursion.

How do I automatically re-load a file that gets modified by a bang (!) commandin Vim?

I have set up a line of vimscript in my .vimrc file to pretty print JavaScript files:
nnoremap <leader>p :!js-beautify -r -j %<cr>
I want to just automatically reload the file instead of being prompted when vim comes back from the shell, is that possible? Thanks.
:! can be used a) to execute arbitrary shell commands or b) as a filter.
You are using it to run js-beautify against the file associated with the current buffer with the following consequences:
You are forced to exit Vim.
The file is modified outside of Vim so you get a prompt asking you if you want to reload or not.
Hence the many seemingly pointless <CR>.
What you actually want is to run js-beautify as a filter against the current buffer, which doesn't require you to exit Vim or press <CR>:
nnoremap <leader>p :%!js-beautify -f - -j<cr>
the special range % represents the whole buffer, it's the range on which we want to apply our filter
-f - passes the content of the buffer via stdin
That's it: no <CR>, no prompt, no mess.
As a bonus, here is a custom command from my config (I didn't want a mapping for that):
command! -buffer -range=% Format execute <line1> . "," . <line2> . "!js-beautify -f - -j -B -s " . &shiftwidth
edit
You can use context marks to return the cursor to its initial position:
nnoremap <leader>p m`:%!js-beautify -f - -j<CR>``
As the help for :! notes:
Vim redraws the screen after the command is finished,
because it may have printed any text. This requires a
hit-enter prompt, so that you can read any messages.
To avoid this use:
:silent !{cmd}
The screen is not redrawn then, thus you have to use
CTRL-L or ":redraw!" if the command did display
something.
So if you wanted to use the :redraw! approach, for example, you could do this using something like
nnoremap <leader>p :silent !js-beautify -r -j %<cr>:e!<cr>:redraw!<cr>

what is the meaning of \ | in a map vim command?

Here is a map command.
nnoremap <F5> :w\|!R %<CR>
1.what is the meaning of \ here?
2. does | mean pipe ?
The | character separates two Ex commands, see :help :|. It's like ; in programming languages like C and Java. It has nothing to do with pipes; Vim hasn't that concept (which is typically found in shells).
It is escaped here so that the entire command sequence belongs to the mapping; i.e. it maps to :w|!R %<CR>. Without escaping, Vim would execute the following instead:
:nnoremap <F5> :w
:!R %<CR>
Note that you can also write <Bar> (cp. :help key-notation) instead of \|, and the former is more frequently used.

Resources