Posted: . At: 10:34 AM. This was 5 months ago. Post ID: 18840
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Some very useful VIM tricks for Linux.


To exit VIM without an Esc key, press Control-C to exit insert mode and type :wq to exit the editor. This is very useful when you want to use keys closer to the home keys to use the editor.

To switch on Syntax highlighting in the VIM editor, use this in command mode.

Bash
~                                                                                                                                                                                                                                                            
~                                                                                                                                                                                                                                                            
~                                                                                                                                                                                                                                                            
~                                                                                                                                                                                                                                                            
~                                                                                                                                                                                                                                                            
:syntax on

Below is a very nice ~/.vimrc file to make it much more usable.

~/.vimrc
" ~/.vimrc (configuration file for vim only)
" skeletons
function! SKEL_spec()
0r /usr/share/vim/current/skeletons/skeleton.spec
language time en_US
if $USER != ''
let login = $USER
elseif $LOGNAME != ''
let login = $LOGNAME
else
let login = 'unknown'
endif
let newline = stridx(login, "\n")
if newline != -1
let login = strpart(login, 0, newline)
endif
if $HOSTNAME != ''
let hostname = $HOSTNAME
else
let hostname = system('hostname -f')
if v:shell_error
let hostname = 'localhost'
endif
endif
let newline = stridx(hostname, "\n")
if newline != -1
let hostname = strpart(hostname, 0, newline)
endif
exe "%s/specRPM_CREATION_DATE/" . strftime("%a\ %b\ %d\ %Y") . "/ge"
exe "%s/specRPM_CREATION_AUTHOR_MAIL/" . login . "@" . hostname . "/ge"
exe "%s/specRPM_CREATION_NAME/" . expand("%:t:r") . "/ge"
setf spec
endfunction
autocmd BufNewFile      *.spec  call SKEL_spec()
" filetypes
filetype plugin on
filetype indent on
syntax on

" enable setting title
set title
" configure title to look like: Vim /path/to/file
set titlestring=VIM:\ %-25.55F\ %a%r%m titlelen=70

" ~/.vimrc ends here
set nu

Use this command in command mode to count the number of lines in a file. Have the cursor at the bottom of the file first and it will count the number of lines up to the cursor.

Bash
echo line("'>") - line("'<") + 1

Another way is to use visual mode and press g, then Control-g. This prints the line count as well as the number of bytes in the file. This is not dependent upon the cursor position.

Bash
Col 1 of 18; Line 1 of 25; Word 1 of 38; Byte 1 of 858

To get help on this function, type this in command mode. use :q in command mode to close the help page.

Bash
:he count-items

This simple function in command mode will count the number of words and lines.

Bash
:%s/\i\+/&/gn

10 matches on 6 lines

You may also count the number of occurrences of a certain word in a large text file.

Bash
:%s/\<DOOM\>/&/gn

This will count the number of times the word DOOM appears in a text file.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.