How To Add Bookmarks Inside Vim Editor

本文详细介绍了 Vim 编辑器中如何创建和访问本地及全局书签,包括如何使用 `ma`, `a`, `ma`, 和 `G` 等命令进行操作,并展示了如何通过 `:marks` 命令查看所有书签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Vim Local Bookmark

Within a single file when you want to go to a particular position or line, then you can use local marking. If the bookmark name is a lower case letter, then that is local mark.

How to Create Bookmark Inside Vi?

Type m{macro-name}. macro-name is an alphabet to refer to the name of the bookmark.


If you type “ma” , it will create bookmark on the current line at the current location with name “a”. In the following example, typing ma has created a bookmark at the exact location where the cursor is highlighted.

ma

Vi Bookmark a Line inside file

Fig 1: Bookmark current location inside Vim using ma

Two Methods To Access Bookmarked Line Inside Vi

Method 1: `{macro-name}

backtick followed by the macro name. Move to the exact bookmark location. This will jump to the exact character location within the line from where it was bookmarked earlier.


For example, if you type `a , it will take you to the bookmark with name “a”. i.e It will take you to the place where the cursor is high-lighted in the above Fig 1.

`a

Method 2: ‘{macro-name}

single-quote followed by the macro name. Move to the beginning of the bookmarked line.


For example, if you type ‘a , it will take you to beginning of the line that has the bookmark with name “a”. It will take you to the the beginning of the “CustomLog logs/access_log combined” line in the above Fig 1.

'a

2. Vim Global Bookmark

When you have multiple files open, if you want to go to a particular position in any one of the open files, then you can use Global mark feature of Vim. If the bookmark name is an upper case letter, then that is a Global Bookmark.

Following sequence of steps will explain how to use global bookmark while editing multiple files.

  1. Open multiple files: # vim /etc/passwd /etc/group
  2. While editing /etc/passwd file go to a specific line and type mP to create a global bookmark called P
  3. Type :n from /etc/passwd file to jump to /etc/group file in vim.
  4. While editing /etc/group file go to a specific line and type mG to create a global bookmark called G
  5. Type `P (back-tick followed by upper-case P), which will take you to the bookmark in /etc/passwd
  6. From /etc/passwd, type `G (back-tick followed by upper-case G), which will take you to the bookmark in /etc/group.

3. How to Display All Bookmarks

If you’ve created several bookmarks and don’t remember them, you can easily get a list of all the bookmarks by typing :marks as shown below.

:marks
mark line  col file/text
 a     15    9 ypserver 192.168.1.119
 b     11   18 domain THEGEEKSTUFF
 G     56    0 group
 P     45    0 passwd


This indicates that I have created the following bookmarks:

  • a – local bookmark with name “a” at line 15 and col 9. This also give the text of line#15 . This is from the current open file, which is yp.conf
  • b – local bookmark with name “b” at line 11 and col 18. This also gives the text of line#18. This is from the current open file, which is yp.conf
  • G – global bookmark with name “G” at line 56 and col 0 of “group” file
  • P – global bookmark with name “P” at line 45 and col 0 of “passwd” file.


Apart from the above bookmarks, anytime you type :marks inside Vim, you may get the following lines. These marks ‘ (single-quote), ” (double quote), [ , ], ^ and . (period) are created by vim by default and you don’t have any control over it.

:marks
mark line  col file/text
 '      8   12 #^IUse  broadcast  on  the local net for domain NISDOMAIN
 "      1    0 # /etc/yp.conf - ypbind configuration file
 [     11    0 domain THEGEEKSTUFF
 ]     11   19 domain THEGEEKSTUFF
 ^     11   19 domain THEGEEKSTUFF
 .     11   18 domain THEGEEKSTUFF


For example, the mark . (period) indicates the last position where a change was made. So, you can always do `. (back-tick followed by period), which will take you to the position where the last change was done.

4. Quick Summary of Vim Bookmark Commands

  • ma – Creates a bookmark called a
  • `a – Jump to the exact location (line and column) of the bookmark a
  • ‘a – Jump to the beginning of the line of the bookmark a
  • :marks – Display all the bookmarks
  • :marks a – Display the details of the bookmark with name a
    `. – Jump to the exact location (line and column) where the last change was performed
  • ‘. – Jump to the beginning of the line where the last change was performed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值