Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

neovim insert filename or path into buffer #42

Open
practicalli-johnny opened this issue Oct 6, 2023 · 0 comments
Open

neovim insert filename or path into buffer #42

practicalli-johnny opened this issue Oct 6, 2023 · 0 comments
Assignees

Comments

@practicalli-johnny
Copy link
Contributor

practicalli-johnny commented Oct 6, 2023

How to get the filename or full path of a file and then past it into a buffer

Use cases

  • mkdocs navigation

Normal mode

" % is the neovim register that holds the filename associated with the current buffer (if it has a file associated with it)

" % p will past the filename into the buffer in normal mode

Vim command

:read inserts the output of a command into the current buffer

:read echo % inserts the filename

:read! echo %:p inserts the full path of the file

:help read and :help filename-modifiers for more details

:put % inserts the filename from the % register

:put=expand('%:p') inserts the full path to the file

A more terse approach is to use !! which replaces the current line in the buffer with the result of the next command

!!echo %

Use basename to ensure only the the file name even when the register contains the full path

`!!basename %``

Bind a key (review)

inserts the current filename without the extension at the cursor position, when you are in insert mode.

:inoremap \fn =expand("%:t:r")
To keep the extension use:

:inoremap \fn =expand("%:t")
To insert the absolute path of the directory the file is in use:

:inoremap \fn =expand("%:p:h")
To insert the relative path of the directory the file is in use:

:inoremap \fn =expand("%:h")

@practicalli-johnny practicalli-johnny converted this from a draft issue Oct 6, 2023
@practicalli-johnny practicalli-johnny self-assigned this Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests

1 participant