VIM run command when opening .sql files - windows

I am having a hard time figuring this out.
I am trying to run the below command when i open a sql file in VIM.
:%!sqlformat --reindent --keywords upper --identifiers upper -
I know this is probably easy and i am over thinking it but have been attempting various variations of the below in my _vimrc file and no luck
autocmd FileType sql call SqlFormatter()
augroup end
function SqlFormatter()
set noai
set mappings map ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<CR>
endfunction
EDIT:
when i run this inside VIM nothing happens
:call SqlFormatter()
when i run i can see the SqlFormatter() function in the list
:function
Currently the function in my _vimrc file looks like and i am still having no luck
autocmd FileType sql call SqlFormatter()
augroup end
function SqlFormatter()
set noai
" set mappings...
map ,pt :%!sqlformat --reindent --keywords upper --identifiers lower -
endfunction

Let's break this down into components. First the mapping:
map ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>
The general rules with mappings is to supply a mode and use noremap if you are able. So this becomes:
nnoremap ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>
Next we need to understand buffer-local mappings. Your mapping is global which means once you open a buffer with a 'filetype' of sql then this mapping will work in any buffer. This is not likely not what you want. By using the <buffer> option we can set this mapping for just this buffer.
You are using an FileType autocmd event to trigger this mapping for sql filetypes. Here is that cleaned up:
augroup SqlStuff
autocmd!
autocmd FileType sql call SqlFormatter()
augroup end
function SqlFormatter()
set noautoindent
nnoremap <buffer> ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>
endfunction
Additionally, you may want to avoid the autocmd & function all together and just add both the setting and the mapping into ~/.vim/after/ftplugin/sql.vim
set noautoindent
nnoremap <buffer> ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>
Note: I have not tested this mapping so if there is an issue with sqlformat then that will also need to be fixed
For more help see:
:h :map-commands
:h :map-local
:h :autocmd
:h :augroup
:h FileType
:h after-directory
More help from Learning Vimscript the Hard Way:
Basic Mapping
Modal Mapping
Strict Mapping
Buffer-Local Options and Mappings
Autocommands
Autocommand Groups

Related

Enable smart indent on curly braces

