VIM
VIM is a command line based text editor, Its Open Source and higly configurable. its a very popular editor among linux developers and people who use command line to edit and modify files.
Installation
sudo apt-get install vim
VIM Modes
VI Support two modes
- Command Mode
- Normal Mode
- Insert Mode
- Visual Mode
Command Mode
Normal Mode
Insert Mode
Visual Mode
Moving in code block
Move cursor end of current function
]]
Move cursor starting of current function
[[
Move between matching (), {} and []
%
vim not to add end-of-file
:set nofixendofline
Set Shift width
set shiftwidth=4
Set Tab Length
set tabstop=4
Enable line Numbering
set nu
Set Color for Colume boundry
set colorcolumn=80
vimrc file
Language related configuration
vim will use language related file for syntex higlighting, indentation ... etc.
global language syntax file is located in /usr/share/vim/vim<version>/syntax/
NOTE: if your vim version is 8.2 then location will be /usr/share/vim/vim82/syntax/
for example: c language file will be /usr/share/vim/vim82/syntax/c.vim
if you want to customized language related file then make your own ~/.vim/syntax/c.vim and add syntax for your own. (override)
For example: if you want to add new atoms with name RCULOCK then use below patch
- syn keyword cTodo contained TODO FIXME XXX
+ syn keyword cTodo contained TODO FIXME XXX RCULOCK
Auto indentation for c program
The Vim editor has a autoindent option which indents each line the same the previous one. The vim editor does a much better job of indentation. The cindent option is set with the command:
:set cindent
This turns on C style indentation. Each new line will be automatically indented the correct amount according to the C indentation standard.
set the size of indentation
for four space indentation
:set sw=4
Add indentation guide/line
:set listchars=tab:\|\
:set list
Enable syntax
:syntax on
Disable syntax
:syntax off
Search a string partially
/string-to-search
incremental searching
incremental searches will be done. The Vim editor will start searching when you type the first character of the search string. As you type in more characters, the search is refined. To use this feature use
:set incsearch
higlite the search string
for hilighting
:set hlsearch
for disable hilight
:set nohlsearch
Search a exact string
/\<string-to-search\>
if you want to search a string having start after a pattern(it is partially to last) then use
/pattern\<string-to-search\>
for example:
emsp /a <char>
will matches with
a char
na char
char
visa char \
NOTE: this can be also done by short kut key. To use this, place the cursor at that string u want to search and press #
search special character
/ \special-charector note:- there are no spece between / and \
copy a block of code and paste at different place
or copy
move a block of code to another place
for copy
find a variable / function name and use them
ctrl+p -used to find variable/function after cursor ctrl+n -used to find variable/function above cursor
use:
eg: if u want to search a variable name packet
then type pa and press ctrl+p or ctrl+n
according to requirment.
go to starting of a function or go to end of a function (i.e matching parentheses)
put the cursor at any parentheses and press %
split the editor into multiple windows(vsplit / hsplit) and open other file
or varticle split:
:vsplit
for horizontal split
:split
to switch window use
ctrl+w direction-key
note:- direction key are
h : for left
l : right
j : down
k : up
to open another file with splite use
:split file-name for horizontal split
:vsplit file-name for verticle split
to open file after splite use
:edit file-name
to copy between multiple windows use
yy for copy
and p for paste.
To close all windows excluding cursor windows use
:only
to close cursor windows use
:hide
to readonly splite windows use
:sview file-name
to edit another file use
:e file-name
to list all open windows( i.e buffers) use
:ls
to switch or open windows( i.e buffer ) use
:b buffer-number
to maximize current windows use
ctrl-w_
to make all windows equal size use
ctrl-w=
jump at a specific line
:line-number
alignment
G=gg
enable and disable syntax hylighting
for enable syntax: :syntax on for disable syntax: :syntax off
Abbreviations
in insert mode
:iab abbreviation string-to-fill-in-place-of-abbreviations
example
:iab #i #include
if in insert mode, u type #i and then press a enter or space then automatically #i string change into #include. This methood is used for speed typing. Here some example.
:iab #b /****************************************
:iab #e ^V^H************************************/
to remove a single abbreviations use
:una abbreviation
to remove all abbreviations use
:abc
browse man page
palce the cursor at the name of command of function to search in man page and use K or shift+k
execute commands (without leaving editor)
:!command-to-execute
To delate all lines of file
use gg to move the cursor at the first line and then type dG
ctags with vim
1.Run Ctags recursively on your project-root-directry to generate the tags file. ctags -R
2.To search for a specific tag and open Vim to its definition, run the following command in your shell:
vim -t <tag-name>
3.Or, open any Linux source file in Vim and use the following basic
commands:
Keyboard command
Action
Ctrl-]
Jump to the tag underneath the cursor
:ts
cscope
There are a few easy steps required to start using Cscope. First, you need to tell it where all of your source code files are. Second, you need to generate the Cscope database. Finally, you can launch the Cscope browser to search for functions and symbols in your source code. Here are commands to perform these steps for a small C project, assumed to be in the directory ~/small- project/.
First, a small bit of setup: you should set the editor that Cscope will open your search results in. The default editor is vi; if you want to change it to something else, set the CSCOPE_EDITOR environment variable, e.g.:
export CSCOPE_EDITOR=`which emacs`
Note that those are backticks, not single-quotes, in that command. Put this command in your .bashrc file if you'd like.
d to the top-level of your project directory and then use a find command to gather up all of the source code files in your project. The following command will recursively find all of the .c, .cpp, .h, and .hpp files in your current directory and any subdirectories, and store the list of these filenames in cscope.files:
cd ~/small-project/
find . -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" > cscope.files
Depending on your project, you can use additional file extensions in this command, such as .java, .py, .s, etc.
Now, pass the list of source files to Cscope, which will build a reference database:
cscope -q -R -b -i cscope.files
The -q flag is used to build a faster (but larger) database. -R tells Cscope to search for symbols recursively. -b builds the database only, but does not start the Cscope browser. -i cscope.files specifies the list of source files. The output of this command will be a set of files in your current directory: cscope.in.out, cscope.out, and cscope.po.out.
Finally, start the Cscope browser:
cscope -d
The -d flag tells Cscope not to regenerate the database (which you already did in the previous step). Within the Cscope browser, type ? to see the help page, and type Ctrl-d to exit. The browser will show you a list of the searches you can perform in your code:
Find this C symbol:
Find this global definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
Most of these should be self-explanatory. C symbols include pretty much anything that you can think of in a C file: function names, variable names,
things that are #define'd, etc. You can search for all instances of a symbol, or find its original definition. Cscope can find all of the functions that call a particular function, which can be extremely useful; this is a feature of Cscope that other tools like Ctags do not have. If you find that the C symbol and function searches do not find what you are looking for, you can fall back to the text search options at the bottom of the list.
Select the type of search that you'd like to perform, type in your search term and hit Enter. At the top of the screen Cscope will display a list of results with the file, function, and line where the search term was found. If you select one of these results and hit Enter, Cscope will open up the editor to the matching line in the file. You can manipulate the file as you please, and when you close it the browser will appear again. When you're finished, press Ctrl-d to exit.
Basic search and replace
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:s/foo/bar/g Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'. :%s/foo/bar/gc Change each 'foo' to 'bar', but ask for confirmation first. :%s/<foo>/bar/gc Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. :%s/foo/bar/gci Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation. :%s/foo\c/bar/gc is the same because \c makes the search case insensitive. This may be wanted after using :set noignorecase to make searches case sensitive (the default). :%s/foo/bar/gcI Change each 'foo' (case sensitive due to the I flag) to 'bar'; ask for confirmation. :%s/foo\C/bar/gc is the same because \C makes the search case sensitive. This may be wanted after using :set ignorecase to make searches case insensitive.
https://stackoverflow.com/questions/4097259/in-vim-how-do-i-highlight-todo-and-fixme
©2023-2024 rculock.com