We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
How to get the filename or full path of a file and then past it into a buffer
" % 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
" % p
:read inserts the output of a command into the current buffer
:read
:read echo % inserts the filename
:read echo %
:read! echo %:p inserts the full path of the file
:read! echo %:p
:help read and :help filename-modifiers for more details
:help read
:help filename-modifiers
:put % inserts the filename from the % register
:put %
%
:put=expand('%:p') inserts the full path to the file
:put=expand('%:p')
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
`!!basename %``
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")
The text was updated successfully, but these errors were encountered:
practicalli-johnny
No branches or pull requests
How to get the filename or full path of a file and then past it into a buffer
Use cases
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 modeVim 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:put %
inserts the filename from the%
register:put=expand('%:p')
inserts the full path to the fileA 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")
The text was updated successfully, but these errors were encountered: