Showing posts with label Vi. Show all posts
Showing posts with label Vi. Show all posts

Saturday, December 2, 2023

Friday, November 10, 2023

Vim search for the escaped string of a line


command! -nargs=1 SH let @/ = '\V'.escape(<q-args>, '\')|set hlsearch

nnoremap <C-y> _vg_y<Esc>:SH <C-r>"<CR>


_    go to first non-space character in line

g_   go to last non-space character in line

v    start visual selection

y     copy selection

<C-r>" paste copied selection 

Saturday, August 26, 2023

Fuzzy Finder FZF with Syntax Highlight Preview

FZF is nice cli program that allow quick file navigation that find files as you type.

BAT is nice replacement for cat with highlight features

sudo apt install fzf

sudo apt install bat

Using fzf together with batcat to source code files and preview it with syntax highlight

fzf --preview 'batcat --style=numbers --color=always --line-range :500 {}'


.bashrc

source /usr/share/doc/fzf/examples/key-bindings.bash


if type rg &> /dev/null; then

  export FZF_DEFAULT_COMMAND='rg --files'

  export FZF_DEFAULT_OPTS='-m --height 50% --border'

fi


alias findz="fzf --preview 'batcat --style=numbers --color=always --line-range :500 {}'"


_fzf_comprun() {

  local command=$1

  shift


  case "$command" in

    cd)           fzf --preview 'tree -C {} | head -200'   "$@" ;;

    export|unset) fzf --preview "eval 'echo \$'{}"         "$@" ;;

    ssh)          fzf --preview 'dig {}'                   "$@" ;;

    *)            fzf --preview 'batcat -n --color=always {}' "$@" ;;

  esac

}

export FZF_CTRL_R_OPTS="

  --preview 'echo {}' --preview-window up:3:hidden:wrap

  --bind 'ctrl-/:toggle-preview'

  --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'

  --color header:italic

  --header 'Press CTRL-Y to copy command into clipboard'"


export FZF_ALT_C_OPTS="--preview 'tree -C {}'"


Usage:

vim $(fzf)

Ctrl+R   # open command history

Alt+C   # show directories and cd to it

Vim **<tab>  #open file list and open with vim

cd **<tab> # open directory list and cd to it


(fzf.vim) .vimrc

command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1, {'options': '--delimiter : --nth 4..'}, <bang>0)

set grepprg=rg\ --vimgrep\ --smart-case\ --follow

command! -bang -nargs=? -complete=dir Files

    \ call fzf#vim#files(<q-args>, {'options': ['--layout=reverse', '--info=inline', '--preview', '~/.vim/bundle/fzf.vim/bin/preview.sh {}']}, <bang>0)

"Open Files in the bottom

"let g:fzf_layout = { 'up':  '40%'}

"let g:fzf_layout = { 'down':  '40%'}

Monday, October 17, 2022

Show a vertical column markers in vim (very usefull to check identation)

To show a background line in the whole column 9 and 17

:set colorcolumn=9,17
or short
:set cc=9,17

To change the color to blue
:highlight ColorColumn ctermbg=blue


show line in the cursor column
exe 'set cc='.col('.')
Source: https://superuser.com/questions/249779/how-to-setup-a-line-length-marker-in-vim-gvim

Friday, July 1, 2022

Vim find and repeat search

 For example,

use

f<space>  to go to the next space

; to repeat search and go to the othe next space;