I have to do this every time when I open curly braces
vim config for indent:
"" Fix backspace indent
set backspace=indent,eol,start
"" Tabs. May be overridden by autocmd rules
set autoindent
set smartindent
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set smarttab
What to do?
You could have a look at amix/vimrc, a powerful vim configuration.
I have installed the awesome version and it could implement what you want. Moreover, it has lots of useful configurations that could improve your efficiency.
If you write go, you could install fatih/vim-go, a powerful plugin adds go language support for Vim.
delimitMate does the thing. Set option delimitMate_expand_cr to 1 or 2 in your .vimrc:
let delimitMate_expand_cr = 2
See:
:h delimitMate_expand_cr
:h delimitMateExpansion
Also you can use simple mapping on {{ to do same:
inoremap {{ <C-o>o{<CR>}<C-o>O

insert bash script output in vim file

I'm writing markdown in Vim and using this shortcut to set correct markdown image syntax:
nnoremap <leader>p :s/.*/![](\0)/ <CR>
when pressing ,p, /path/to/image/img.jpg (in my vim text file) becomes
![](/path/to/image/img.jpg)
But I want to add after that this { width = *variable*% }, like this
![](img.jpg){ width = *variable*% }
I made this little bash script (img.sh) that gives me the variable according to the image size:
#!/bin/bash
VAR=$(identify -format '%h' $1)
echo "scale=3; 300 * (100/$VAR)" | bc
If I do this in vim :r !img.sh /path/to/image/img.jpg I get a number in this case 32
I would want to launch this script with the shortcut above, I tried this:
nnoremap <leader>p :s/.*/![](\0){ width=/ <CR> :r !img.sh /path/to/image/img.jpg <CR> % }
I want you to help me to find a way to not type path to image to execute the script. Path to image is already written in the text if i could find a way to indicate to vim to place it in the shortcut after img.sh it would be great !
You can use a Vimscript expression on the replacement side of your :s command, that way you can refer to the text in the match (which is the path to the image) using submatch(0). You can use an expression on the replacement of a :s by beginning it with \=, see :help sub-replace-expression for more details.
Using a replacement expression, you can also use the system() function to call the external command, instead of using a separate :r !... command to read its output into the current buffer.
Putting it all together:
:s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/
Or to add a mapping to it:
nnoremap <leader>p :s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/<CR>
(NOTE: You might also want to add a :noh command to the end of your mapping, otherwise it will keep highlighting the search for .* which matches everything.)
nnoremap <leader>p :s/.*/\='![]('.submatch(0).']{ width='.trim(system('img.sh '.submatch(0))).' }'/\|noh<CR>

I want to removing highlighted braces in vim

" Configuration file for vim
set modelines=0
" CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible
" Use Vim defaults instead of 100% vi compatibility
set backspace=2
" more powerful backspacing
" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup nobackup
syntax on
set ai
set shiftwidth=4
set tabstop=4
set ruler
set backspace=2
set ic
set hlsearch
set incsearch
set smartindent
set confirm
set history=200
set cursorline
set number
:nohl
:set nowrap
set mouse=a
colo google
hi Normal ctermbg=none
the above is my .vimrc
As you can see, my braces was highlighted at line 3 and line 5 although my cursor is not on them(so, the highlighting may not be controlled by MatchParen).
However, while I can't remove their highlighting by using MatchParen, I think there must be another way to deal with it.
How can I disable these highlighting???
thx :)
You need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. When you have the name of the offending syntax group, you can investigate where it comes from; (the last lines of) :scriptnames may help.

Vim Command Mapping "Copy to Clipboard" Not Working

I recently began using Vim as my primary editor instead of programs like Atom/VSCode. I added a number of leader mappings to simplify tasks I do quite often but I'm having trouble with a few of them.
In Visual mode, I would like to be able to press <Space>y to copy the current selection to the clipboard (+ register). I've verified that I can do this manually by entering visual mode, selecting the text I want, and pressing "+y. However, my mapping doesn't seem to work:
vmap <Leader>y "+y
I set my leader the following way:
map <Space> <Leader>
I do it this way so that when showcmd is set, I get a visual indicator in operator-pending mode. By looking at that indicator, I can tell that when I press <Space>, I do enter operator pending mode on the \ key as expected. Then, when I press y, I am no longer in operator pending mode, but I am still in visual mode and haven't yanked the selection to the register.
To make sure there wasn't a plugin colliding with my mapping, I backed up my .vimrc and replaced it with one that only has the following contents:
set showcmd
map <Space> <Leader>
vmap <Leader>y "+y
Does one of these keys need to be escaped? Or am I doing something else wrong?
(I'm currently running Ubuntu Bash on Windows. Vim is version 7.4)
For reference, I got the idea from this article (And use the exact same command):
https://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
Thankfully, the fix is pretty simple.
map <Space> <Leader>
is incorrect. The right way is
let mapleader=" "
From :help mapleader
*<Leader>* *mapleader*
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead. Example: >
:map <Leader>A oanother line<Esc>
Works like: >
:map \A oanother line<Esc>
But after: >
:let mapleader = ","
It works like: >
:map ,A oanother line<Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
How to define the "leader" key is explained under :help mapleader. If you want to use <Space> as "leader" you are supposed to do:
let mapleader = "\<Space>"
Note that the "leader" key is not a special key at all. With <Space> as "leader", the two mappings below are strictly equivalent:
vmap <leader>y "+y
vmap <Space>y "+y

How to set line numbers by default in vim?

I know you can add line numbers in vim by using,
:set number
How do I set this to default behavior?
add this line to ~/.vimrc (if not exist, create a new file)
:set nu
and save the file
the settings in $HOME/.vimrc file would be loaded automatically.
Except for set number to show linenumber, I have this to toggle normal line number and relative line number: (by pressing <leader>nu)
"---------------------------------------------------------
"toggle relativeline number
"---------------------------------------------------------
function! ToggleRelativeNumber()
let &relativenumber = &relativenumber?0:1
"let &number = &relativenumber? 0:1
endfunction
nnoremap <silent> <Leader>nu :call ToggleRelativeNumber()<cr>
https://github.com/sk1418/myConf/blob/master/common/.vimrc#L704
Open the file /etc/vim/vimrc (in sudo mode) and add the following line: set number
By the way, you will also find other (highly recommended) interesting commands you can enable:
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set showcmd " Show (partial) command in status line.
"set showmatch " Show matching brackets.
"set ignorecase " Do case insensitive matching
"set smartcase " Do smart case matching
"set incsearch " Incremental search
"set autowrite " Automatically save before commands like :next and :make
"set hidden " Hide buffers when they are abandoned
"set mouse=a " Enable mouse usage (all modes)

Resources