f(  find next (

;;;  go to the fourth ( 

Wednesday, March 31, 2021

C/C++ manpages quick access from VIM

Vim has by default a helpful shortcut to open the man page of the word under cursor, Shift + k, (or captital K), but very frequently we have shell commands with the same name of a C function, and for example

man printf actually opens the man page for the command, not the C function, but

man 3 printf opens the man pages of the C function, so to open this same man page in Vim, you need to use Shift + 3k (3K)

and for example readlink C function it's man 2 readlink, so in Vim it is Shift + 2k (2K)

Install C++ man pages libstdc++-10-doc


Saturday, June 20, 2020

Execute Comand in normal mode in all lines of the file


For example to add the word thiago in the end of all lines of a file
:%normal Athiago


Other Example

To remove, second names from all lines

Joana Dack
Ada Lovelace
Fabia Carla

:%normal WhD

To remove middle name

Joana Dack Silva
Ada Lovelace Silva
Fabia Carla Silva

:%normal Wdw

Grep lines on VIM

Thiago Masaki
Joana Dark
Fulando Masaki
Siclano de tal


:g/Masaki    - show lines which contains Masaki


:g/Masaki/d  - delete  lines which contains Masaki

Insert text in block based on first line

For example

Name: Ana
Bia
Carol
Dani
Elza
Fabi

Ctr+v  - to enter block selection
j  until reach Fabi

shift + i

Start typing
Ctrl + y 
It will type char by char the same characters from the previous line

Esc to complete all lines


Or to append text from clipboard in the end of a block

Ctrl+v
j until reach Fabi
$  - to block selection go to the end of the line
A - to enter append mode

Ctr+r, "

Spell Checker and Dictionary search in VIM

:set spell
:set spelllang=pt_br

Vim start highlighting wrong senteces in the language selected


Start typed a word for example
paral
Ctrl+x, Ctr+k         - This will open menu with word of the dictionary


Navigate in wrong words, 
[s    -   backwards
 ]s    -  forward

Show possible fix to the current wrong word
 z=


If  you have two splits or buffer set for only one buffer or split

:setlocal  spell spelllang=en_us


to disable

:set nospell

Friday, June 19, 2020

Vim Show lines relative to the cursor position


:set relativenumber




It is useful when you want to delete a block of lines, quickly without entering in visual mode and selecting 

For example, 
To delete lines Setima Linha (0) until  Nona Linha (2) , just type:
 d 2 j

To delete lines from Setima Linha (0) until Segunda Linha (5), just type:
 d 5 k

Tuesday, May 26, 2020

Disable bell in Vim, Bash

Disable bell in bash autocompletion


/etc/inputrc

 set bell-style none


 Disable bell when pressing ESC in Vim

 ~/.vimrc

set visualbell

Saturday, January 25, 2020

Make your Linux Terminal and VIM statusline more visually beautiful and eye candy with Powerline






sudo apt-get install powerline python3-powerline


.bashrc
powerline-daemon -q POWERLINE_BASH_CONTINUATION=1 POWERLINE_BASH_SELECT=1 . /usr/share/powerline/bindings/bash/powerline.sh

.vimrc
python3 from powerline.vim import setup as powerline_setup 
python3 powerline_setup() 
python3 del powerline_setup 
set laststatus=2 

Wednesday, September 13, 2017

Execute transformation inside VIM

.vimrc

function! XSLTransform()
    silent !clear
    wa
    exe 3 ."wincmd w"
    setl autoread
    "setl autoread | execute "!xsltproc #2 #1 > output.xml"
    "silent !xsltproc #2 #1 > output.xml
    silent !xsltproc transformation.xsl input.xml > output.xml
    wa
    redraw!
endfunction

noremap ,t :call XSLTransform()


3 different Layouts for viewing

vim -O /tmp/input.xml  /tmp/transformation.xsl /tmp/output.xml
vim -o -c "wincmd w|wincmd H" /tmp/input.xml  /tmp/transformation.xsl /tmp/output.xml
vim -O -c "wincmd w|wincmd K|resize +3" /tmp/input.xml  /tmp/transformation.xsl /tmp/output.xml

Tuesday, May 16, 2017

Find files in VIM

:e **/test/Suite.java
To find for example NmwsBus/plugins/libNmwsBusRMQ.c
Type 
:e **/**RMQ.c
and then press [TAB] to expland wildcards and start autocompletion
keep pressing TAB until find the corret file

Monday, December 26, 2016

Fill XML with tag name in uppecase very easy

vnoremap <F9> :s/:\(.*\)>.*</:\1>\U\1</g<CR> nnoremap <F9> :s/:\(.*\)>.*</:\1>\U\1</g<CR>

Monday, November 14, 2016

Delete blank lines in Vim

Blank lines
:g/^$/d

Lines with only tab or space
:g/^\s*$/d

Delete trailing whitespace

:%s/\s*$//g

Quick exit from vim

to save and exit
Shift + zz

to exit without saving
Shift +zq

Friday, October 28, 2016

Delete lines out of selection and delete search pattern


Use visual mode to select

than

:

Ctrl + u     - to clean command line

than

:v/\%V/d
 
 
Example 
:v/\%V/d|%s/orch://g|%s/?//g|%s/<!--Optional:-->//g 
 
 
 
Delete search pattern
 
:g/<pattern>/d

Blog Archive